How can i convert a 4 bytes hex to decimal?  
Author Message
qiyushen





PostPosted: Visual C# General, How can i convert a 4 bytes hex to decimal? Top

I have a hex number, for example "C2D3". How can i convert the 4 bytes hex to decimal

I try to use byte.parse(), but the compiler show "C2D3" is too big. I also want to try BitConverter.ToInt16(), but i feel it's not convinent.

Do you guys have any good function on converting the "C2D3" to decimal Thanks!




Visual C#17  
 
 
Sagitt





PostPosted: Visual C# General, How can i convert a 4 bytes hex to decimal? Top

Assuming that your hex number is a string:

int num = Int32.Parse("C2D3", System.Globalization.NumberStyles.HexNumber);

 
 
qiyushen





PostPosted: Visual C# General, How can i convert a 4 bytes hex to decimal? Top

If i have a sign indicator at the 1st bit in the Hex number, what can i do

For example, "8001" -> -1, "0001"->1

Thanks!



 
 
James Curran





PostPosted: Visual C# General, How can i convert a 4 bytes hex to decimal? Top

short num = Int16.Parse("C2D3", System.Globalization.NumberStyles.HexNumber);

which also could be written as:

short num2 = Convert.ToInt16("C2D3", 16);



 
 
Paul Louth





PostPosted: Visual C# General, How can i convert a 4 bytes hex to decimal? Top

If i have a sign indicator at the 1st bit in the Hex number, what can i do

For example, "8001" -> -1, "0001"->1

Thanks!

Btw. FFFF == -1 not 8001. But yeah use the Int16 parse with base 16.