There is a need to write code today: In order to be unified with other devices in the same product, the host computer requires processing strings, not arrays. We all know that the array is convenient when processing the data, but considering the other devices (yes, I said that TI430 do RFID card reader device, and the protocol between the host computer is the string/laughter), tried to change the 16 binary data into a string, that is, 0xAB, changed to "AB" ... Although it doesn't seem to work.
The original data type is uint8_t, familiar with the SCM know the meaning of this, in fact, is unsigned char, in the final analysis is a char, but it is required to represent the ' A ' B ' two char, and then my heart is of course refused, but there is no way, Or to realize this unreasonable demand with brains. The code is as follows:
#include <stdio.h>#include <stdlib.h>typedef unsigned Charuint8_t;intMain () {inti =0; uint8_t data[5]={0x12,0x34,0x56,0xAB,0xEF}; uint8_t str[Ten]; uint8_t dst[Ten]; for(i =0; i<5; i++) {str[2*i] = data[i]>>4; str[2*i+1] = data[i]&0xf; } for(i =0; i<Ten; i++) {sprintf(&dst[i],"%x/n", Str[i]); } for(i =0; i<Ten; i++) {printf("%c\n", Dst[i]); }return 0;}
The idea is to use the sprintf function to put an array of parameter 3 in the format of parameter 2 in the place of parameter 1, the sprintf function is a bit outdated, it has a more secure brother called sprintf_s, But the IDE that I use may be too old (IAR8.10, hehe), do not support his brother.
There is a warning that the type of parameter 1 cannot be unsigned,
Warning:pointer targets in passing argument 1 of ' sprintf ' differ in signedness [-wpointer-sign]|
But the function realized, no longer toss it, anyway, no one to see, find a solution to the more.
If you want to change to lowercase letters, you can change the second parameter to%x.
Here is the result of the operation, the IDE is code::blocks 16.01,GNU GCC Compiler
A method of converting 16 binary numbers to strings in the C language