Hset Hset Aung
Hset Hset Aung

Reputation: 259

Adding hexa values in C#

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.

Upvotes: 6

Views: 27096

Answers (6)

Derek Tremblay
Derek Tremblay

Reputation: 197

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

Upvotes: 2

Kobi
Kobi

Reputation: 138027

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)

Upvotes: 4

Arto Viitanen
Arto Viitanen

Reputation: 188

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

Upvotes: 0

Javed Akram
Javed Akram

Reputation: 15344

  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

Upvotes: 2

Kobi
Kobi

Reputation: 138027

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

Upvotes: 16

user541686
user541686

Reputation: 210515

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

Upvotes: 1

Related Questions