どうも、ちょげ(@chogetarou)です。
文字列(string)のバイト配列に変換する方法を紹介します。
方法

文字列(string)のバイト配列に変換するには、unpack()を使います。
まず、unpack()を呼び出します。
そして、unpacl()の第1引数に変換のフォーマット、第2引数に対象の文字列を指定します。
//char=指定文字, text=対象の文字列,
$result = unpack(format, $text);
上記のunpack()は、対象の文字列(string)をフォーマットに基づいて、バイト配列に変換した結果を返します。
使用例
<?php
$text = "Hello,World";
$result = unpack("C*", $text);
var_dump($result);
?>
出力:
array(11) {
[1]=>
int(72)
[2]=>
int(101)
[3]=>
int(108)
[4]=>
int(108)
[5]=>
int(111)
[6]=>
int(44)
[7]=>
int(87)
[8]=>
int(111)
[9]=>
int(114)
[10]=>
int(108)
[11]=>
int(100)
}
コメント