0

I have a char* of user input hex digits that I want to convert to actual hex (u8_t type) to be able to be used in an i2c command.

Example: Input: char* = 55 and I want to be able to use this as 0x55 in my i2c command so it has to be in hex format (My command looks like i2c_write(u8_t addr, blah, blah) so I would use it like i2c_write(0x55, blah, blah). Does anyone know how to change it? I've looked at resources online and I can only find how to convert it to an int.

PurpleSpark
  • 63
  • 1
  • 10
  • 1
    what is "actual" hex? If you convert it to an int then just use the low order byte of the int – Garr Godfrey Jul 23 '20 at 01:16
  • First, u8_t **is** an (8 bit, unsigned) int. Anyway, there are plenty of answer online, including here https://stackoverflow.com/questions/3408706/hexadecimal-string-to-byte-array-in-c – Boris Lipschitz Jul 23 '20 at 01:17
  • 1
    Does this answer your question? [Hexadecimal string to byte array in C](https://stackoverflow.com/questions/3408706/hexadecimal-string-to-byte-array-in-c) – DYZ Jul 23 '20 at 01:18
  • Shouldn't this `char* = 55` read `char* = "55"`? – alk Jul 23 '20 at 05:15

1 Answers1

1

Never mind, I figured it out by using strtol(<char array>, NULL, 16)

PurpleSpark
  • 63
  • 1
  • 10