LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to convert the string to Hex array, Like the String "A0,00,0B,C0" convert to hex array 0xA0,0x00,0x0B,0xC0 in the labwindows

Please help me to solve this problem as the subject description.

Thanks a lot.

0 Kudos
Message 1 of 6
(8,022 Views)

If you know the exact numbers of elements your string is made of, you can simply use

 

    char    msg[64];

    int   a, b, c, d;

 

    sscanf (msg, "%x,%x,%x,%x", &a, &b, &c, &d);
 

If your string is dynamically allocated and can hold a variable number of arguments, you will need to operate in a loop, this way:

 

    int    i, a[10];

    char    msg[64];
    char    *item;

    i = 0;
    item = strtok (msg, ",");
    while (item) {
        sscanf (item, "%x", &a[i++]);
        item = strtok (NULL, ",");
    }
 

The "%x" operator evaluates a string of hexadecimal digits (0 to 9, a to f) and calculates the corresponding integer number. This number can be lately represented in hex, decimal, octal or binary format while keeping the same exact value (0xA0 can be represented as decimal 160, octal 240 or binary 10100000)



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 2 of 6
(8,018 Views)

Roberto,

Have any idea how to do the above example if there are no delimiters in the hex string?

 

such "A0000BC0"

 

Thanks

Diego

0 Kudos
Message 3 of 6
(7,772 Views)

Supposing 'a' is your string, you could scan it in a loop for example this way:

 

  for (i = 0; i < strlen(a) / 2; i++) {
   Scan (a, "%s[i*w2]>%i[r16]", i * 2, &item);
  }

Look in the help for Scan function in the Formatting and I/O Library for an explanation of how the asterisk is used in this formula.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 4 of 6
(7,755 Views)
It may sound dumb, but first thing that comes to my mind is actually inserting the commas into the string first and then use the code Roberto posted. ------ int i; char a[32] = {"A0000BC0"}; char b[64] = {""}; for (i=0; i
S. Eren BALCI
IMESTEK
0 Kudos
Message 5 of 6
(7,754 Views)

Eren,

Interesting idea which I had not considered.

 

Roberto,

U rok ! Alot cleaner than my solution.

Thx

 

Diego

0 Kudos
Message 6 of 6
(7,729 Views)