Re: PHP - Convert unicode to hex

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This may help.

<?
$test = 'अमोल';

function utf8_to_unicode( $str ) {

        $unicode = array();
        $values = array();
        $lookingFor = 1;

        for ($i = 0; $i < strlen( $str ); $i++ ) {
            $thisValue = ord( $str[ $i ] );
        if ( $thisValue < ord('A') ) {
            // exclude 0-9
            if ($thisValue >= ord('0') && $thisValue <= ord('9')) {
                 // number
                 $unicode[] = chr($thisValue);
            }
            else {
                 $unicode[] = '%'.dechex($thisValue);
            }
        } else {
              if ( $thisValue < 128)
            $unicode[] = $str[ $i ];
              else {
                    if ( count( $values ) == 0 ) $lookingFor = ( $thisValue
< 224 ) ? 2 : 3;
                    $values[] = $thisValue;
                    if ( count( $values ) == $lookingFor ) {
                        $number = ( $lookingFor == 3 ) ?
                            ( ( $values[0] % 16 ) * 4096 ) + ( ( $values[1]
% 64 ) * 64 ) + ( $values[2] % 64 ):
                            ( ( $values[0] % 32 ) * 64 ) + ( $values[1] % 64
);
                $number = dechex($number);
                $unicode[] =
(strlen($number)==3)?"%u0".$number:"%u".$number;
                        $values = array();
                        $lookingFor = 1;
              } // if
            } // if
        }
        } // for
        return implode("",$unicode);

    }
echo str_replace('%u', '', utf8_to_unicode($test));
?>



2009/4/30 vivek bansal <bansvivek@xxxxxxxxx>

>
>
> hi abishek,
> this is not the exact solotuin to ur problem but can give some idea to
> proceed
> this is a sample java code to convert string to hex
>
> /**
> * Convert a String to Hexa decimal form.
> */
> import java.io.*;
>
> class StringToHexConvert {
>
> String stringToHex(String str) {
> char[] chars = str.toCharArray();
> StringBuffer strBuffer = new StringBuffer();
> for (int i = 0; i < chars.length; i++) {
> strBuffer.append(Integer.toHexString((int)
> chars[i]));
> }
> return strBuffer.toString();
> }
>
> public static void main(String[] args) throws IOException
> {
> System.out.print("Enter the String : ");
> BufferedReader br =new BufferedReader(new
> InputStreamReader(System.in));
> String inputString = br.readLine();
> StringToHexConvert obj = new StringToHexConvert();
> String hexString = obj.stringToHex(inputString);
> System.out.println("String in hexa form : " +
> hexString);
> }
> }
>
> i know u want hindi to hex code
> when i got the solution to convert hindi to englist string i will mail it
> to
> u
>
> bye
>
>
> [Non-text portions of this message have been removed]
>
>  
>


[Non-text portions of this message have been removed]


[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Soap]     [Kernel Newbies]     [Yosemite]     [Yosemite Campsites]

  Powered by Linux