0

I have an unsigned char array where I would like to parse its first four elements as HEX values.

I receive and unsiged char array a[],whose content is this

for (int i = 0; i < l; i++) { SerialUSB.print(" Index : "); SerialUSB.print(i); SerialUSB.print(" ,Unsigned Char "); SerialUSB.print( a[i]); SerialUSB.print(", CHAR "); SerialUSB.print( (char) a[i]); SerialUSB.println(); } Index:0 ,Unsigned Char 98, CHAR b Index:1 ,Unsigned Char 49, CHAR 1 Index:2 ,Unsigned Char 53, CHAR 5 Index:3 ,Unsigned Char 57, CHAR 9 Index:4 ,Unsigned Char 55, CHAR 7 Index:5 ,Unsigned Char 56, CHAR 8 Index:6 ,Unsigned Char 85, CHAR U Index:7 ,Unsigned Char 55, CHAR 7 Index:8 ,Unsigned Char 56, CHAR 8 Index:9 ,Unsigned Char 85, CHAR U Index:10 ,Unsigned Char 55, CHAR 7 Index:11 ,Unsigned Char 56, CHAR 8 Index:12 ,Unsigned Char 85, CHAR U 

How can I get the HEX value of the first four elements of array a[]

inHex[0] = 0xB; // 0xB = 11 inHex[1] = 0x15; // which should come from having concatenated the values `a[1]= '1'` algong with `a[2]='2'` inHex[3] = 0x9; // which should be the char value of a[3] in HEX 

Thanks in advance.

20
  • 1
    @ndarkness I totally don't get what you are trying to do even when you updated your question. Can you write something clear, without leaving as guessing what is a etc?! Commented May 11, 2017 at 20:21
  • 1
    You have char * a = "b159 ... ", why then you say a[2]='2' ? Do you wan to get 4 different integers for each of 0xb, 0x1, 0x5, 0x9? Commented May 11, 2017 at 20:22
  • 1
    inHex[0] = 0x11; // which should come from having converted toHEX( (char) a[0] ) - this is complete mess, nothing makes sense. it's not to hex, but from hex, result of toHex is a string, if you want to get integers then it's from hex. b or 'B' if treated as a hex digit means 11, or 0xb if written to hex. Commented May 11, 2017 at 20:34
  • 1
    @ndarkness why is that 'b' becomes 0x11!? Do you understand yourself? Why is that second 0x15 is formed by strcat(a[1],a[2])? Commented May 11, 2017 at 20:38
  • 1
    @ndarkness b I would interpreate it as the HEXadecimal representation B - what is the difference? 0xb == 0xB Commented May 11, 2017 at 20:39

1 Answer 1

2

You can get integer value from a hex digit (where hex digit is any character from this set: 0123456789abcdefABCDEF):

int fromHex(char c) { if (c>='0' && c<='9') return c-'0'; if (c>='a' && c<='f') return 10+(c-'a'); if (c>='A' && c<='F') return 10+(c-'A'); return -1; // unexpected char } 

this way if you have char a[] = "abcd", you'll get {11, 11, 12, 14} for each of the chars.

Then to get integers from you your first 4 chars:

inHex[0] = fromHex(a[0]); inHex[1] = fromHex(a[1]) * 16 + fromHex(a[2]); inHex[2] = fromHex(a[3]); 

Sample code:

#include <stdio.h> int fromHex(char c) { if (c >= '0' && c <= '9') return c - '0'; if (c >= 'a' && c <= 'f') return 10 + (c - 'a'); if (c >= 'A' && c <= 'F') return 10 + (c - 'A'); return -1; // unexpected char } int main() { char a[] = "b159"; int inHex[3]; inHex[0] = fromHex(a[0]); inHex[1] = fromHex(a[1]) * 16 + fromHex(a[2]); inHex[2] = fromHex(a[3]); printf("%d %d %d\n", inHex[0], inHex[1], inHex[2]); printf("0x%x 0x%x 0x%x\n", inHex[0], inHex[1], inHex[2]); } 

output:

11 21 9 0xb 0x15 0x9 
Sign up to request clarification or add additional context in comments.

2 Comments

To get the value of a hex character.
I am commenting on the imprecision of your first sentence. I am aware of what the function does.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.