Convert GUID into Byte array using PHP?

岁酱吖の 提交于 2020-01-15 14:15:47

问题


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]));
}


来源:https://stackoverflow.com/questions/6051734/convert-guid-into-byte-array-using-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!