Pages: [1]   Go Down
Author Topic: STRING To HEX Converter  (Read 4071 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi
I need help Please...
How to convert a text string "003C" to Hex value 0x003C ?
Thanks in advance for any help
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
How to convert a text string "003C" to Hex value 0x003C ?
Use strtoul().
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you Pauls...
I'm pretty new beginner, please give me an example string "003C" to hex 0x003C
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
God Member
*****
Karma: 32
Posts: 828
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
« Last Edit: September 19, 2012, 09:56:22 am by pico » Logged

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.)

Scunthorpe, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 129
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: September 19, 2012, 01:15:00 pm by Pavilion1984 » Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12541
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Scunthorpe, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 129
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Note that the above sample code works only for upper case hex.  Not a big deal.  Just add another else if:
Code:
else if (c >= 'a' && c <= 'f') {
      x *= 16;
      x += (c - 'a') + 10;
}
Logged

Mid-Atlantic, USA
Offline Offline
God Member
*****
Karma: 30
Posts: 514
"Remember kids, the only difference between Science and screwing around is writing it down." - Adam Savage
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...)
Logged


Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: