Go Down

Topic: STRING To HEX Converter (Read 4 times) previous topic - next topic

Pavilion1984

#5
Sep 19, 2012, 08:13 pm Last Edit: Sep 19, 2012, 08:15 pm by Pavilion1984 Reason: 1
I use strtoul(). It puts my ASCII sting into a unsigned long HEX string that i can use for anything and is only one line of code.

PeterH


I use strtoul(). It puts my ASCII sting into a unsigned long HEX string that i can use for anything and is only one line of code.


No, strtoul() doesn't put anything into any sort of string. It parses an unsigned long integer from a string.
I only provide help via the forum - please do not contact me for private consultancy.

Pavilion1984

Quote
No, strtoul() doesn't put anything into any sort of string. It parses an unsigned long integer from a string.


I know it doen't. I was just saying thats how i use it and that it's easy to use.

meawoppl

Note that the above sample code works only for upper case hex.  Not a big deal.  Just add another else if:
Code: [Select]

else if (c >= 'a' && c <= 'f') {
      x *= 16;
      x += (c - 'a') + 10;
}

Sembazuru


Quote
No, strtoul() doesn't put anything into any sort of string. It parses an unsigned long integer from a string.


I know it doen't. I was just saying thats how i use it and that it's easy to use.


It might be better to clarify your terminology. You had said:
Quote

I use strtoul(). It puts my ASCII sting into a unsigned long HEX string that i can use for anything and is only one line of code.

but you probably meant to say:
Quote

I use strtoul(). It puts my ASCII sting into a unsigned long HEX value that i can use for anything and is only one line of code.

or better:
Quote

I use strtoul(). It puts my ASCII sting into a unsigned long HEX variable that i can use for anything and is only one line of code.


The variable (at it's lowest level) only contains binary. It can be represented in many bases when output to the serial port (or other Stream device) with built in support for hex, octal, and decimal. You can create your own function to represent it in any base you like. (You could even write a function to represent it in roman numerals if you want.) But at the lowest level all variables are just plain binary.

(I actually read it the way you intended first until you were slapped down...)

Go Up