Hướng dẫn check byte string php

Say I have a string in php, that prints out to a text file like this:

Show

nÖ§9q1Fª£

How do I get the byte codes of this to my text file rather than the funky ascii characters?

asked Feb 26, 2009 at 17:00

Use the ord function

http://ca.php.net/ord

eg.

";
}
?>

answered Feb 26, 2009 at 17:03

GautamGautam

2,0651 gold badge24 silver badges24 bronze badges

2

If You wish to get the string as an array of integer codes, there's a nice one-liner:

unpack('C*', $string)

Beware, the resulting array is indexed from 1, not from 0!

answered Mar 21, 2014 at 14:28

Hướng dẫn check byte string php

Roman HockeRoman Hocke

4,0971 gold badge19 silver badges34 bronze badges

0

If you are talking about the hex value, this should do for you:

$value = unpack('H*', "Stack");
echo $value[1];

Reference

answered Apr 18, 2013 at 10:26

AdeeAdee

4546 silver badges17 bronze badges

1

Ord() does the trick with an ASCII-charset. If you, however, meddle with multibyte strings (like UTF-8), you're out of luck, and need to hack it yourself.

answered Feb 26, 2009 at 17:06

Henrik PaulHenrik Paul

66k31 gold badges84 silver badges96 bronze badges

(PHP 4 >= 4.4.3, PHP 5 >= 5.1.3, PHP 7, PHP 8)

mb_check_encodingCheck if strings are valid for the specified encoding

Description

mb_check_encoding(array|string|null $value = null, ?string $encoding = null): bool

Parameters

value

The byte stream or array to check. If it is omitted, this function checks all the input from the beginning of the request.

Warning

As of PHP 8.1.0, omitting this parameter or passing null is deprecated.

encoding

The expected encoding.

Return Values

Returns true on success or false on failure.

Changelog

VersionDescription
8.1.0 Calling this function with null as value or without argument is deprecated.
8.0.0 value and encoding are nullable now.
7.2.0 This function now also accepts an array as value. Formerly, only strings have been supported.

How can I get the byte array from some string which can contain numbers, letters and so on? If you are familiar with Java, I am looking for the same functionality of the getBytes() method.

I tried a snippet like this one:

for($i = 0; $i < strlen($msg); $i++){
    $data.=ord($msg[$i]);
        //or $data[]=ord($msg[$1]); 
}

but without success, so any kind of help will be appreciated.

PS: Why do I need this at all!? Well, I need to send a byte array via fputs() to a server written in Java...

dokaspar

7,65714 gold badges67 silver badges93 bronze badges

asked May 19, 2009 at 23:49

@Sparr is right, but I guess you expected byte array like byte[] in C#. It's the same solution as Sparr did but instead of HEX you expected int presentation (range from 0 to 255) of each char. You can do as follows:

$byte_array = unpack('C*', 'The quick fox jumped over the lazy brown dog');
var_dump($byte_array);  // $byte_array should be int[] which can be converted
                        // to byte[] in C# since values are range of 0 - 255

By using var_dump you can see that elements are int (not string).

   array(44) {  [1]=>  int(84)  [2]=>  int(104) [3]=>  int(101) [4]=>  int(32)
[5]=> int(113)  [6]=>  int(117) [7]=>  int(105) [8]=>  int(99)  [9]=>  int(107)
[10]=> int(32)  [11]=> int(102) [12]=> int(111) [13]=> int(120) [14]=> int(32)
[15]=> int(106) [16]=> int(117) [17]=> int(109) [18]=> int(112) [19]=> int(101)
[20]=> int(100) [21]=> int(32)  [22]=> int(111) [23]=> int(118) [24]=> int(101)
[25]=> int(114) [26]=> int(32)  [27]=> int(116) [28]=> int(104) [29]=> int(101)
[30]=> int(32)  [31]=> int(108) [32]=> int(97)  [33]=> int(122) [34]=> int(121)
[35]=> int(32)  [36]=> int(98)  [37]=> int(114) [38]=> int(111) [39]=> int(119)
[40]=> int(110) [41]=> int(32)  [42]=> int(100) [43]=> int(111) [44]=> int(103) }

Be careful: the output array is of 1-based index (as it was pointed out in the comment)

answered Jul 13, 2012 at 8:22

BronekBronek

10.4k2 gold badges43 silver badges45 bronze badges

2

print_r(unpack("H*","The quick fox jumped over the lazy brown dog"))

Array ( [1] => 54686520717569636b20666f78206a756d706564206f76657220746865206c617a792062726f776e20646f67 ) 

T = 0x54, h = 0x68, ...

You can split the result into two-hex-character chunks if necessary.

answered May 20, 2009 at 0:29

SparrSparr

7,37730 silver badges47 bronze badges

7

PHP has no explicit byte type, but its string is already the equivalent of Java's byte array. You can safely write fputs($connection, "The quick brown fox …"). The only thing you must be aware of is character encoding, they must be the same on both sides. Use mb_convert_encoding() when in doubt.

answered Jul 13, 2011 at 11:48

soulmergesoulmerge

71.9k19 gold badges117 silver badges152 bronze badges

0

You could try this:

$in_str = 'this is a test';
$hex_ary = array();
foreach (str_split($in_str) as $chr) {
    $hex_ary[] = sprintf("%02X", ord($chr));
}
echo implode(' ',$hex_ary);

answered May 19, 2009 at 23:59

karim79karim79

336k66 gold badges410 silver badges405 bronze badges

In PHP, strings are bytestreams. What exactly are you trying to do?

Re: edit

Ps. Why do I need this at all!? Well I need to send via fputs() bytearray to server written in java...

fputs takes a string as argument. Most likely, you just need to pass your string to it. On the Java side of things, you should decode the data in whatever encoding, you're using in php (the default is iso-8859-1).

answered Jun 24, 2009 at 20:30

troelskntroelskn

112k24 gold badges132 silver badges154 bronze badges

0

I found several functions defined in http://tw1.php.net/unpack are very useful.
They can covert string to byte array and vice versa.

Take byteStr2byteArray() as an example:


answered Feb 13, 2014 at 5:53

1