How to Byte Sequence a Character in PHP
The PHP programming language supports multi-byte characters using the Unicode standard. Underlying all characters is a sequence of binary data that contain the code for a specific letter, number or symbol. This is sometimes referred to as a byte sequence, particularly for Unicode characters that are composed of multiple bytes. With PHP, you can set characters by entering their byte sequence. You can also get the byte sequence of an already set character. This can be helpful when converting character encoding schemes.
Instructions
-
-
1
Decide how you will run your PHP code. If you have a PHP server, you can execute code using PHP files. If you do not have access to a PHP server, you can use an online PHP interpreter. Enter the code in this tutorial into either a PHP file or the online PHP interpreter.
-
2
Begin your PHP program with the following statement: <?php
-
-
3
Declare a variable and assign it the value 'A,' by writing this line of code: $str = 'A';
-
4
Get the ASCII value of the variable $str. ASCII values and Unicode values overlap, so this value will also be the Unicode value of the letter 'A.' To get the ASCII value, you can use the ord function like this: $str = ord($str);
-
5
Print out the byte sequence using printf, which allows you to print the raw bit sequence of a value. To print the byte sequence, you refer to a variable as %b in the output string. For example, to print the byte sequence of the variable $str, you can write this: printf('Byte sequence: %b', $str);
-
6
Conclude your PHP program with the statement "?>". Your program is now ready to be tested on your PHP server or online PHP interpreter.
-
7
Execute the program. The output looks like this: Byte sequence: 1000001
-
1