[英]Turn string with numbers to byte array - C#
I created string that contains numbers by calling string.Join on a byte array: 我通过调用string.Join创建了一个包含数字的字符串,并加入了字节数组:
string str = string.Join(", ", arr);
(arr is a byte array). (arr是一个字节数组)。
How can I turn the string back to a byte array? 如何将字符串转换回字节数组?
You can use String.Split
and then Byte.Parse
to parse the string, eg : 您可以使用
String.Split
然后Byte.Parse
解析字符串,如:
var newArray = str.Split(',').Select(Byte.Parse).ToArray();
Byte.Parse
ignores whitespace so there's no need to trim Byte.Parse
忽略空格,因此无需修剪
If you create the array like this : 如果您这样创建数组:
var str = String.Join(", ", new byte[]{0xFF,0x05,0x56});
The new array produced by splitting: 通过拆分产生的新数组:
var newArray = Split(',').Select(Byte.Parse).ToArray();
Will contain the values 255, 5 and 86. 将包含值255、5和86。
Assuming your string looks like "1,2,3,4" 假设您的字符串看起来像“ 1,2,3,4”
var numArray = str.Split(',').Select(s => Byte.Parse(s)).ToArray();
Runnable: 可运行的:
https://rextester.com/XOMQ99840 https://rextester.com/XOMQ99840
Not sure what the down vote is for. 不知道什么是否决票。 Shrug.
耸耸肩。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.