Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a gi-normous integer (in string format) to hex format? (C#)

Given a potentially huge integer value (in C# string format), I want to be able to generate its hex equivalent. Normal methods don't apply here as we are talking arbitrarily large numbers, 50 digits or more. The techniques I've seen which use a technique like this:

// Store integer 182
int decValue = 182;
// Convert integer 182 as a hex in a string variable
string hexValue = decValue.ToString("X");
// Convert the hex string back to the number
int decAgain = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);

won't work because the integer to convert is too large.

For example I need to be able to convert a string like this:

843370923007003347112437570992242323

to its hex equivalent.

these don't work:

C# convert integer to hex and back again How to convert numbers between hexadecimal and decimal in C#?

like image 294
eviljack Avatar asked Apr 16 '10 12:04

eviljack


People also ask

Which method converts an integer to a hexadecimal string?

toHexString() method in Java converts Integer to hex string. Let's say the following are our integer values. int val1 = 5; int val2 = 7; int val3 = 13; Convert the above int values to hex string.

What is hex string format?

The format for coding a hexadecimal string mask is: X'yy...yy' The value yy represents any pair of hexadecimal digits that constitute a byte (8 bits). Each bit must be 1 (test bit) or 0 (ignore bit).

What is hexadecimal in coding?

Hexadecimal is a numbering system with base 16. It can be used to represent large numbers with fewer digits. In this system there are 16 symbols or possible digit values from 0 to 9, followed by six alphabetic characters -- A, B, C, D, E and F.

How to understand hexadecimal?

Hexadecimal is a base-16 number system. That means there are 16 possible digits used to represent numbers. 10 of the numerical values you're probably used to seeing in decimal numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9; those values still represent the same value you're used to.


2 Answers

Oh, that's easy:

        var s = "843370923007003347112437570992242323";
        var result = new List<byte>();
        result.Add( 0 );
        foreach ( char c in s )
        {
            int val = (int)( c - '0' );
            for ( int i = 0 ; i < result.Count ; i++ )
            {
                int digit = result[i] * 10 + val;
                result[i] = (byte)( digit & 0x0F );
                val = digit >> 4;
            }
            if ( val != 0 )
                result.Add( (byte)val );
        }

        var hex = "";
        foreach ( byte b in result )
            hex = "0123456789ABCDEF"[ b ] + hex;
like image 177
Dan Byström Avatar answered Oct 04 '22 19:10

Dan Byström


Use a BigInteger to store the integer, and than use the .ToString("X") on that object.

Example:

var number = BigInteger.Parse("843370923007003347112437570992242323");
string hexValue = number.ToString("X");

This is however limited to .NET 4 and later. But Jens A. pointed to a BigInteger class on codeproject that class contains a method called ToHexString so that would work for a < .NET 4 scenario.

like image 36
Davy Landman Avatar answered Oct 04 '22 19:10

Davy Landman