Go Down

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

#### Leon2k

##### Sep 19, 2012, 03:09 pm
Hi
How to convert a text string "003C" to Hex value 0x003C ?
Thanks in advance for any help

#### PaulS

#1
##### Sep 19, 2012, 03:18 pm
Quote
How to convert a text string "003C" to Hex value 0x003C ?

Use strtoul().

#### Leon2k

#2
##### Sep 19, 2012, 03:36 pm
Thank you Pauls...
I'm pretty new beginner, please give me an example string "003C" to hex 0x003C

#### PaulS

#3
##### Sep 19, 2012, 03:48 pm
Quote
I'm pretty new beginner, please give me an example string "003C" to hex 0x003C

No. If you really have a string containing "003C", you should be able to google strtoul() to figure out that it takes three arguments (the string to convert, a place to store a pointer to the first non converted character, which can be NULL, and the base (16 for hex)), and that it returns one value - the result of the conversion as an unsigned long.

There is even an example of calling the function, but calling strtoul() is so trivial that an example is not needed.

#### pico

#4
##### Sep 19, 2012, 04:43 pmLast Edit: Sep 19, 2012, 04:56 pm by pico Reason: 1

Quote
How to convert a text string "003C" to Hex value 0x003C ?

Use strtoul().

Or even better -- don't use strtoul(). In fact, don't use the C++ string library at all, if you can possibly avoid it.

Assuming null delimited C-style strings (really character arrays declared like char s[] = "this is an C-style string"; ), you can write a simple conversion fn in a few lines like this:

Code: [Select]
`int x2i(char *s) {  int x = 0;  for(;;) {    char c = *s;    if (c >= '0' && c <= '9') {      x *= 16;      x += c - '0';    }    else if (c >= 'A' && c <= 'F') {      x *= 16;      x += (c - 'A') + 10;    }    else break;    s++;  }  return x;}`

Pass a string like

int v = x2i("003C");

or

int v  = x2i(s);

and you will get a numeric conversion from a string of hexadecimal digits. Note this won't work for lower case letters (although easy to add -- I will leave as an exercise). It will stop converting on the first character it finds that is not 0-9 or A-F (hopefully that would be the null delimiter!), but be aware it would also return x2i("Can I have a slice of toast?") as 12 (i.e. 0x0C), for example.

(BTW, once an integer type variable holds a value, it makes no sense to describe it as decimal or hex or octal -- those are string representations for human readability. The integer v will hold exactly the same value for int v = 12; and v = 0x0C; for example. So really, the title of the post should really be something like "hex string to int converter".)

Hope that helps.
WiFi shields/Yun too expensive? Embeddedcoolness.com is now selling the RFXduino nRF24L01+ <-> TCP/IP Linux gateway: Simpler, more affordable, and even more powerful wireless Internet connectivity for *all* your Arduino projects! (nRF24L01+ shield and dev board kits available too.)

#### Pavilion1984

#5
##### Sep 19, 2012, 08:13 pmLast 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

#6
##### Sep 19, 2012, 09:35 pm

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.

#### Pavilion1984

#7
##### Sep 19, 2012, 09:45 pm
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

#8
##### May 08, 2013, 12:44 am
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

#9
##### May 08, 2013, 05:30 am

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.

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...)
http://www.catb.org/jargon/html/I/I-didn-t-change-anything-.html

#### WizenedEE

#10
##### May 08, 2013, 07:20 am

Quote
How to convert a text string "003C" to Hex value 0x003C ?

Use strtoul().

Or even better -- don't use strtoul(). In fact, don't use the C++ string library at all, if you can possibly avoid it.
strtoul was in C. What's wrong with it? It does the same thing as your code except probably faster because it's optimized and debugged.

Go Up