46 users online (0 members and 46 guests)  


  Results 1 to 4 of 4

Related

  1. allow french characters using javascript    Forum: Javascript Forum
    Replies: 0
  2. Help! ASP convert PDF    Forum: ASP Forum
    Replies: 5
  3. how to match the characters in file ?    Forum: CGI Perl Forum
    Replies: 1
  4. How many characters in a confirm/alert box?    Forum: Javascript Forum
    Replies: 1
  5. How to unescape characters    Forum: CGI Perl Forum
    Replies: 1
  1. #1
    flashkid's Avatar
    New User

    Status
    Offline
    Join Date
    Aug 2005
    Posts
    2

    Convert UTF8 characters to Hexadecimal

    I want to convert UTF8 characters to Hexadecimal numbers. For example:

    if I have a string like:

    HI

    I want the Hex to be like :

    \u0048\u0049

    Can this be done?

    Thanx

  2. #2
    ALL's Avatar
    Super Dooper Nerd

    Status
    Offline
    Join Date
    Feb 2005
    Location
    localhost
    Posts
    382

    Re: Convert UTF8 characters to Hexadecimal

    Code:
    $somestring = "this is a test";
    for ($i = 0; $i <= length($somestring); $i++) {
        $arychrs[$i] = dec2hex(ord(substr($somestring, $i, 1)));
    }
    sub dec2hex {
        # parameter passed to
        # the subfunction
        my $decnum = $_[0];
        # the final hex number
        my $hexnum;
        my $tempval;
        while ($decnum != 0) {
        # get the remainder (modulus function)
        # by dividing by 16
        $tempval = $decnum % 16;
        # convert to the appropriate letter
        # if the value is greater than 9
        if ($tempval > 9) {
        $tempval = chr($tempval + 55);
        }
        # 'concatenate' the number to 
        # what we have so far in what will
        # be the final variable
        $hexnum = $tempval . $hexnum ;
        # new actually divide by 16, and 
        # keep the integer value of the 
        # answer
        $decnum = int($decnum / 16); 
        # if we cant divide by 16, this is the
        # last step
        if ($decnum < 16) {
        # convert to letters again..
        if ($decnum > 9) {
        $decnum = chr($decnum + 55);
        }
        
        # add this onto the final answer.. 
        # reset decnum variable to zero so loop
        # will exit
        $hexnum = $decnum . $hexnum; 
        $decnum = 0 
        }
        }
        return $hexnum;
        } # end sub
    try that, it is untested and may have bugs...

    ps: i got the dec2hex from another site!

  3. #3
    flashkid's Avatar
    New User

    Status
    Offline
    Join Date
    Aug 2005
    Posts
    2

    Re: Convert UTF8 characters to Hexadecimal

    ALL,

    Thank you for your precious effort.

    After lots of experiments and searches, I discovered that
    using ord(character) works only on the first 255 characters which are the iso latin 1 characters.
    Characters above 255 (Arabic in my case) won't return the appropriate hexadecimal number of the unicode character.

    Thank you again.

  4. #4
    ALL's Avatar
    Super Dooper Nerd

    Status
    Offline
    Join Date
    Feb 2005
    Location
    localhost
    Posts
    382

    Re: Convert UTF8 characters to Hexadecimal

    well i have only experiance with 255 character set. anything above that may not work on every computer. sorry for your problem

    -ALL