Say I have a string in php, that prints out to a text file like this:
nÖ§9q1Fª£
How do I get the byte codes of this to my text file rather than the funky ascii characters?
Use the PHP strlen() function to get the number of bytes of a string. Use the PHP mb_strlen() function to get the number of characters in a string with a specific encoding.
Convert byte[] to String (text data) toString() to get the string from the bytes; The bytes. toString() only returns the address of the object in memory, NOT converting byte[] to a string ! The correct way to convert byte[] to string is new String(bytes, StandardCharsets. UTF_8) .
One method is to create a string variable and then append the byte value to the string variable with the help of + operator. This will directly convert the byte value to a string and add it in the string variable. The simplest way to do so is using valueOf() method of String class in java.
LENB returns the number of bytes used to represent the characters in a text string.
Use the ord function
http://ca.php.net/ord
eg.
<?php
$var = "nÖ§9q1Fª£ˆæÓ§Œ_»—Ló]j";
for($i = 0; $i < strlen($var); $i++)
{
echo ord($var[$i])."<br/>";
}
?>
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!
If you are talking about the hex value, this should do for you:
$value = unpack('H*', "Stack");
echo $value[1];
Reference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With