Jump to content

Convert Unicode value to ASCII value for use in Query


rocafreestyler

Recommended Posts

i have a function that returns a value in Unicode format. When I try and make a MySQL select statement with the Unicoce value it does not work.

 

So I need to change this Unicode value into a ASCII value that I can use in my database query.

 

Any help would be greatly appeciacted

 

Regards

function NC_DecryptString($key, $encryptedString)  
{  
    // is the mcrypt extension installed?  
    if (!extension_loaded('mcrypt'))  
    { 
        return false;  
    } 

    // first, convert the HEX-Encoded string to real characters  
    $deHexedString = "";  
    $originalStringLength = strlen($encryptedString);  
    if (($originalStringLength % 2) != 0)  
    {  
        return false;  
    }  

    for ($i = 0 ; $i < $originalStringLength ; $i += 2)  
    {  
        list($value) = sscanf(substr($encryptedString, $i, 2), "%x");  

        $deHexedString .= chr($value);  
    }  

    // make sure the key isn't too long;  
    $key = substr($key, 0, 56);  

    // prepare to decrypt  
    $td = mcrypt_module_open(MCRYPT_BLOWFISH, '', MCRYPT_MODE_CBC, '');  
    $iv = str_repeat(chr(0), ;  
    mcrypt_generic_init($td, $key, $iv);  

    // decrypt the data  
    return mdecrypt_generic($td, $deHexedString);  
}  
// Test decrypting data. 
$s = NC_DecryptString("Some magic key", "DA3111ED52E51A3E8A0AA734C833799D7074730274D9F8C6A561423CC9AB2C7C");  
if ($s !== false)  
{  
    echo $s . '<br>'; 
    echo 'Length: ' . strlen($s) . '<br>'; 
    for ($i = 0 ; $i < strlen($s) ; $i++) 
    { 
        echo ord($s[$i]) . '  ';  
    } 
}  
else  
{  
    echo "There was a failure.";  
}   

echo '<hr>';

 

The output of NC_DecryptString is Unicode.

 

I think the reason that it is not working is because of null values in the string that I need to strip out.

 

So the value i receive back from this function does not work in my mySQL query, but if I save the string it appears correct but if I do a strLen on it it is over double the length I would expect

 

Cheers

 

Cheers

 

if you have extra characters then your not encoding it correctly, either in the html or the database (i guess it could just be a font issule),

if the that function fails when no $encryptedString is passed just an error check!

 

e.g.

if (empty($encryptedString)) return false;

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.