简体   繁体   English

将字节值的字符串数组转换为字节数组

[英]convert string array of byte values to a byte array

I have a textbox on a form where a person types in a byte array into in the format shown below. 我在表单上有一个文本框,其中一个人以下面显示的格式输入字节数组。

My question is how can I then convert the string array produced into a byte array of the same values? 我的问题是如何将生成的字符串数组转换为相同值的字节数组?

so this would be entered into the text box : 因此这将被输入到文本框中:

0x11, 0x01, 0x49, 0x4D, 0x41, 0x47, 0x45, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x53, 0x75, 0x6D, 0x6D, 0x61, 0x72, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00

the following code then splits it and converts it to a byte array 下面的代码然后将其拆分并将其转换为字节数组

string text = txtChecksumText.Text;
        string[] parts = text.Split(new string[] { ", " }, StringSplitOptions.None);
        byte[] bytes = new byte[parts.Length];

        for (int i = 0; i < parts.Length; i++)
        {
            bytes[i] = Convert.ToByte(parts[i], 16); // this isn't working as expected
            txtResponse.Text += Environment.NewLine + "     " + i + " = " + parts[i] + " = " + bytes[i].ToString() ;
        }

and the response to show it isn't working 并显示它不起作用的响应

 0 = 0x11 = 17
 1 = 0x01 = 1
 2 = 0x49 = 73
 3 = 0x4D = 77
 4 = 0x41 = 65
 5 = 0x47 = 71
 6 = 0x45 = 69
 7 = 0x31 = 49
 8 = 0x00 = 0
 9 = 0x00 = 0
 10 = 0x00 = 0
 11 = 0x00 = 0
 12 = 0x00 = 0
 13 = 0x00 = 0
 14 = 0x00 = 0
 15 = 0x00 = 0
 16 = 0x00 = 0
 17 = 0x00 = 0
 18 = 0x00 = 0
 19 = 0x00 = 0
 20 = 0x00 = 0
 21 = 0x00 = 0
 22 = 0x00 = 0
 23 = 0x01 = 1
 24 = 0x53 = 83
 25 = 0x75 = 117
 26 = 0x6D = 109
 27 = 0x6D = 109
 28 = 0x61 = 97
 29 = 0x72 = 114
 30 = 0x79 = 121
 31 = 0x00 = 0
 32 = 0x00 = 0
 33 = 0x00 = 0
 34 = 0x00 = 0
 35 = 0x00 = 0
 36 = 0x00 = 0
 37 = 0x00 = 0
 38 = 0x00 = 0
 39 = 0x00 = 0
 40 = 0x00 = 0
 41 = 0x00 = 0
 42 = 0x00 = 0
 43 = 0x00 = 0
 44 = 0x00 = 0
 45 = 0x00 = 0
 46 = 0x00 = 0
 47 = 0x00 = 0
 48 = 0x00 = 0
 49 = 0x00 = 0
 50 = 0x00 = 0
 51 = 0x00 = 0
 52 = 0x00 = 0
 53 = 0x00 = 0
 54 = 0x00 = 0
 55 = 0x00 = 0

Just to be clear, the 0x11 should come back as a byte 11 not byte 17, same with all the others I'm not trying to convert to decimal i'm trying to convert the string of literal bytes to a byte array for check-sum creation 只需清楚一点,0x11应该以字节11而不是字节17的形式返回,与所有其他我不想转换为十进制的其他字符一样,我试图将文字字节的字符串转换为字节数组以进行检查-总和创造

The bytes you're getting are just bytes; 您得到的字节只是字节; they aren't intrinsically decimal or hexadecimal. 它们本质上不是十进制或十六进制。

If you want to pass the bytes to something else (for a checksum), they're fine. 如果您想将字节传递给其他对象(用于校验和),则可以。

Your only problem is that you're writing them to the console in decimal form - use ToString("x") if you wish to write them out in hexadecimal form for any reason. 唯一的问题是您将它们以十进制形式写入控制台-如果您出于任何原因希望以十六进制形式写入它们,请使用ToString("x")

Please do the following: 请执行以下操作:

txtHexString.Text="0x11, 0x01, 0x49, 0x4D, 0x41, 0x47, 0x45, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x53, 0x75, 0x6D, 0x6D, 0x61, 0x72, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00";

string[] namesArray =  txtHexString.Text.Split(',');
byte[] abc= new byte [namesArray.Length];

for (int i = 0; i <= namesArray.Length - 1; i = i + 1)
        {
            abc[i] = Convert.ToByte(namesArray[i].Replace(" ", ""), 16);
        }

here, abc is the desired byte array. 在这里,abc是所需的字节数组。 Now do what ever you want with abc. 现在,用abc做任何您想做的事情。

Your code is working. 您的代码正在运行。 Chesk this to observe different conversions and choose whatever you need: 检查一下以观察不同的转换并选择您需要的任何东西:

MessageBox.Show(11.ToString());
MessageBox.Show(11.ToString("x"));
MessageBox.Show(0x11.ToString());
MessageBox.Show(0x11.ToString("x"));
MessageBox.Show(BitConverter.ToString(new byte[] { 11, 0x11, 16 }));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM