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

Author: Thomas Müller (Guest)
Posted on:
Attached files:

Rate this post
1 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 

- 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..
#include <stdio.h>
#include <string.h>

int hex_to_int(char c){
        int first = c / 16 - 3;
        int second = c % 16;
        int result = first*10 + second;
        if(result > 9) result--;
        return result;

int hex_to_ascii(char c, char d){
        int high = hex_to_int(c) * 16;
        int low = hex_to_int(d);
        return high+low;

int main(){
        const char* st = "48656C6C6F3B";
        int length = strlen(st);
        int i;
        char buf = 0;
        for(i = 0; i < length; i++){
                if(i % 2 != 0){
                        printf("%c", hex_to_ascii(buf, st[i]));
                        buf = st[i];

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.

Author: Vincent Van Beveren (Company: Nikhef) (vvanbeveren)
Posted on:

Rate this post
0 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 = 
    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
  // 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);



Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]

Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.