6

In my system,i need to add 2 Hexa values.So, how can i add hexa values in C#? And i also want to know the max length of Hexa values and which Instance hold these values.

6 Answers 6

16

C# supports hexadecimal literals:

int i = 0xff;

However, they have the same numeric values as decimal literals - you are limited by the type you use. There isn't a special Hexa type.

If you have an integer and want to display is in hexadecimal base, you can use the x format (example):

int i = 255;
Console.Write(i.ToString("x")); // ff
Console.Write(i); // 255

Note that writing i = 0xff or i = 255 is exactly the same - the value is resolved by the compiler, and you get the same compiled code.

Lastly, if you have strings with hexadecimal numbers, you can convert them to integers, sum them, and reformat them (example):

string hexValue = "af";
int i = Convert.ToInt32(hexValue, 16); //175
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, thanks for your answer. But my Hexa value contains 64 characters so which instance can hold this when convert to some type?
@Hset Hset Aung - You should have mentioned you have such huge values :). I've added another answer.
4

For 64 character numbers, you need to use the BigInteger type of .Net 4, the normal types are too small:

BigInteger bi1 = BigInteger.Parse("123456789012345678901234567890123456789012345678901234567890abc5", NumberStyles.HexNumber);
BigInteger bi2 = BigInteger.Parse("123456789012345678901234567890123456789012345678901234567890abc1", NumberStyles.HexNumber);
BigInteger sum = BigInteger.Add(bi1, bi2);
Console.WriteLine("{0:x}", sum); //or sum.ToString("x")

(remember adding a reference to System.Numerics)

3 Comments

But , i use .Net 3.5 version. I am sorry to ask again, how can i solve in .Net version 3.5
@Hset Hset Aung - In that case you need a library. Here are a few options: stackoverflow.com/questions/176775/big-integers-in-c
Thanks a lot for your answers.Ok , i check at that link now.
2
  int a = 0x2;
  int b = 0x5f;
  int value = a + b; //adding hex values

  string maxHex = int.MaxValue.ToString("x"); //maximum range of hex value as int

Comments

2

You can use this code for sum your result. You can see more on my ByteConverters class

    public static long HexLiteralToLong(string hex)
    {
        if (string.IsNullOrEmpty(hex)) throw new ArgumentException("hex");

        int i = hex.Length > 1 && hex[0] == '0' && (hex[1] == 'x' || hex[1] == 'X') ? 2 : 0;
        long value = 0;

        while (i < hex.Length)
        {
            int x = hex[i++];

            if
                (x >= '0' && x <= '9') x = x - '0';
            else if
                (x >= 'A' && x <= 'F') x = (x - 'A') + 10;
            else if
                (x >= 'a' && x <= 'f') x = (x - 'a') + 10;
            else
                throw new ArgumentOutOfRangeException("hex");

            value = 16 * value + x;
        }

        return value;
    }

Usage :

var HexSum = HexLiteralToLong("FF") + HexLiteralToLong("FF");

Result :

510

Comments

1

Hexadecimal values are just plain old integers (after all, the number 10 in base ten and the number A in hexadecimal are the same thing, just viewed differently); if you need to convert one to a hex string, use: value.ToString("X"), where value is an integral type (like int).

Comments

0

On your last question: you can use hexadecimal constants in integer types (byte, short, int and long). In C# long is 64 bit, so longest hexadecimal constant is 0x1234567812345678 (or any other with 16 hexadecimal digits).

2 Comments

Well, not really any value with 16 digits, they aren't all equal, and half of them are too big - the maximal value for long is 0x7fffffffffffffff: ideone.com/tdpIX , which is also visible in the specifications: msdn.microsoft.com/en-us/library/system.int64.maxvalue.aspx
Oh, I get it. You're talking about maximal length, not value.

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.