PeterMader
PeterMader

Reputation: 7285

Converting ISO-8859-1 charcodes to UTF-8

I have an input string that looks like this:

4BFC434845000000

Every two characters in the input string represent the hexadecimal code in ISO-8859-1.

The example string above means Küche, which is the german word for kitchen.

The input string is guaranteed to be 16 characters long, so the resulting string will always be 8 characters long. Unused characters (like in the example) will be 00.

I know that I could use iconv or another function in PHP to convert an ISO-8859-1 string to another character encoding. However I don't know how to convert an ISO-8859-1 charcode (for example FC16 or 25210) to an UTF-8 character.

Of course, I could have an associative array to map all the charcodes to the characters they represent:

$table = array(
  0x4B => 'K',
  0xFC => 'ü',
  // ...
);

What would the best way to achieve the same? Is there a PHP function that does that?

Upvotes: 1

Views: 2551

Answers (1)

deceze
deceze

Reputation: 522510

It's rather trivial: convert the hex string to binary, convert the ISO-8859 binary to UTF-8 binary:

$input = '4BFC434845000000';
echo iconv('ISO-8859-1', 'UTF-8', hex2bin($input));

Optionally strip out the NUL bytes at some point.

Upvotes: 4

Related Questions