0

i want to convert string into hexdecimal value..

Like
'Hi'

into

00680069

<?php
  function ascii2hex($ascii) {
$hex = '';
for ($i = 0; $i < strlen($ascii); $i++) {
$byte = strtoupper(dechex(ord($ascii{$i})));
$byte = str_repeat('0', 2 - strlen($byte)).$byte;
$hex.=$byte." ";
}
return $hex;
}
//echo ascii2hex('he');
echo dechex(ord('hello'));
?>

I got this function which convert it but works only for enlish char.
not for non english char like arabic...

Can anybody help me.? :O

3
Contributors
8
Replies
9
Views
7 Years
Discussion Span
Last Post by sam023
0

That function works perfectly. A hex is made of the values 0-9 and A-F. When I test that function it seem to follow those rules and does it perfectly. But are you after a function that converts string to decimal and not string to hex? Because that's a completely different thing.

0

That function works perfectly. A hex is made of the values 0-9 and A-F. When I test that function it seem to follow those rules and does it perfectly. But are you after a function that converts string to decimal and not string to hex? Because that's a completely different thing.

ya that function convert string into Hexadecimal..!! and work perfectly with english characters but not with non english char.

wht should i do when string is non english characters like Arabic..??
like مرحبا

how can i convert Arabic string into hexadecimal..?? and how can i insert Arabic string into database..??

Edited by sam023: n/a

0

مرحب

They are non-ascii characters and are unable to convert with the ord function. I would suggest finding a replacement for the ord function as the ascii table ranges from 0 to 255 and those characters are out of that range.

0

yeah that why i am here..!!

non english string is out of range of Ascii...!!

what should i do.. or is it not possible in php.?

0

I believe there are some custom scripts/functions that you can be downloaded so perhaps a google search for "php uniord function" might reveal a few functions.

0

ya that function convert string into Hexadecimal..!! and work perfectly with english characters but not with non english char.

wht should i do when string is non english characters like Arabic..??
like مرحبا

how can i convert Arabic string into hexadecimal..?? and how can i insert Arabic string into database..??

You should not be converting to hex to save to the DB. Just make sure your db is saving data in the correct encoding. MySQL is multibyte aware, PHP isn't natively up until PHP5 inclusive. So the only place to take care in is within PHP.

Saving strings as hex makes it very hard to use the MySQL string functions, and string indexing. If you save in the correct encoding to begin with, you do not have to worry about encoding conversion.

PHP5 and lower sees strings as a sequence of bytes. So any operation on the string is done per byte, neglecting any encoding. You will need to avoid using the native PHP string functions.

What I would do is use the UTF-8 functions, since UTF-8 is the defacto standard on the web. To do that first convert to UTF-8:
http://www.php.net/manual/en/function.mb-convert-encoding.php

After you convert all your data to UTF-8, you do not have to anymore if you tell the browser it is UTF-8, since the browser will send you UTF-8. Same for any other encoding.

Rules:

1) Tell the browser your encoding. (header('Content-Type...'))
2) Do not use native PHP string functions, use encoding specific ones
3) Tell MySQL your encoding (set encoding... http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html )

With those in mind you should be good.

0
<script>
function dec2hex ( textString ) {
    alert((textString+0).toString(16));
 return (textString+0).toString(16).toUpperCase(); // IN BETWEEN
 
}
function convertChar2CP ( textString ) { 
    var haut = 0;
    var n = 0;
    CPstring = '';
    for (var i = 0; i < textString.length; i++) {
        var b = textString.charCodeAt(i); 
       
            CPstring += dec2hex(b) + ' ';
           // alert(CPstring);
         
        }
    CPstring = CPstring.substring(0,CPstring.length-1);

    return CPstring;
    }
document.write(convertChar2CP('م'));   

</script>

i got this function in javascript which convert values including non english values into hexadecimal...
kindly help me to convert into php..!!

Thanx

Edited by sam023: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.