0

I have the need to convert a hex string into an integer using C#. I've tried all of the suggestions on SO including this one and many others. All of them throw the same or roughly the same... error. Value was either too large or too small for an Int32 (same for Int64).

I can get the desired result in SQL Server 2008 with the following code:

select convert(int, 0x1B1D3E1B22176145272C1631282D221D30)

However THIS C# code

Int64.Parse("1B1D3E1B22176145272C1631282D221D30", NumberStyles.HexNumber)
Int32.Parse("1B1D3E1B22176145272C1631282D221D30", NumberStyles.HexNumber)

...yields the errors described above. Thoughts?? Solutions?

UPDATE: The SQL Code above yields the following integer.. 555949360. With every record I can find, the sql conversion yields a unique Integer. So the new question (I guess) is.. how to replicate the results of the SQL Convert function on this hex data?

Community
  • 1
  • 1
Jay Stevens
  • 5,425
  • 8
  • 42
  • 67
  • 2
    Lets see, each digit in hex reprsents 4 bits. There are 34 digits * 4 bits = 136 bits. Not is not going to fit into a 32bit ot 64bit integer. – user957902 Dec 13 '11 at 18:05
  • by my count, that's a 134 bit number. How is that supposed to fit into a 64 bit integer? You need to use a class/struct made for handling very large integers. – hatchet - done with SOverflow Dec 13 '11 at 18:06
  • OK. Bottom line is that I want to REPLICATE what SQL is doing with that #.. because it is yielding what looks and acts very much like a unique integer. – Jay Stevens Dec 13 '11 at 18:16

4 Answers4

1

Your number is too big. It's about 9.2264939914744E+39 when Int64.MaxValue is 9,223,372,036,854,775,807.

Try to use BigInteger.Parse method.

Wojteq
  • 1,153
  • 9
  • 22
1

While SQL Server does not error, its not giving you the correct answer. Its truncating the hex string to just the lower 32bits

select convert(int, 0x2D221D30) = 757210416
select convert(int, 0x1B1D3E1B22176145272C1631282D221D30) = 757210416

If you change this to bigint you get different results:

select convert(bigint, 0x2D221D30) = 757210416
select convert(bigint, 0x1B1D3E1B22176145272C1631282D221D30) = 3176780635782126896
user957902
  • 2,890
  • 11
  • 17
  • So I'm actually not guaranteed uniqueness with the INT conversion, correct? – Jay Stevens Dec 13 '11 at 18:19
  • Correct. At best you could use it as a hash value. If you need a unique value you should look at using the the uniqueidentifer SqlServer type. – user957902 Dec 13 '11 at 18:24
  • Problem is less needing unique key. I have that. I have to match up to an existing Database that is now using this MD5 hash as their external key – Jay Stevens Dec 13 '11 at 21:53
  • Have you tried just storing it in a varbinary(17) field? You should be able to take your hex value constant and directly store it in that type. – user957902 Dec 13 '11 at 22:06
0

I tried it on wolfram that is a 133bit number. You can try using a BigInteger(Byte[]) in c#

This is the binary number in case you wanted to see it: 1101100011101001111100001101100100010000101110110000101000101001001110010110000010110001100010010100000101101001000100001110100110000

SamFisher83
  • 3,601
  • 7
  • 35
  • 50
0

You (nor SQL Server) can decode 34 bytes into a 4 byte integer. Hexedecimal strings represent each byte of data as two characters, one for the high nibble (4 bits), one for the low nibble.

You can use a HexEncoding class I wrote to decode this into bytes or write your own.

see also http://en.wikipedia.org/wiki/Hexidecimal

csharptest.net
  • 53,926
  • 10
  • 66
  • 86