views:

74

answers:

1

I know how to convert hexadecimals to integers when i only have one hexadecimal character. but im getting a string of two characters.

I'm converting both characters to hex with:

String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()))

so then lets say i have 01, and 3f as my two length characters. how am i supposed to convert those to integer? i tried doing

int length = int.Parse("013f", System.Globalization.NumberStyles.AllowHexSpecifier);

but apparently that is incorrect. i even took the values of both of those, Concat'd them together, and then tried to parse them.. that obviously was not right

am i doing something wrong trying to parse these two hexadecimals into a length, or is the server im getting the information from using a non-standard length system. because i can get the correct lengths when the first character is 00, like 0001 or something. im just not sure about when i have two length characters.

EDIT: after some trial and error, the expected value is 491

Update number two:

Basically the server provides me with a two character long string, that tells you the length of the following data.

the two characters for this specific attribute are 01, and 3f. the following data is 491 characters long, which as far as i can see, has no relation to 013f what-so-ever, but i know there is one somewhere, because another application is using this exact same data but coming out with the correct results.

+2  A: 

It's not really clear what the problem is.

The hex value 0x013F is decimal 319. Your code evaluates this correctly, although I'd use NumberStyles.HexNumber rather than NumberStyles.AllowHexSpecifier:

int length = int.Parse("013f", NumberStyles.HexNumber);

What number are you expecting 0x013F to be converted to?

LukeH
Also you can use Convert.ToInt32() method: http://msdn.microsoft.com/en-us/library/1k20k614.aspx
Nick Martyshchenko
that is what im doing, and that is coming out with 319 i think which is not the length that i need.
Tommy
319 is correct, isn't it?
adrift
@Tommy: So why don't you tell us what result you are expecting, since it's obviously not a straighforward hex-to-decimal translation?
LukeH
@Tommy: 0x13f = 319, do you use it as Convert.ToInt32("013f", 16)?
Nick Martyshchenko
after ALOT of trial and error, im thinking that the length im expecting is 491.
Tommy
@Tommy: I can't see any correlation between `0x31F` and `491`. Can you tell us how you did the calculation? Or, failing that, show us a few more input/output combinations so that we can try to find the pattern ourselves.
LukeH
@LukeH: I dont know what to say, but i updated my question with a little bit more clear information.. is there something else in particular you were looking for besides that?
Tommy
@LukeH: I showed how i did the conversions in the question
Tommy