I am using C in Labwindows CVI 8.5.
I convert the float to ASCII Hex(this part is already done):
#include <stdio.h>
#include <string.h>
int main () {
char K_char[20], K_ASCII[20];
int a = 30; //30 degree Celsius
int i, len ;
float k = a + 273.15; //Temperature Celsius to Kelvin
sprintf(K_char, "%.2f", k); //Temperature K convert to char array
len = strlen(K_char);
for(i = 0; i<len; i++){
sprintf(K_ASCII+i*2, "%02X", K_char[i]); //char array convert to ASCII Hex
}
printf("%s\n", K_ASCII);
return(0);
}
The result from above code is 3330332E3135.
Now I want to extract each byte from above string like this :
a[0] = 0x33
a[1] = 0x30
a[2] = 0x33
a[3] = 0x2E
a[4] = 0x33
a[5] = 0x35
Can someone give me some advice? Thanks for any help.
It seems you are over complicate it. You already have these values in K_char
. Like
K_char[0] = 0x33
K_char[1] = 0x30
K_char[2] = 0x33
K_char[3] = 0x2E
K_char[4] = 0x31
K_char[5] = 0x35
'0'
to '9'
or 'A'
to 'F'
into the numeric equivalent.Then loop over your ASCII format array like this:
size_t length = strlen(K_ASCII); uint8_t hex [length/2+1]; for(size_t i=0; i<length/2; i++) { hex[i] = to_hex(K_ASCII[2*i]); hex[i] <<= 4; hex[i] += to_hex(K_ASCII[2*i+1]); }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.