简体   繁体   中英

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.

  1. is there a HEX namespace ?
  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 i = int.Parse( "FFFFFF", System.Globalization.NumberStyles.HexNumber );

Converting int to hex string is done this way:

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

Defining a number in C# can be done using hex notation:

int i = 0xFFFFFF;

BitArray can also be used to store bytes. It does have an AND,OR,XOR and NOT function.

Hexadecimal (base-16), decimal (base-10), octal (base-8) etc mean nothing to the computer whatsoever. 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). 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.

No, there is no "HEX" namespace, because it would be redundant. 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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