s4surbhi2218 Posted November 15, 2012 Share Posted November 15, 2012 HI, I want to write a code to convert each character of user enetered data to its equivalent four digit hexadecimal value. eg : if user enters "Hi i am good" convert H to its four digit hexadecimal convert i to its four digit hexadecimal convert blank to its four digit hexadecimal and so on. any suggestions would be great. Many thanks. Quote Link to comment Share on other sites More sharing options...
Barand Posted November 15, 2012 Share Posted November 15, 2012 printf ('%04X', ord($char)); Quote Link to comment Share on other sites More sharing options...
s4surbhi2218 Posted November 15, 2012 Author Share Posted November 15, 2012 printf ('%04X', ord($char)); and how to assign this to a variable? is this used only with printf()? Quote Link to comment Share on other sites More sharing options...
DavidAM Posted November 15, 2012 Share Posted November 15, 2012 sprintf Quote Link to comment Share on other sites More sharing options...
s4surbhi2218 Posted November 15, 2012 Author Share Posted November 15, 2012 (edited) sprintf got it thanks. Edited November 15, 2012 by s4surbhi2218 Quote Link to comment Share on other sites More sharing options...
s4surbhi2218 Posted November 15, 2012 Author Share Posted November 15, 2012 got it thanks. And how to convert the hex data to character again at user end? Quote Link to comment Share on other sites More sharing options...
silkfire Posted November 15, 2012 Share Posted November 15, 2012 Use the functions bin2hex() and hex2bin() for this. They were designed exactly for the purpose you need. Quote Link to comment Share on other sites More sharing options...
Barand Posted November 15, 2012 Share Posted November 15, 2012 And how to convert the hex data to character again at user end? $char = chr(hexdec('0041')); echo $char; // -> A Quote Link to comment Share on other sites More sharing options...
Barand Posted November 15, 2012 Share Posted November 15, 2012 Use the functions bin2hex() and hex2bin() for this. They were designed exactly for the purpose you need. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted November 15, 2012 Share Posted November 15, 2012 (edited) Unfortunately ord () and char () only works for ASCII-characters, which means that if you introduce multi-byte characters to the mix Barand's solution will not work. In this case you should be using unpack () to convert the string to hexadecimal values. If you really want to convert each character individually, then the following approach may be used: /** * Returns an array of the hexadecimal values for each character in the given string. * * @param string $string * @param string[optional] $charset = 'UTF-8' * @return array */ function hexify ($string, $charset = 'UTF-8') { $length = mb_strlen ($string, $charset); $retval = array (); for ($run = 0; $run < $length; $run++) { $temp = unpack ('H*', mb_substr ($string, $run, 1, $charset)); $retval[] = $temp[1]; } return $retval; } If it is imperative that the hexadecimal values returned are of at least 2 bytes long, then I'm sure you can sort it out sprintf (). Do remember to take into account that quite a few UTF-8 characters can be more than just 2 bytes long though. Edit: I should note that you can indeed use bin2hex () and hex2bin () for this purpose as well. As they do exactly the same as unpack () and pack () in this case. So while Silkfire's comment wasn't entirely accurate on the exact method needed, he was not wrong in his statement. Edited November 15, 2012 by Christian F. Quote Link to comment Share on other sites More sharing options...
s4surbhi2218 Posted November 16, 2012 Author Share Posted November 16, 2012 $char = chr(hexdec('0041')); echo $char; // -> A this did not give the exact output Quote Link to comment Share on other sites More sharing options...
Christian F. Posted November 17, 2012 Share Posted November 17, 2012 Then you've either done something wrong, or not used that exact code: php > $char = chr(hexdec('0041')); php > echo $char; A Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.