Jump to content

Can't get the unicode character


asmith

Recommended Posts

Hey guys

 

I'm trying to get some text from various parts of a file. I have converted the file to hex (bin2hex) and I've got this:

 

64 72 e6 62 65 72  (without spaces)

 

I'm converting that back by:

 

<?php
function hex2bin($h)
{
if (!is_string($h)) return null;
$r='';
for ($a=0; $a<strlen($h); $a+=2)
	$r .= chr(hexdec($h{$a}.$h{($a+1)}));

return $r;
}
?>

 

It works fine, except for unicode characters. For example, the above hex is giving me:

 

dr�ber

 

While it must be:

 

dræber

 

How can I get the correct word?

Thanks a lot

Link to comment
Share on other sites

Try this: It'll retain the codepoints:

<?php
define('HEX2BIN_WS', " \t\n\r");
function hex2bin($hex_string) {
    $pos = 0;
$result = '';
while ($pos < strlen($hex_string)) {
  if (strpos(HEX2BIN_WS, $hex_string{$pos}) !== FALSE) {
    $pos++;
  } else {
    $code = hexdec(substr($hex_string, $pos, 2));
	$pos = $pos + 2;
    $result .= chr($code); 
  }
}
return $result;
}

echo hex2bin('6472e6626572'); //Returns 'dræber'
?>

Link to comment
Share on other sites

Thanks for your reply. It is working fine I guess.

 

But when I have this on my pages:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

 

It kinda mess with it again.

Any idea?

 

ASCII is the default character set from binary, you need to simply encode it in unicode, try this example:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<?php
define('HEX2BIN_WS', " \t\n\r");
function hex2bin($hex_string) {
    $pos = 0;
$result = '';
while ($pos < strlen($hex_string)) {
  if (strpos(HEX2BIN_WS, $hex_string{$pos}) !== FALSE) {
    $pos++;
  } else {
    $code = hexdec(substr($hex_string, $pos, 2));
	$pos = $pos + 2;
    $result .= chr($code); 
  }
}
return utf8_encode($result);
}

echo hex2bin('6472e6626572');
?>
</body
</html>

 

And it should return it correctly. If it doesn't, than add the header from serverside..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.