Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Wow, rare that I can not find an answer on this forum through the search function.

 

I have a Hex string like "51DD8" that I need to convert to a double.

 

string sData = "651DD8 Plus More Stuff Here";
int iChannel = convert.tointeger(sData.substring(0,1));
double dData = convert.todouble("0x"+sData.substring(1,5));
//does not work, throws out "string not valid for this procedure" error
//51DD8 should = 1.59893

 

Thanks

MT

"Beer is proof that God loves us and wants us to be happy."

-Benjamin Franklin

  • Leaders
Posted

You do not need to 0x in number parsing functions. This is a C# (or c-style to be clearer) hexadecimal notation, not a .Net notation. VB uses a different notation (&H1234567) and other languages use other notations (such as $1337), and the .Net framework does not cater to a particular language.

 

To parse something as hexadecimal, you best be is to use the integer.Parse method, which allows you to specify a number style (including hex). I'm not sure quite how you are working out the integer and decimal parts from the hex value, but this might be a good starting point.

string startWithThis = "2f0b";
int intValue = Int32.Parse(startWithThis, System.Globalization.NumberStyles.HexNumber);
double becauseYouWantItAsADouble = intValue;

 

And because knowing is half the battle, in case anyone doesn't know, here is how you can make a round trip with hex strings and integers.

int someInt = 0x1337;
string someHexString = someInt.ToString("x"); 
// use "x" for lower case hex and "X" for upper case hex
int thatIntAgain = int.Parse(someHexString, System.Globalization.NumberStyles.HexNumber);

[sIGPIC]e[/sIGPIC]
Posted

I tried pushing it straight to a double just to see if it would work but it throws an exception.

 

I am actually working off an old excel spread sheet that parses a 33 char data string captured off a serial line. A voltage is input to the unit and then the unit digitizes the value and transmits it (900ish MHz) to another unit. What I do in the process is measure the voltage into the unit then check what value the unit thinks it sees (transmitted value on the serial line). Then I generate a curve to correct the voltage transmitted so it is very close to actual. I think the whole thing is pretty cool, but the testers are doing all of the voltage captures, comparisons and manipulations by hand. The whole thing is very slow and prone to human error, so this should improve the processing speed and reliability of the test by about 20 fold.

 

The actual formula to get it to double form will look something like.

Int32 iTemp;
double FinalValue;
iTemp = Int32.Parse(SerialData[x].Substring(1, 5), System.Globalization.NumberStyles.HexNumber);
FinalValue = convert.double(iTemp*5/2^20);
//not sure how that will work in c# still need to play with it.

"Beer is proof that God loves us and wants us to be happy."

-Benjamin Franklin

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...