Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中将十六进制字符串转换为字节数组_Php_Hex - Fatal编程技术网

如何在php中将十六进制字符串转换为字节数组

如何在php中将十六进制字符串转换为字节数组,php,hex,Php,Hex,下面是c#实现 using System; public class Program { void CanDecodeFromString(string str) { if (str.Length % 2 != 0) return; //OutputText(" " + "J1939:" + "\r\n"); byte[] j1939data = new b

下面是c#实现

using System;

public class Program
{
     void CanDecodeFromString(string str)
        {
            if (str.Length % 2 != 0)
                return;
            //OutputText("    " + "J1939:" + "\r\n");

            byte[] j1939data = new byte[str.Length / 2];

            for (int i = 0; i < str.Length / 2; i++)
            {
                j1939data[i] = System.Convert.ToByte(str.Substring(2 * i, 2), 16);
            }
            //J1939DataDecode(j1939data);
         Console.WriteLine(String.Join("\n", j1939data));
        }
    public static void Main()
    {
        var p= new Program();
        p.CanDecodeFromString("31058F410C15E1310D48312F8F41310000630220290082");
        Console.WriteLine("Hello World");
    }

}
在php中,什么是正确的方法

样本输出

49
5
143
65
12
21
225
49
13
72
49
47
143
65
49
0
0
99
2
32
41
0
130

样本输出是什么?[0x31,0x05,0x8F…]?49 5 143 65 12 21 225 49 13 72 49 47 143 65 49 0 99 2 32 41 0 130Hi,您知道如何将长十六进制字符串转换为字节数组?示例输出是什么?[0x31,0x05,0x8F…]?49 5 143 65 12 21 225 49 13 72 49 47 143 65 49 0 99 2 32 41 0 130Hi,您知道如何将长十六进制字符串转换为字节数组了吗?
49
5
143
65
12
21
225
49
13
72
49
47
143
65
49
0
0
99
2
32
41
0
130