字节[]带有ASCII值的字符串到INT
因此,有人将int值转换为字符串,然后将其转换为ASCII值,然后以不一致的长度1-4字节将其转换为Byte []。
例如100-> “ 100” - > {49,48,48}。
现在我需要那个int值,我这样做了:
{49、48、48} - > '1' +'0' +'0' - > “ 100” - > 100
switch (header[25].Count)
{
case 1:
hex = "" + (char)header[25][0];
amountOfData = Convert.ToInt32(hex, 16);
break;
case 2:
hex = "" + (char)header[25][0] + (char)header[25][1];
amountOfData = Convert.ToInt32(hex, 16);
break;
case 3:
hex = "" + (char)header[25][0] + (char)header[25][1] + (char)header[25][2];
amountOfData = Convert.ToInt32(hex, 16);
break;
case 4:
hex = "" + (char)header[25][0] + (char)header[25][1] + (char)header[25][2] + (char)header[25][3];
amountOfData = Convert.ToInt32(hex, 16); ;
break;
default:
break;
}
但是也许有更好的解决方案...
编辑:对不起,没有提及这一点,但是标题是list< list< byte>>
So someone took int value, converted it to string then converted it to ASCII values and then finally to byte[] with inconsistent length 1 - 4 bytes.
e.g. 100 -> "100" -> { 49, 48, 48 }.
Now I need that int value and I did it like this:
{ 49, 48, 48 } -> '1' + '0' + '0' -> "100" -> 100
switch (header[25].Count)
{
case 1:
hex = "" + (char)header[25][0];
amountOfData = Convert.ToInt32(hex, 16);
break;
case 2:
hex = "" + (char)header[25][0] + (char)header[25][1];
amountOfData = Convert.ToInt32(hex, 16);
break;
case 3:
hex = "" + (char)header[25][0] + (char)header[25][1] + (char)header[25][2];
amountOfData = Convert.ToInt32(hex, 16);
break;
case 4:
hex = "" + (char)header[25][0] + (char)header[25][1] + (char)header[25][2] + (char)header[25][3];
amountOfData = Convert.ToInt32(hex, 16); ;
break;
default:
break;
}
but maybe there is better solution...
EDIT: sorry for not mentioning that, but header is List<List<byte>>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用编码/getString方法将不同编码(例如ASCII)的字节转换为.NET字符串:
You can use the Encoding/GetString method to convert bytes of different encodings (e.g. ASCII in your case) to a .NET string:
您可以使用库函数从字节式数据解析为原语。您是在谈论ASCII,这意味着
utf8parser
对我们有效(所有ASCII也是有效的UTF8,尽管反向显然不是正确的);通常,我们希望标题[25]
是byte []
,一个段或其他一些原始二进制源,但是:最终,类似:如果
标题[25]
是不太方便的(例如list&lt; byte&gt;
- 我注意到在您的示例中,您的header [25]
具有.count
不是.length
,这表明它不是byte []
),然后您始终可以> stackalloc
本地缓冲区并复制数据 out ,或者您可以在内访问 带有collection> collectionmarshal.asspan&lt; t&gt;(list&lt; t&gt; t&gt; )
,从基础数据返回span&lt; t&gt;
:作为可运行的示例,仅显示API:
You can use library functions to parse from byte-like data to primitives; you're talking about ASCII, which means that
Utf8Parser
will work fine for us (all ASCII is also valid UTF8, although the reverse is obviously not true); normally, we would expect thatheader[25]
is abyte[]
, a segment there-of, or some other raw binary source, but: ultimately, something like:If
header[25]
is something less convenient (like aList<byte>
- I notice that in your example, yourheader[25]
has a.Count
not a.Length
, which suggests it isn't abyte[]
), then you can always eitherstackalloc
a local buffer and copy the data out, or you can peek inside the list withCollectionMarshal.AsSpan<T>(List<T>)
, which returns aSpan<T>
from the underlying data:As a runnable example that just shows the API: