How to convert char array to byte?

Hi,

I am new to c and I'm having trouble with something. According to
this I can use byte() to convert value x of any type to byte data type. I am trying to convert "0xff" to 255, but using byte() is displaying 40. Is there a build in function I can use or do I have to build something myself? Thank you.

 char high[4] = "0xff";
 Serial.println(byte(high));

UPDATE:
I was trying to simplify the problem I was facing, but here is a more detailed idea of what I am trying to do. I am trying to parse some JSON data using ArduinoJson. Below you can see a simplified version of my JSON file and my goal is to create a list of bytes for each frame. I'm using JsonArray to extract the frame lists and then copying the JsonArray to a String array using copyArray and then try to convert that to a list of bytes.

JSON file (simplified; real file is pretty big)
{"frames":{"0":["ff","ff","ff","ff"],"1":["0","0","0","0","0"],"2":["ff","ff","ff","ff"],"3":["0","0","0","0","0"]}}

what you want do can be achieved several different ways -

If you show us your project, there’s probably a faster, tidier, more efficient way to achieve it.

//         ***
  char high[5] = "0xff";
  Serial.println(strtoul(high, NULL, 16));

Also, "0xff" is 5 bytes instead of 4 bytes.
The C string requires a null termination.

atol(), or,if a smaller integer will do, you could use the atoi() function.

I think atoi() not supports arguments in hexadecimal string.

image

Good point, perhaps sscanf(), although it has a bigger footprint, it also supports all number bases if needed.
Thanks for mentioning the hex !

SSCANF()

The third parameter of strtoul() is any radix(a.k.a. number bases). :wink:

https://www.cplusplus.com/reference/cstdlib/strtoul/

Hmmm, we learn something every day… thanks.

Would be interesting to know where the "0xFF" string originate from. May be there is more straightforward way if the format could be different. (Otherwise yes, strtoul() is the solution)

Should be [5] but you don’t need to explicitly indicate, it would be done for you if you leave it empty

char high[] = "0xff";

But since you are not modifying 'high' you can just make it a const char *

const char *high = "0xff";
1 Like

I think he is trying to convert the value 0xFF to a char.
In this case, you do not need to surround 0xFF in quotes. The 0x part mean that the following thing is in "hexadecimal format", and a value of FF equal to 0b11111111 (with 0b denote the following to be in binary), or 255.
Just do:

//make the syntax look right
char high = 0xFF;

Note the 0xFF is now blue.


however, if you want to convert "0xFF"(which is a string), you need to throw out the "0x" part because it mean nothing. Then you toss "FF" to a hexadecimal converter (mutation of atoi or the like. I've never seen one before)

strtoul() function correctly interprets both "0xFF" and "FF".
If really receive data as a character string, it doesn't matter which one.

1 Like

this is indeed helpful. I haven't even heard of it.


If we do a traditional "atoi", it will read"0", but the next character is "x", so it will end there and return 0.

What makes you think this when OP explicitly wrote a cString as the source?

char high[4] = "0xff"; // with wrong length

maybe he think that the correct representation for 0xFF is a string.
But he understand that 0xFF evaluate to 255, so, maybe he put it intentionally as a string.

Lots of maybe :wink:

OP should clarify

I am parsing data from a JSON file using ArduinoJson and one of my variables is a list of string lists and I am trying to convert all those strings to bytes which I will be sending to Teensy.

Thank you :slight_smile: This worked for this example. I will incorporate it into my project.

Im actually parsing data from a JSON file using ArduinoJson and was under the impression that I had to use strings in that format.

This is a simplified version of my JSON file and my goal is to create a list of bytes for each frame. I'm using JsonArray to extract the frame lists and then copying the JsonArray to a String array and then try to convert that to a list of bytes.

JSON file
{"frames":{"0":["ff","ff","ff","ff"],"1":["0","0","0","0","0"],"2":["ff","ff","ff","ff"],"3":["0","0","0","0","0"]}}

ArduinoJson Could help you go through the structure and parse using strtoul()