char array to byte

Hey!

I'm trying to convert a char array into one byte, but I can't figure out how to. I only know how to copy one single char into the byte.

byte myByte [1] = {0x10};
char myChar[] = "0x13";

void setup() {

  Serial.begin(115200);

  Serial.println(myByte[0], HEX);
  Serial.println(myChar);

  myByte[0] = myChar[0];

  Serial.println(myByte[0], HEX);
  Serial.println(myChar);

}

void loop() {

}

Any ideas?

Regards,
Christian

What do you expect the byte to hold after you're done?

Also, see XYproblem.

septillion:
What do you expect the byte to hold after you're done?

Also, see XYproblem.

0x13

I don't get why you are saying that I'm trying to fumble my way into anything. I simply want to learn how to solve my problem.

Let's call that experience. A lot of newbies think they need to do stuff in a (very) specific way although in reality they are making a Robe Goldberg machine.

But for now, have a look at strtol().

Are you trying to get the hex value 13 to appear in byte[0]? An example of strtol()

septillion:
Let's call that experience. A lot of newbies think they need to do stuff in a (very) specific way although in reality they are making a Robe Goldberg machine.

But for now, have a look at strtol().

I can understand that, but there is nothing in my code implying that I'm trying to do something very simple in a very complex way. I'm almost certain that you too have been new to this at one point.

Thank you, and the others, for the tip about strtol. I think that'll work.

chrstrvs:
I can understand that, but there is nothing in my code implying that I'm trying to do something very simple in a very complex way.

I disagree. It's not very normal to end up with hex text. Most of the time it's due to newbies not understanding the difference between a value and value representation.
0x13 == 19 == 023 == 0b00010011 == '\r' only in different representation
but indeed it's not "0x13"

chrstrvs:
I'm almost certain that you too have been new to this at one point.

Yes I have. But if I asked for help I always painted the whole picture. And not saying you are not. If you just want to learn rather than you're in the middle of a projects, fine. But do remember, that makes you the 1% of the newbies here :wink: I just try to save everybody a lot of time in case it turn out to be a XYproblem and the OP is trying to build a Rube Goldberg machine to blink a led...

Might help ?

A byte is 256 bits and can only “hold” one character.
If you have an array of characters then you can only convert one cell of that array into a character , e.g: characterA= array[1]; characterB= array[2].
And so on.

There is a lot on line google such things as “ Arduino char to int” or whatever you want.

I do wonder what you are trying to do , there may be an easy solution?

hammy:
A byte is 256 bits

How about no. A byte is 8-bit. And with 8-bit you can make 256 different values.

I'm sensing an XY problem, here.

Probably, what the OP is after is '\x13'. Actually, what they are probably after is an ASCII 13, which is not hex, and can better be drawn '\r'. Likewise, that 0x10 is probably meant to be an ascii 10, aka '\n'.

PaulMurrayCbr:
I'm sensing an XY problem, here.

Probably, what the OP is after is '\x13'. Actually, what they are probably after is an ASCII 13, which is not hex, and can better be drawn '\r'. Likewise, that 0x10 is probably meant to be an ascii 10, aka '\n'.

Probably not. What I am after is the hex value 0x13. After some more investigating I learned that septillion is right about the difference in value and value representation. I did not know about the difference, but now I do. At least more than before.
Using strtol I got the decimal value 19, which works fine for me.

Thank you all for your help. It is greatly appreciated.

Great you learned a thing or two! ;D

To recap, you can write integer literals in any of the relevant bases (binary, octal, decimal, hexadecimal). This has no impact on how it is actually seen by the microcontroller, because it only understands binary.

For example:





```
  Serial.printlncolor=#000000[/color]; // Prints “19” 

Serial.printlncolor=#000000[/color];        // Prints “19” 
  Serial.printlncolor=#000000[/color];        // Prints “19” 
  Serial.printlncolor=#000000[/color];      // Prints “19” 
```

|

These four lines are exactly the same. The compiler converts them all to 0b00010011.

It is only when you request to print the value, that it will be converted to text.
This text is also represented by binary numbers. The link between the actual number and the character they represent is completely arbitrary. Most of the time, ASCII is used (nowadays, it’s mostly UTF-8, but it is compatible with ASCII for the most important characters).

For example:





```
  Serial.print(0x13, BIN);    // Prints “10011”

Serial.print(0x13, OCT);    // Prints “23”
  Serial.print(0x13, DEC);    // Prints “19”
  Serial.print(0x13, HEX);    // Prints “13”
```

|

The string “10011” that is printed on the first line is represented by the numbers [0x31 0x30 0x30 0x31 0x31 0x00] (I wrote them here in hexadecimal for convenience, but as mentioned above the base of the number doesn’t matter to the Arduino). All you need to remember is that the digit/character ‘0’ is represented by number 0x30. The 0x00 byte (NULL) at the end is used to know where the text ends.

Note how I use single quotation marks for a single character, double quotes for a string of characters, and no quotation marks for normal numbers. This is the same as you’d write in your code.

The text representations are only to help humans read or write the numbers. All calculations on the Arduino happen in binary. It’s also much less memory-efficient to save numbers as strings of text, for example, the number 128 can be represented as a single byte (0b10000000), but if you were to convert it to decimal text, you’d need 4 bytes (three characters + NULL), and if you’d convert it to binary text, you’d need 9 times more (eight characters + NULL).

The important thing to take away here is that there are three different scenarios where you represent numbers:

  • The text representation you use in your code. This can be binary, octal, decimal or hexadecimal.
  • The compiler converts these characters in your code to the binary representation that’s used by the Arduino.
  • The Arduino can then convert these binary represented numbers back into a string of text, using the print functions.

Unless you have to parse text data (from a text file on an SD card, from a protocol that uses ASCII like the HTTP protocol, etc.), it’s not a good idea to save numbers as text on the Arduino.

Pieter

PieterP:
(Alotta text)

Thank you very much for clarifying all of that, that was very kind of you!