# How to convert a 39-decimal characters into hex in c#

Hi experts
I've a string representation of a 39-characters long decimal integer.
I need to convert this into hex format in C#.
eg. 340282366920938463463374607431768211455 (39 characters)
-> ffffffffffffffffffffffffffffffff (32 characters)

By the same token, I need to be able to reverse the operation from hex to decimal.

I am using C# .NET 3.5 which hasn't got BigInteger.

###### Who is Participating?

Commented:
the code for doing this in strings is below but if you want to work in numbers try intX

https://github.com/devoyster/IntXLib

decimal string to hex string

``````        var s = "843370923007003347112437570992242323";
var result = new List<byte>();
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 )
}

var hex = "";
foreach ( byte b in result )
hex = "0123456789ABCDEF"[ b ] + hex;
``````

hex string to decimal string

``````static string HexToDecimal(string hex)
{
List<int> dec = new List<int> { 0 };   // decimal result

foreach (char c in hex)
{
int carry = Convert.ToInt32(c.ToString(), 16);
// initially holds decimal value of current hex digit;
// subsequently holds carry-over for multiplication

for (int i = 0; i < dec.Count; ++i)
{
int val = dec[i] * 16 + carry;
dec[i] = val % 10;
carry = val / 10;
}

while (carry > 0)
{
carry /= 10;
}
}

var chars = dec.Select(d => (char)('0' + d));
var cArr = chars.Reverse().ToArray();
return new string(cArr);
}
``````
0

PresidentCommented:
There is a BigInteger. It is "hidden" in the System.Numerics namespace, available after referencing the dll by the same name.

If you can get a string representation of your value, the following will do the job:

BigInteger bigInt = BigInteger.Parse ( "340282366920938463463374607431768211455" );
string bigHex = bigInt.ToString ("X");
bigInt = BigInteger.Parse ( bigHex, System.Globalization.NumberStyles.HexNumber );

Otherwise, give a look at the BigInteger Structure in the documentation, it is quite exhaustive.
0

Commented:
BigInteger is part of .net 4, OP is working with .net 3.5
0

Author Commented:
Thanks QuinnDex. That works like a charm, and is exactly what I need.

Thanks also to JamesBurger for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.