2

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.

1
  • Hex and binary are just convenient representations of the underlying voltage state of a transistor input. Applicability of bitwise operators is a completely independent concept. Commented May 28, 2012 at 6:48

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@Amait Is your input a string of 0 and 1 that you need to parse as a number?
@Amait Convert.ToInt32("1000110101", 2).ToString("x");
1

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,A-F, 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

Comments

1

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);

1 Comment

no homework dude, I'm employed. and the client requirements are vague so before I tell them its IMPOSSIBLE, i need to be sure and all you guys go back to deal with bits and bytes, so I am a bit relived. not what i'm looking for but thanks.
0

Probably BitArray is what you are looking for.

Comments

0

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.