-1

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.

berg
  • 1
  • 1
  • Do you mean, that you want to convert `3330332E3135` back into a `float` value? – Weather Vane Feb 27 '20 at 14:48
  • I want to split 3330332E3135 into [33][30][33][2E][31][35] and then put them into 6 arrays: a[0] = 0x33, b[1] = 0x30, c[2] = 0x33, d[3] = 0x2E, e[4] = 0x33, f[5] = 0x35 – berg Feb 27 '20 at 14:53
  • 1
    You can `sprintf` them *directly* into the individual arrays, instead of into the single array `K_ASCII` which then needs splitting, although it's unclear why each `a[0]`, `b[1]` etc has a different index. – Weather Vane Feb 27 '20 at 14:58
  • 1
    Does this answer your question? [Hex to char array in C](https://stackoverflow.com/questions/1557400/hex-to-char-array-in-c) – Marco Bonelli Feb 27 '20 at 15:16
  • Avoid signed `char` issues: use `sprintf(K_ASCII+i*2, "%02X", (unsigned char) K_char[i]);` or the like. – chux - Reinstate Monica Feb 27 '20 at 15:16
  • Minor: `strlen()` not needed. `len = sprintf(K_char, "%.2f", k);` – chux - Reinstate Monica Feb 27 '20 at 15:18
  • @WeatherVane I see! I am trying to split it directly. It should be the same index. Thanks for reminding me. I have edited them. – berg Feb 28 '20 at 07:31
  • @chux-ReinstateMonica Thanks! I will modified my code according your suggestions. – berg Feb 28 '20 at 07:33
  • 1
    @MarcoBonelli the solution there is more complicated. but thanks for the info! I know how to solve my problem. – berg Feb 28 '20 at 09:29

2 Answers2

2

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
Eraklon
  • 4,206
  • 2
  • 13
  • 29
1
  • Write a function that converts a single ASCII character '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]);
    }
    
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • Good solution when `length` is even. With odd `length`, last array element not set. – chux - Reinstate Monica Feb 28 '20 at 12:36
  • @chux-ReinstateMonica Well you need to error check that in advance, because if the string length is uneven, converting to hex doesn't make sense. Good exercise for OP: add error handling. – Lundin Feb 28 '20 at 12:39