HEX 到 bit[ ] 数组(也称为 bool[ ])

发布于 2024-11-24 03:05:00 字数 2514 浏览 1 评论 0原文

我对 C# 有点陌生,我正在寻找关于两件事的一些想法。我已经广泛寻找答案,但还没有找到我的确切问题的答案。

  1. 我在 for 循环中有一个字节数组(称为 BA),它不断地覆盖自身,并且我无法将其作为整个数组打印。有没有办法将其导出到 for 循环之外(可能使用不同的名称),以便我稍后可以在程序中使用它?我只想要这样的东西:

    byte[] BA2 = {3 187,3 203,111 32, ...etc}; //(以逗号分隔的 2 个字节组)。
    

    摘自原文

    string hexValues = "03 BB,03 CB,6F 20,57 6F,72 6C,64 21";
    

    (并且还以位(布尔值)表示此信息,因此 {00000011 10111011,00000011 11001011,...etc})

  2. 我必须做的第二件事是移动这两个字节乘以 4 并应用与 FFF0 的与门(这与将第一个字节 * 1 和第二个字节乘以相同) 0xF0)。然后将其放入 ushort[ ] (无符号短数组)中,该数组以二进制格式保存转换后的字节,然后从那里将其转换回十六进制。

我知道这可能不清楚(我的代码有点混乱),而且相当复杂。但我希望你们中的一些 c# 大师可以帮我一把。

到目前为止,这是我的代码,我已将不起作用的部分添加到注释中,以便代码运行。但我迫切需要修复它们。

class Program
{
    static void Main(string[] args)
    {

        string hexValues = "03 BB,03 CB,6F 20,57 6F,72 6C,64 21";
        string[] hex2byte = hexValues.Split(',');

        for (int j = 0; j < 6; j++)
        {
            Console.WriteLine("\n2 byte String is: "+ hex2byte[j]);
            string[] hex1byte = hex2byte[j].Split(' ');

            for (int k = 0; k < 2; k++)
            {
                Console.WriteLine("byte " + hex1byte[k]);
                byte[] BA = StringToByteArray((hex1byte[k]));

                //bool[] AA = BitConverter.ToBoolean(BA);           // I'm essentially stuck here. I need somehting which actually works.

                //for (int i2 = 0; i2 < 2; i2++);                   // This is my attemp to perform de shift and AND.
                //{
                //    ushort[] X = new ushort[1];
                //    X[0] = (ushort)((ushort)(BA[0] << 4) + (ushort)((BA[1] & 0xF0) >> 4));        // They have to be in this order: ((1stByte & 0xFF) << 4) + ((2byte & 0xF0) >> 4); first to the left then the right.

                //}

                Console.WriteLine("Converted " + BA[0]);
            }
        }
        //Console.WriteLine(BA[4]);               // it says: Does not exist in current context. Can it only b accesed in the for loop?
        Console.ReadKey();

    }   // Main method finishes.


    // Define StringToByteArray method.
    public static byte[] StringToByteArray(String hex)
    {
        int NumberChars = hex.Length;
        byte[] bytes = new byte[NumberChars / 2];
        for (int i = 0; i < NumberChars; i += 2)
        {
            bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
        }
        return bytes;
    }
}

I'm kindda new to c# and i was looking for some ideas on 2 thing. I have looked far and wide for answers but haven't found an answer to my exact problem.

  1. I have a byte array (called BA) within a for loop which keeps over-writting itself and there is no way for my to be able print it as a whole array. Is there a way to export it outside the for loop (maybe with a different name) so i can use it later on in the program? i just want something like this:

    byte[] BA2 = {3 187,3 203,111 32, ...etc}; //(groups of 2bytes separated by commas).
    

    From the original

    string hexValues = "03 BB,03 CB,6F 20,57 6F,72 6C,64 21";
    

    (and also to represent this information in bits (boolean) so {00000011 10111011,00000011 11001011, ...etc})

  2. The second thing i must do is to shift these two bytes by 4 and apply and AND gate with FFF0 (which is the same as multiplying the first byte * 1, and the second by 0xF0). Then put this in a ushort[ ] (unsigned short array) which holds the transformed bytes in binary format and then from there convert it back to HEX.

I understand that this might be unclear (my code is kind of messy), and pretty complex. but i was hoping some of you c# guru's could lend me hand.

Here's my code so far, i have put in comments the bits that don't work so the code runs. but i desperatly need to fix them.

class Program
{
    static void Main(string[] args)
    {

        string hexValues = "03 BB,03 CB,6F 20,57 6F,72 6C,64 21";
        string[] hex2byte = hexValues.Split(',');

        for (int j = 0; j < 6; j++)
        {
            Console.WriteLine("\n2 byte String is: "+ hex2byte[j]);
            string[] hex1byte = hex2byte[j].Split(' ');

            for (int k = 0; k < 2; k++)
            {
                Console.WriteLine("byte " + hex1byte[k]);
                byte[] BA = StringToByteArray((hex1byte[k]));

                //bool[] AA = BitConverter.ToBoolean(BA);           // I'm essentially stuck here. I need somehting which actually works.

                //for (int i2 = 0; i2 < 2; i2++);                   // This is my attemp to perform de shift and AND.
                //{
                //    ushort[] X = new ushort[1];
                //    X[0] = (ushort)((ushort)(BA[0] << 4) + (ushort)((BA[1] & 0xF0) >> 4));        // They have to be in this order: ((1stByte & 0xFF) << 4) + ((2byte & 0xF0) >> 4); first to the left then the right.

                //}

                Console.WriteLine("Converted " + BA[0]);
            }
        }
        //Console.WriteLine(BA[4]);               // it says: Does not exist in current context. Can it only b accesed in the for loop?
        Console.ReadKey();

    }   // Main method finishes.


    // Define StringToByteArray method.
    public static byte[] StringToByteArray(String hex)
    {
        int NumberChars = hex.Length;
        byte[] bytes = new byte[NumberChars / 2];
        for (int i = 0; i < NumberChars; i += 2)
        {
            bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
        }
        return bytes;
    }
}

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

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

发布评论

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

评论(1

萌梦深 2024-12-01 03:05:00

这是您要找的吗?

string[] hexValues = new string[] { "03BB", "03CB", "6F20", "576F", "726C", "6421" };
ushort result = 0;
for (int i = 0; i < hexValues.Length; i++)
{
    Console.WriteLine("2 byte String is {0}", hexValues[i]);
    result = ushort.Parse(hexValues[i], NumberStyles.AllowHexSpecifier);
    Console.WriteLine("converted: {0}", result.ToString());
    Console.WriteLine("converted: {0}", result.ToString("x")); // "x" format in ToString -> very useful for creating hex strings.
}

对于您的转换,您可以使用 <<和>>运算符,和 |和&用于按位运算。

Is this what you are looking for?

string[] hexValues = new string[] { "03BB", "03CB", "6F20", "576F", "726C", "6421" };
ushort result = 0;
for (int i = 0; i < hexValues.Length; i++)
{
    Console.WriteLine("2 byte String is {0}", hexValues[i]);
    result = ushort.Parse(hexValues[i], NumberStyles.AllowHexSpecifier);
    Console.WriteLine("converted: {0}", result.ToString());
    Console.WriteLine("converted: {0}", result.ToString("x")); // "x" format in ToString -> very useful for creating hex strings.
}

For your shifting you can use the << and >> operators, and | and & for bitwise operations.

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