Hallo Leute, ich brauche Hilfe bei der Umwandlung und Ausgabe folgender
hexadezimaler CS_UID(immer 64bit lang) die so definiert ist:
- #define CS_UID 0x1234567890abcdff
und welche mit folgender Methode an die UART geschrieben wird:
- void appWriteDataToUart(uint8_t* aData, uint8_t aLength);
Ich möchte die CS_UID irgendwie sinnvoll umwandeln, sodass sie über die
UART ausgegeben werden kann..
Jemand meinte der folgende Code würde mir helfen es in ascii
umzuwandeln..
-----------------------------------------------------
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 | }
|
------------------------------------------------------------------------
und folgendes ist dabei rausgekommen..
------------------------------------------------------------------------
1 | #include <stdio.h>
|
2 | #include <string.h>
|
3 |
|
4 | #define CS_UID 0x1234567890abcdff
|
5 |
|
6 | int hex_to_int(char c){
|
7 | int first = c / 16 - 3;
|
8 | int second = c % 16;
|
9 | int result = first*10 + second;
|
10 | if(result > 9) result--;
|
11 | return result;
|
12 | }
|
13 |
|
14 | int hex_to_ascii(char c, char d){
|
15 | int high = hex_to_int(c) * 16;
|
16 | int low = hex_to_int(d);
|
17 | return high+low;
|
18 | }
|
19 |
|
20 | int main (void){
|
21 |
|
22 | const char* st = CS_UID;
|
23 | char csuid_array[8]; //Zum Ausgeben später über UART
|
24 | int length = strlen(st);
|
25 | int j;
|
26 | char buf = 0;
|
27 | for (j=0; j < length; j++){
|
28 | if(j % 2 != 0){
|
29 | csuid_array[j] = hex_to_ascii(buf,st[j]);
|
30 | }
|
31 | else{
|
32 | buf = st[j];
|
33 | }
|
34 | }
|
35 | }
|
-----------------------------------------------------------------------
aber es funktioniert nicht, ich habe sicherlich einiges falsch gemacht,
bin da noch nicht so erfahren.. ich wäre dankbar für jegliche Hilfe oder
andere Lösungen/Lösungsansätze!