Home AMX User Forum AMX Technical Discussion

Hex value to Integer

I have a string where I'm parsing out 1 character at a time. The first character is $22, when I try and use HEXTOI(char) I get an error, I think it's because $22 is not a literal string which is what it looks like HEXTOI needs to do the conversion per the documentation.

Is there any other way to convert $22 to an Integer? How would this be done?

Thanks,

J

Comments

  • mpullinmpullin Posts: 949
    $22 is already an integer; it's 34. The $ in front just means it is being represented in hexadecimal, e.g. when you look at it in your notifications as part of an incoming string.
  • DHawthorneDHawthorne Posts: 4,584
    This topic keeps coming up, because it truly is confusing until you wrap your head around it. All data is the same, as far as the values stored in the program are concerned. What's different is the notation used to represent it to the human reader. If my protocol calls for hex 20, they are just telling me they are representing the value in hex, and I can send a space character, or decimal 32, and it's the same thing.

    But where it really gets hairy is that we are never actually typing in those values in any form, as there is another layer of abstraction: ASCII. Everything you type on a computer is an ASCII code representing the value, and there is something in the notation to tell the compiler how you want it translated. In the AMX world, enclosing it in single spaces tells the compiler, "I am typing it in ASCII, and I want you to send the ASCII values directly", which is how we do text and string variables. If I type in numerals, I'm telling it, "I am typing in ASCII, but I am representing my numbers in decimal, so translate it as such" If I put a $ in front of it, it's hex instead of decimal, but it's all just a way of telling the compiler how to translate what I am typing. They are all interchangeable as long as the device is getting the values it expects.

    Every now and then, just to thoroughly confuse people, a protocol is written so that the device is actually asking for the ASCII representation of the hex value, not the actual hex value. Some also may want the ASCII representation of the decimal value too, which is more common. In both those cases, you are sending the ASCII directly, with whatever notation the device requires to translate it properly. In those cases, you type enclosed in single quotes, and those are the cases you might want to use functions like ITOHEX(), which returns an ASCII with hex notation string for a numeric value represented in hex. However, it is very important to be sure that is what they really want. Most of the time, they really just want the value itself. I won't even get into the protocols that are poorly documented and don't really tell you what they want ...

    Clear as mud, eh? Maybe someone else can chime in who is better at explaining.
Sign In or Register to comment.