From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

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,066 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,062 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,816 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,799 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,798 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,773 Views)