Convert Hex String to Long/Decimals?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a problem. But on .NET 1.1

My Scenario:
Actually I will have a string of hexadecimals read from a xml file. Then
from the hexadecimals, i will add 1 value whenever i made any modifications.
But just i do not how to get started. It is like an incremental hexadecimals.

I have a string of hex.

string strHex = "0100000000FF";

I want to convert this Hex to decimals, just like the windows calculate,
when you type FF in Hex and click on Dec radio button, it will change to 255.

I tried string.Format("{0:d}", strHex);

I tried Convert.ToDecimal or Convert.ToInt64

But i still cannot find a solution?

----

I know how to convert long to hex. Like this.


for (long x = 000000000000; x <= 281474976710655; x++)

{

Console.WriteLine(string.Format("{0:x12}", x));

Console.ReadLine();

}

But i do not know how to do the opposite? Anyone can help me please?

Thanks.
 
Hi,

you could use this:
int number;
string strHex = "0100000000FF";
number = Convert.ToInt32("FF", 16);
// ..... some operation maybe
// number++
strHex = number.ToString("X");

This works, but it doesn't output leading zeros.

Christof
 
Try the following:

Int64.Parse("0100000000FF",System.Globalization.NumberStyles.HexNumber)
 
Ooops
you should use long instead of int and ToInt64 instead of ToInt32.
otherwise your string value is to big.
My Example would work up to "7FFFFFFF"

Christof
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top