Convert GUID into Byte array using PHP?

2019-08-08 19:01发布

问题:

I need to convert a system-generated GUID into a Byte[16] array and then convert those decimal values into a hex value string. Crazy, I know and I'm very open to a more efficient approach than the ones I've tried!

I've tried PHPs unpack() function on this GUID but can't seem to get the right format. I've tried c*, C* and all of the other possible formats.

Here's the GUID I'm starting with:

GUID: dwRhRSIjlGXhAAAAkuFQ0A

The Byte[16] array I'm trying to populate should look like this:

$bytearray = array(119,4,97,69,34,35,-108,101,-31,0,0,0,-110,-31,80,-48);

Once I get this $bytearray value, I can easily use PHPs dechex() function to convert each of these decimal values into their corresponding hex values. The end result of this conversion should look like this:

DEC: HEX
119: 77
4: 4
97: 61
69: 45
34: 22
35: 23
-108: 94
101: 65
-31: E1
0: 00
0: 00
0: 00
-110: 92
-31: E1
80: 50
-48: D0

Again, any suggestions are greatly appreciated.

回答1:

I'm not sure I really get what you want… You want an array with the hex value of the ASCCI code of the chars within you ID, am I right?

$str = "dwRhRSIjlGXhAAAAkuFQ0A";
$bytes = array();

for($i=0,$l = strlen($str);$i<$l;$i++)
{
    $bytes[] = dechex(ord($str[$i]));
}