1

I want to convert Hexadecimal into a UInt. The problem is the following:

when I try this:

uint value = Convert.ToUInt32((hex), 16);

and the hex is for example 12 bytes size, all works fine, but when I try to convert a hex with 32 bytes size I have this error:

value too large or too small for int32

Then I try this :

ulong = Convert.ToUInt64((hex), 16);

and I get this error.

value too large or too small for int64

Someone Knows what I am doing wrong?

IrApp
  • 1,823
  • 5
  • 24
  • 42
  • 1
    It is not clear what you are asking. Your code samples do not compile. Do you want to **parse** a string containging some number in hex format? show as an example... – DrKoch Jan 23 '15 at 14:32
  • can you please give us an example of string you're using? – Francesca Aldrovandi Jan 23 '15 at 14:37
  • You are right sorry, the string is a hexadecimal number. For example : I have the hexadecimal number D34128 and I want 13844776 decimal number, but when I have a hexadecimal 32 bytes size, I get the error. – IrApp Jan 23 '15 at 14:45
  • I tried: Convert.ToUInt32("D34128", 16); works. Could you give us concrete example when you get this error? – roxik0 Jan 23 '15 at 15:04
  • Try this hexadecimal : 234567891123456789223456789. If I try this: Convert.ToUInt64("1234567891123456789223456789", 16); and this Convert.ToUInt32("1234567891123456789223456789", 16) I get a error. – IrApp Jan 23 '15 at 15:17
  • Value you try to convert is to big see my answer below – roxik0 Jan 23 '15 at 15:29

2 Answers2

0

Sowi error is correct: https://msdn.microsoft.com/pl-pl/library/system.uint64.maxvalue(v=vs.110).aspx 0xFFFFFFFFFFFFFFFF is max Uint64 value. You tried to input: 0x1234567891123456789223456789 what is much bigger value.

roxik0
  • 97
  • 6
0

Finally I cut the hexadecimal number like this:

Hex = 1234567891123456789223456789

ulong result= Convert.ToUInt64(12345678911234, 16);
result += Convert.ToUInt64(56789223456789, 16);

That resolve my problem. Thanks for the replies, made me think!!!

IrApp
  • 1,823
  • 5
  • 24
  • 42