如何在 C# 中将 int[,] 转换为 byte[]

发布于 2024-09-10 20:41:48 字数 138 浏览 5 评论 0原文

如何在 C# 中将 int[,] 转换为 byte[]? 有些代码将受到赞赏

编辑:

我需要一个函数来执行以下操作:

byte[] FuncName (int[,] Input)

How to convert an int[,] to byte[] in C#?
Some code will be appreciated

EDIT:

I need a function to perform the following:

byte[] FuncName (int[,] Input)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

提笔书几行 2024-09-17 20:41:48

由于您的问题中的细节很少,我只能猜测您要做什么...假设您想将 2D 整数数组“展平”为 1D 字节数组,您可以这样做:

byte[] Flatten(int[,] input)
{
    return input.Cast<int>().Select(i => (byte)i).ToArray();
}

请注意对 Cast 的调用:这是因为多维数组实现了 IEnumerable,但没有实现 IEnumerable

Since there is very little detail in your question, I can only guess what you're trying to do... Assuming you want to "flatten" a 2D array of ints into a 1D array of bytes, you can do something like that :

byte[] Flatten(int[,] input)
{
    return input.Cast<int>().Select(i => (byte)i).ToArray();
}

Note the call to Cast : that's because multidimensional arrays implement IEnumerable but not IEnumerable<T>

傲性难收 2024-09-17 20:41:48

您似乎写错了类型,但您可能正在寻找以下内容:

byte[] FuncName (int[,] input)
{
    byte[] byteArray = new byte[input.Length];

    int idx = 0;
    foreach (int v in input) {
        byteArray[idx++] = (byte)v;
    }

    return byteArray;
}

It seem that you are writing the types wrong, but here is what you might be looking for:

byte[] FuncName (int[,] input)
{
    byte[] byteArray = new byte[input.Length];

    int idx = 0;
    foreach (int v in input) {
        byteArray[idx++] = (byte)v;
    }

    return byteArray;
}
浅沫记忆 2024-09-17 20:41:48

这是假设您正在尝试序列化的一个实现;不过,不知道这是否是您想要的;它为尺寸添加前缀,然后每个单元格使用基本编码:

public byte[] Encode(int[,] input)
{
    int d0 = input.GetLength(0), d1 = input.GetLength(1);
    byte[] raw = new byte[((d0 * d1) + 2) * 4];
    Buffer.BlockCopy(BitConverter.GetBytes(d0), 0, raw, 0, 4);
    Buffer.BlockCopy(BitConverter.GetBytes(d1), 0, raw, 4, 4);
    int offset = 8;
    for(int i0 = 0 ; i0 < d0 ; i0++)
        for (int i1 = 0; i1 < d1; i1++)
        {
            Buffer.BlockCopy(BitConverter.GetBytes(input[i0,i1]), 0,
                  raw, offset, 4);
            offset += 4;
        }
    return raw;
}

Here's an implementation that assumes you are attempting serialization; no idea if this is what you want, though; it prefixes the dimensions, then each cell using basic encoding:

public byte[] Encode(int[,] input)
{
    int d0 = input.GetLength(0), d1 = input.GetLength(1);
    byte[] raw = new byte[((d0 * d1) + 2) * 4];
    Buffer.BlockCopy(BitConverter.GetBytes(d0), 0, raw, 0, 4);
    Buffer.BlockCopy(BitConverter.GetBytes(d1), 0, raw, 4, 4);
    int offset = 8;
    for(int i0 = 0 ; i0 < d0 ; i0++)
        for (int i1 = 0; i1 < d1; i1++)
        {
            Buffer.BlockCopy(BitConverter.GetBytes(input[i0,i1]), 0,
                  raw, offset, 4);
            offset += 4;
        }
    return raw;
}
甜嗑 2024-09-17 20:41:48

BitConverter 将基本类型转换为字节数组:

byte[] myByteArray = System.BitConverter.GetBytes(myInt);

您似乎希望将整数的二维数组转换为字节。将 BitConverter 与必要的循环结构(例如 foreach)以及您想要组合数组维度的任何逻辑相结合。

The BitConverter converts primitive types to byte arrays:

byte[] myByteArray = System.BitConverter.GetBytes(myInt);

You appear to want a 2 dimensional array of ints to be converted to bytes. Combine the BitConverter with the requisite loop construct (e.g foreach) and whatever logic you want to combine the array dimensions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文