How to convert this hex string to long?

I have: "0xE94C827CEB" in hexadecimal, but as a string.

Which: 1002011000043 (dd mm yyyy hh mm ss)

Unfortunately, I don’t know how to do the conversion if I only have it in the string format and I don’t have the Convert.ToLong function ("0xE94C827CEB", 16) because I use the .NET Micro Framework (there is also no space NumberStyles.)

Is there a function that will convert this for me?

thanks

+8
c # hex .net-micro-framework netduino
source share
3 answers

I don't know any function for this, but I think you can do it quite simply by splitting the hexadecimal string and passing each part through Convert.ToInt32 ():

int part1 = Convert.ToInt32("E9", 16) int part2 = Convert.ToInt32("4C827CEB", 16) //the last 4 bytes long result = part1 * 4294967296 + part2 //4294967296 being 2^32. Result = 1002011000043 
+11
source share

For those who are looking for an answer, using the full .NET platform for PC.

 long answer = Convert.ToInt64("E94C827CEB",16); 

see MSDN documentation

+13
source share

Kill him with the old school and drop your own. This is not exactly rocket science:

 public ulong HexLiteral2Unsigned( string hex ) { if ( string.IsNullOrEmpty(hex) ) throw new ArgumentException("hex") ; int i = hex.Length > 1 && hex[0]=='0' && (hex[1]=='x'||hex[1]=='X') ? 2 : 0 ; ulong value = 0 ; while ( i < hex.Length ) { uint x = hex[i++] ; if ( x >= '0' && x <= '9' ) x = x - '0' ; else if ( x >= 'A' && x <= 'F' ) x = ( x - 'A' ) + 10 ; else if ( x >= 'a' && x <= 'f' ) x = ( x - 'a' ) + 10 ; else throw new ArgumentOutOfRangeException("hex") ; value = 16*value + x ; } return value ; } 
+3
source share

All Articles