STRING To HEX Converter

Hi
I need help Please…
How to convert a text string “003C” to Hex value 0x003C ?
Thanks in advance for any help

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

Use strtoul().

Thank you Pauls…
I’m pretty new beginner, please give me an example string “003C” to hex 0x003C

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.

PaulS:

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:

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.

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.

Pavilion1984: 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 [u]from[/u] a string.

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.

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

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

Pavilion1984:

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:

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:

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:

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

pico:

PaulS:

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.

I want to convert a string value which is coming from another board via UART [Serial.readString] to hex value. If the incoming value is "3A" then it should display "003A" . Tried strtoul n sprintf. Both are not working for me

Post what you tried, and we'll try to show you why it didn't work, and how to correct it.

Tried strtoul n sprintf. Both are not working for me

Then you did something wrong. My crystal ball says that the problem is on line 5452546521465854296, but that is +/- 545882365496320635865.

METHOD 1:

char errortodisplay[10]={0};

while(Serial.available()) { str=Serial.readString(); }

if(str.indexOf("ERROR")>=0) { int x=str.indexOf("+"); int y=str.indexOf("#"); String nes=str.substring(x+1,y); // err= nes.toInt();

sprintf(errortodisplay,"%04X",nes.toInt()); Serial.println("er2dis="); Serial.print(errortodisplay);

But this is showing int values.

METHOD 2:

char errortodisplay[10]={0};

while(Serial.available()) { str=Serial.readString(); }

if(str.indexOf("ERROR")>=0) { int x=str.indexOf("+"); int y=str.indexOf("#"); String nes=str.substring(x+1,y); // err= nes.toInt(); char *ptr; long ret; ret = strtoul(nes, &ptr, 16); Serial.println("er2dis="); Serial.print(ret);

This is showing error at ret = strtoul(nes,&ptr,16)

Also is there any way to left zero pad in string?

       ret = strtoul(nes, &ptr, 16);

The strtoul() function does not take a String.

Also is there any way to left zero pad in string?

Yes.

You post code that won't compile. You don't show what is in the String instances. You don't show the output. ALL you do is whine "it doesn't work".

I'm convinced that you don't want help.

nes is a String, but strtoul expects a char array. That's why you get an eror. The String class has a method to turn Strings into strings.

Or, you could cut out the middleman, and not bother using Strings in the first place.

Hello,

I tried your function int x2i(char *s) . It works fine for 3 digits.

But my value is 6 digits like FC031A. How can I change your routine

to do 6 digits.

Thanks

By