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?
While SQL Server does not error, its not giving you the correct answer. Its truncating the hex string to just the lower 32bits
If you change this to bigint you get different results: