EmbDev.net

Forum: ARM programming with GCC/GNU tools Convert Hex CS_UID to ascii and write to UART


von Thomas Müller (Guest)


Attached files:

Rate this post
useful
not useful
Hey everybody, this is my first time posting, so please be patient with 
me.. I need help with my Code.. i am very unexperienced. I want to write 
the CS_UID (the unique ID of my microcontroller) to UART, using this 
function:

- void appWriteDataToUart(uint8_t* aData, uint8_t aLength);

Therefore i think i need to convert the hexadecimal CS_UID (64bit long) 
which is defined as follows for example:

- #define CS_UID 0x1234567890abcdff

into something that makes sense and is possible to be written to UART.

Someone told me this would help..
-----------------------------------------------------
1
#include <stdio.h>
2
#include <string.h>
3
4
int hex_to_int(char c){
5
        int first = c / 16 - 3;
6
        int second = c % 16;
7
        int result = first*10 + second;
8
        if(result > 9) result--;
9
        return result;
10
}
11
12
int hex_to_ascii(char c, char d){
13
        int high = hex_to_int(c) * 16;
14
        int low = hex_to_int(d);
15
        return high+low;
16
}
17
18
int main(){
19
        const char* st = "48656C6C6F3B";
20
        int length = strlen(st);
21
        int i;
22
        char buf = 0;
23
        for(i = 0; i < length; i++){
24
                if(i % 2 != 0){
25
                        printf("%c", hex_to_ascii(buf, st[i]));
26
                }else{
27
                        buf = st[i];
28
                }
29
        }
30
}


but i dont know how to use that.. im clueless. These functions return 
"int" and use printf and %c.. i'd appreciate any help or other solution! 
Thanks in advance.

P.S: In the attached .c file at line 89 is where i am stuck.

von Vincent V. (Company: Nikhef) (vvanbeveren)


Rate this post
useful
not useful
Hi Thomas,

I'm not entirely sure what the above code is supposed to be doing, but 
converting a real number to hex is not that difficult. The idea is to 
shift out the highest nibble (4 bits) first, because that needs to be 
outputted first, and then the 2nd higest, etc...

void uint64ToHexString(uint64_t inp, uint8_t * outbuf)
{
  int v;
  int shift;
  // count down, so highest nibble comes first
  for (shift = 60; shift >= 0; shift -= 4) {
    // shift out 4 bits and mask, thus keeping only 4 bits
    v = (inp >> shift) & 0xF;
    // v is now the value between 0 and 15.
    // if its below 10, we simply take '0' and add v, cause '0' + 1 = 
'1'
    if (v < 10) {
      *outbuf = '0' + v;
    } else {
      // otherwise, we take 'a', add v - 10, cause 'a' + (11 - 10) = 'b'
      *outbuf = 'a' + (v - 10);
    }
    // increase output pointer to next character
    outbuf++;
  }
  // null terminate, so it plays nice with printf() and such
  *outbuf = '\0';
}

Now you can define an temporary buffer, needs to be one byte bigger than 
the number of hex characters, e.g. for 64 bit value, this would be 8 * 2 
+ 1.

uint64_t hexValue = CS_UID;
uint8_t output[17];
uint64ToHexString(hexValue, output);

appWriteDataToUart(output, 16);

Regards,
Vincent

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.