Encoding/decoding string in hexadecimal and back
Use pack()
and unpack()
:
function hex2str( $hex ) {
return pack('H*', $hex);
}
function str2hex( $str ) {
return array_shift( unpack('H*', $str) );
}
$txt = 'This is test';
$hex = str2hex( $txt );
$str = hex2str( $hex );
echo "{$txt} => {$hex} => {$str}\n";
would produce
This is test => 546869732069732074657374 => This is test
PHP - Converting Hex string to uint64 and to int64
Finally managed to solve the problem
unpack("J",hex2bin("eb3dd796efda2409"));
seems hex2bin messed up the endianness so when we tried to do J it resolves correctly
Thank you for helping!
php string to hex with2's complement:
Solution you are looking for is as below,
It is referenced from one of the answer given at Create hex-representation of signed int in PHP.
<?php
function signed2hex($value, $reverseEndianness = true)
{
$packed = pack('i', $value);
$hex='';
for ($i=0; $i < 4; $i++){
$hex .= strtoupper( str_pad( dechex(ord($packed[$i])) , 2, '0', STR_PAD_LEFT) );
}
$tmp = str_split($hex, 2);
$out = implode('', ($reverseEndianness ? array_reverse($tmp) : $tmp));
return $out;
}
echo signed2hex(193390663);
C# conversion Hex to String does not match PHP Hex to String
I think your c# algorithm Hex2Str
looks good, though I might suggest the following small change to avoid any possible inconsistencies with surrogate pair encoding:
public static string Hex2Str(string hexString)
{
var sb = new StringBuilder();
var len = hexString.Length / 2;
for (var i = 0; i < len; i++)
{
// Convert the number expressed in base-16 to an integer.
int value = Convert.ToInt32(hexString.Substring(i * 2, 2), 16);
string stringValue = Char.ConvertFromUtf32(value);
sb.Append(stringValue);
}
return sb.ToString();
}
The real problem here, I suspect, is that the string from PHP is being mangled when passed through the console due to inconsistent encodings. For instance, if the PHP console has Latin 9 (ISO)
encoding and your input console has OEM United States
encoding (which it is on my computer) then 'à' will be transformed to 'α'.
Instead, I recommend taking the additional step of encoding your PHP string in Base64 using base64_encode
before writing it to the console. This will guarantee a pure ASCII representation as it is passed through the console. Then decode as follows:
public static string FromPHPBase64String(string phpString)
{
var bytes = Convert.FromBase64String(phpString);
var sb = new StringBuilder();
foreach (var b in bytes)
{
string stringValue = char.ConvertFromUtf32(b);
sb.Append(stringValue);
}
return sb.ToString();
}
I believe everything should now match.
PHP convert string to \xnn hex
Should work
function strhex($string) {
$hexstr = unpack('H*', $string);
return array_shift($hexstr);
}
Update
Code below what you need
function strtohex($string)
{
$string = str_split($string);
foreach($string as &$char)
$char = "\x".dechex(ord($char));
return implode('',$string);
}
print strtohex("[0-9A-Za-z\+/=]*");
Related Topics
List All Files in One Directory PHP
Blocking Comment Spam Without Using Captcha
How to PHP-Unserialize a Jquery-Serialized Form
Disable PHP in Directory (Including All Sub-Directories) With .Htaccess
What's the Net::Err_Http2_Protocol_Error About
Facebook Graph API, How to Get Users Email
How to Run a PHP File in a Scheduled Task (Windows Task Scheduler)
Making Sure PHP Substr Finishes on a Word Not a Character
Execute PHP Scripts Within Node.Js Web Server
How to Find Out How "Deep" a PHP Array Is
How to Write a File in Utf-8 Format
Connect PHP to Mssql Via Pdo Odbc
Who Should Handle the Conditions in Complex Queries, the Data Mapper or the Service Layer