简体   繁体   English

是否可以将字符串转换为二进制表示的字节数组

[英]Is it possible to convert a string to byte array in binary representation

I need to convert an integer which in the form of string to byte array in binary representation.我需要将字符串形式的整数转换为二进制表示的字节数组。 For example : I have a value "29" , then convert this value to binary equivalent 2-> 0010 and 9-> 1001 and store it in byte array where 0th index has 0010 and 1st index has 1001 .例如:我有一个值"29" ,然后将此值转换为二进制等效值2-> 0010 and 9-> 1001并将其存储在字节数组中,其中0th index has 0010 and 1st index has 1001 I have tried this but this gives me an array of 8 bytes.我试过这个,但这给了我一个 8 个字节的数组。

var val = "29".ToCharArray();
var a = Convert.ToString(Convert.ToInt32(Convert.ToString(val[0])), 2).PadLeft(4, '0');
var b = Convert.ToString(Convert.ToInt32(Convert.ToString(val[1])), 2).PadLeft(4, '0');

var c = a.ToList();
c.ForEach(x => sb.Append(Convert.ToString(x) + " "));
var f = sb.ToString().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var g = f.ToList();
byte[] buff = new byte[g.Count];
for (int z = 0; z < g.Count; z++)
{
    buff[z] = (byte)Convert.ToInt32(g[z]);
}

var h = b.ToList();
sb.Clear();
h.ForEach(x => sb.Append(Convert.ToString(x) + " "));
var i = sb.ToString().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var j = i.ToList();
byte[] buff2 = new byte[j.Count];
for (int k = 0; k < j.Count; k++)
{
    buff2[k] = (byte)Convert.ToInt32(j[k]);
}
byte[] buffer = buff.Concat(buff2).ToArray();

You can do this much easier:你可以更容易地做到这一点:

string s = "29";
var buffer = new byte[s.Length];
for (int i = 0; i < buffer.Length; i++) {
    buffer[i] = (byte)(s[i] - '0');
}

Explanation:解释:

  • We create a byte buffer with the same length as the input string since every character in the string is supposed to be a decimal digit.我们创建一个与输入字符串长度相同的字节缓冲区,因为字符串中的每个字符都应该是十进制数字。
  • In C#, a character is a numeric type.在 C# 中,字符是数字类型。 We subtract the character '0' from the character representing our digit to get its numeric value.我们从代表我们数字的字符中减去字符'0'以获得它的数值。 We get this digit by using the String indexer which allows us to access single characters in a string.我们通过使用String索引器获得这个数字,它允许我们访问字符串中的单个字符。
  • The result is an integer that we cast to byte we can then insert into the buffer.结果是一个整数,我们将其转换为byte ,然后我们可以将其插入缓冲区。

Console.WriteLine(buffer[0]) prints 2 because numbers are converted to a string in a decimal format for display. Console.WriteLine(buffer[0])打印2因为数字被转换为十进制格式的字符串以进行显示。 Everything the debugger, the console or a textbox displays is always a string the data has been converted to.调试器、控制台或文本框显示的所有内容始终是数据已转换为的字符串。 This conversion is called formatting.这种转换称为格式化。 Therefore, you do not see the result as binary.因此,您看不到二进制结果。 But believe me, it is stored in the bytes in the requested binary format.但相信我,它以请求的二进制格式存储在字节中。

You can use Convert.ToString and specify the desired numeric base as second parameter to see the result in binary.您可以使用Convert.ToString并指定所需的数字基数作为第二个参数以查看二进制结果。

foreach (byte b in buffer) {
    Console.WriteLine($"{b} --> {Convert.ToString(b, toBase: 2).PadLeft(4, '0')}");
}

If you want to store it in this visual binary format, then you must store it in a string array如果要以这种可视化二进制格式存储,则必须将其存储在字符串数组中

var stringBuffer = new string[s.Length];
for (int i = 0; i < stringBuffer.Length; i++) {
    stringBuffer[i] = Convert.ToString(s[i] - '0', toBase: 2).PadLeft(4, '0');
}

Note that everything is stored in a binary format with 0s and 1s in a computer, but you never see these 0s and 1s directly.请注意,所有内容都以二进制格式存储在计算机中,其中包含 0 和 1,但您永远不会直接看到这些 0 和 1。 What you see is always an image on your screen.您看到的始终是屏幕上的图像。 And this image was created from images of characters in a specific font.这个图像是根据特定字体的字符图像创建的。 And these characters result from converting some data into a string, ie, from formatting your data.这些字符是将某些数据转换为字符串(即格式化数据)而产生的。 The same data might look different on PCs using a different culture, but the underlying data is stored with the same pattern of 0s and 1s.相同的数据在使用不同文化的 PC 上可能看起来不同,但底层数据以相同的 0 和 1 模式存储。

The difference between storing the numeric value of the digit as byte and storing this digit as character (possibly being an element of a string) is that a different encoding is used.将数字的数值存储为字节与将此数字存储为字符(可能是字符串的元素)之间的区别在于使用了不同的编码。

  • The byte stores it as a binary number equivalent to the decimal number.该字节将其存储为与十进制数等效的二进制数。 Ie, 9 (decimal) becomes 00001001 (binary).即, 9 (十进制)变为00001001 (二进制)。
  • The string or character stores the digit using the UTF-16 character table in .NET.字符串或字符使用 .NET 中的 UTF-16 字符表存储数字。 This table is equivalent to the ASCII table for Latin letters without accents or umlauts, for digits and for the most common punctuation, except that it uses 16 bits per character instead of 7 bits (expanded to 8 when stored as byte).该表等同于不带重音符号或变音符号的拉丁字母、数字和最常见标点符号的ASCII 表,不同之处在于它使用每个字符 16 位而不是 7 位(当存储为字节时扩展为 8)。 According to this table, the character '9' is represented by the binary 00111001 (decimal 57).根据此表,字符'9'由二进制00111001 (十进制 57)表示。

The string "1001" is stored in UTF-16 as字符串"1001"以 UTF-16 格式存储

00000000 00110001  00000000 00110000  00000000 00110000  00000000 00110001

where 0 is encoded as 00000000 00110000 (decimal 48) and 1 is encoded as 00000000 00110001 (decimal 49).其中0编码为00000000 00110000 (十进制 48), 1编码为00000000 00110001 (十进制 49)。 Also, additional data is stored for a string, as its length, a NUL character terminator and data related to its class nature.此外,还为字符串存储附加数据,如其长度、NUL 字符终止符和与其类性质相关的数据。


Alternative ways to store the result would be to use an array of the BitArray Class or to use an array of array of bytes where each byte in the inner array would store one bit only, ie, be either 0 or 1.存储结果的替代方法是使用BitArray 类的数组或使用字节数组的数组,其中内部数组中的每个字节仅存储一位,即 0 或 1。

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

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