How can I convert an array of bytes into a UTF-8 string? I need this because I am extracting from a binary format.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A string is nothing more than an array of bytes. So a UTF-8 string is the very same as an array of bytes, except that in addition you know what the array of bytes represent.
So your input array of bytes needs one more additional information as well: the character set (character encoding). If you know the input character set, you can convert the array of bytes to another array of bytes representing an UTF-8 string.
The PHP method for doing that is called
mb_convert_encoding().PHP itself does not know of character sets (character encodings). So a string really is nothing more than an array of bytes. The application has to know how to handle that.
So if you have an array of bytes and want to turn that into a PHP string in order to convert the character set using
mb_convert_encoding(), try the following:(Instead of the single example above, have a look at more examples at https://stackoverflow.com/a/5473057/530502.)
$output_utf8then will be a PHP string of the input array of bytes converted to UTF-8.