简体   繁体   English

C#处理十六进制值和二进制转换

[英]C# dealing with HEX values, and binary conversion

is there a way to deal with HEX values (XORing values and shifting bytes, for example) I need the whole program to deal with HEX only, and if the input is in Binary to convert it into HEX. 有没有一种方法来处理HEX值(例如XORing值和移位字节),我需要整个程序仅处理HEX,并且如果输入是二进制的,则将其转换为HEX。

  1. is there a HEX namespace ? 有HEX命名空间吗?
  2. is there a way to represent a HEX other than using strings? 除了使用字符串外,还有什么方法可以代表十六进制?

as I need to count the number of zeros and ones (for some tests I'm preforming), without the need to convert it into binary. 因为我需要计算零和一的数量(对于某些测试,我正在执行), 无需将其转换为二进制。 so It doesn't matter how it looks as long there is a way to deal with it as HEX. 因此,只要看起来像十六进制一样,它的外观就无关紧要。

I need to know the possibility and my options. 我需要知道可能性和我的选择。

Hex, binary and decimal are different faces of the same objects, namely integers. 十六进制,二进制和十进制是同一对象的不同面,即整数。

Parsing hex to int is done this way: 通过以下方式将十六进制解析为int:

int i = int.Parse( "FFFFFF", System.Globalization.NumberStyles.HexNumber );

Converting int to hex string is done this way: 通过以下方式将int转换为十六进制字符串:

string s = i.ToString("x");

Defining a number in C# can be done using hex notation: 可以使用十六进制表示法在C#中定义数字:

int i = 0xFFFFFF;

BitArray can also be used to store bytes. BitArray也可以用于存储字节。 It does have an AND,OR,XOR and NOT function. 它确实具有AND,OR,XOR和NOT函数。

Hexadecimal (base-16), decimal (base-10), octal (base-8) etc mean nothing to the computer whatsoever. 十六进制(base-16),十进制(base-10),八进制(base-8)等对计算机毫无意义 Wanting to get the computer to work "with HEX" is ... meaningless. 想要让计算机“与十六进制一起工作”是没有意义的。

The most appropriate data format for working with raw data is in primitives such as byte (perhaps in a byte[] ), or (arguably better) in larger composites such as int / long (which can allow for many performance savings, by allowing you to work with multiple values at the same time). 处理原始数据的最合适的数据格式是诸如byte原语(也许在byte[] ),或者诸如int / long类的较大组合中(可能更好)(通过允许您节省许多性能)同时使用多个值)。 Of course, if you are determined to work with string (which I do not recommend), for things such as "binary bit count" you could handle that at the character level simply by pre-computing the number of bits set in each of 0-9,AF, and just doing a lookup against that value. 当然,如果您决定使用string (我不建议这样做),那么对于“二进制位数”这样的事情,您可以通过预先计算每个0中设置的位数来在字符级别上处理。 -9,AF,然后针对该值进行查找。

No, there is no "HEX" namespace, because it would be redundant. 不,没有“ HEX”名称空间,因为它将是多余的。 Hex is just a number ; 十六进制只是一个数字 ; the number is the same number no matter how us weak-minded fleshy humans are thinking about it. 无论我们弱智的肉质人如何思考,这个数字都是相同的数字。 Likewise you could use similar pre-generated lookups for changing to/from numbers, or on-the-fly via: 同样,您可以使用类似的预先生成的查找来更改数字,或通过以下方式即时进行查找:

int i = Convert.ToInt32("f3", 16); // hex to Int32
string hex = i.ToString("x2"); // Int32 to hex

You haven't really been clear on what your input and output requirements are, and this reeks of homework, but: 您还不清楚输入和输出要求是什么,以及这种家庭作业的问题,但是:

        var x = 0x1111L;

        /// number of bits that are on
        var ones = 0;

        /// number of bits that are off
        var zeros = 0;

        var bitLength = Marshal.SizeOf(x) * 8;

        while (x > 0)
        {
            if ((x & 1) > 0)
            {
                ones++;
            }
            x >>= 1;
        }

        zeros = bitLength - ones;

        Console.WriteLine("{0} bits are on, {1} are off", ones, zeros);

您可能正在寻找BitArray

Here is some "manual" approach 这是一些“手动”方法

static void ConvertBinToHex(string hexadecimal)
{
    if (hexadecimal.Length == 0)
        return;

    string binary = string.Empty;

    for (int i = 0; i < hexadecimal.Length; i++)
    {
        switch (hexadecimal[i])
        {
            case '0': binary += "0000"; break;
            case '1': binary += "0001"; break;
            case '2': binary += "0010"; break;
            case '3': binary += "0011"; break;
            case '4': binary += "0100"; break;
            case '5': binary += "0101"; break;
            case '6': binary += "0110"; break;
            case '7': binary += "0111"; break;
            case '8': binary += "1000"; break;
            case '9': binary += "1001"; break;
            case 'A': binary += "1010"; break;
            case 'B': binary += "1011"; break;
            case 'C': binary += "1100"; break;
            case 'D': binary += "1101"; break;
            case 'E': binary += "1110"; break;
            case 'F': binary += "1111"; break;

            default:
                break;
        }
    }

    //remove leading zero's
    binary = binary.Trim('0');
    Console.WriteLine(binary);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM