字节[]带有ASCII值的字符串到INT

发布于 2025-01-27 05:41:03 字数 1320 浏览 1 评论 0原文

因此,有人将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我们的影子 2025-02-03 05:41:03

您可以使用编码/getString方法将不同编码(例如ASCII)的字节转换为.NET字符串:

var input = new byte[] { 49, 48, 48 };
var str = Encoding.ASCII.GetString(input);
var result = int.Parse(str, NumberStyles.None, CultureInfo.InvariantCulture);

You can use the Encoding/GetString method to convert bytes of different encodings (e.g. ASCII in your case) to a .NET string:

var input = new byte[] { 49, 48, 48 };
var str = Encoding.ASCII.GetString(input);
var result = int.Parse(str, NumberStyles.None, CultureInfo.InvariantCulture);
能否归途做我良人 2025-02-03 05:41:03

您可以使用库函数从字节式数据解析为原语。您是在谈论ASCII,这意味着utf8parser对我们有效(所有ASCII也是有效的UTF8,尽管反向显然不是正确的);通常,我们希望标题[25]byte [],一个段或其他一些原始二进制源,但是:最终,类似:

var span = new ReadOnlySpan<byte>(header[25], 0, header[25].Count);
if (!Utf8Parser.TryParse(span, out int amountOfData, out _))
    ThrowSomeError(); // not an integer

如果标题[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;

var span = CollectionMarshal.AsSpan(header[25]);
if (!Utf8Parser.TryParse(span, out int amountOfData, out _))
    ThrowSomeError(); // not an integer

作为可运行的示例,仅显示API:

using System;
using System.Buffers.Text;

Span<byte> span = stackalloc byte[]  { 49, 48, 48 };
if (!Utf8Parser.TryParse(span, out int amountOfData, out _))
    throw new FormatException();
Console.WriteLine(amountOfData); // 100

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 that header[25] is a byte[], a segment there-of, or some other raw binary source, but: ultimately, something like:

var span = new ReadOnlySpan<byte>(header[25], 0, header[25].Count);
if (!Utf8Parser.TryParse(span, out int amountOfData, out _))
    ThrowSomeError(); // not an integer

If header[25] is something less convenient (like a List<byte> - I notice that in your example, your header[25] has a .Count not a .Length, which suggests it isn't a byte[]), then you can always either stackalloc a local buffer and copy the data out, or you can peek inside the list with CollectionMarshal.AsSpan<T>(List<T>), which returns a Span<T> from the underlying data:

var span = CollectionMarshal.AsSpan(header[25]);
if (!Utf8Parser.TryParse(span, out int amountOfData, out _))
    ThrowSomeError(); // not an integer

As a runnable example that just shows the API:

using System;
using System.Buffers.Text;

Span<byte> span = stackalloc byte[]  { 49, 48, 48 };
if (!Utf8Parser.TryParse(span, out int amountOfData, out _))
    throw new FormatException();
Console.WriteLine(amountOfData); // 100
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文