NI Home > Community > NI Discussion Forums

LabWindows/CVI

Showing results for 
Search instead for 
Do you mean 
Reply
Member
Joseph.han
Posts: 5
0 Kudos

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.

Proven Zealot
RobertoBozzolo
Posts: 5,259

Re: 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

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 Zone Community
________________________________________
If I have helped you, why not giving me a kudos?
Member
SpidyMan
Posts: 34
0 Kudos

Re: 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 - Roberto question

Roberto,

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

 

such "A0000BC0"

 

Thanks

Diego

Proven Zealot
RobertoBozzolo
Posts: 5,259
0 Kudos

Re: 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 - Roberto question

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 Zone Community
________________________________________
If I have helped you, why not giving me a kudos?
Active Participant
ebalci
Posts: 721
0 Kudos

Re: 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 - Roberto question

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
www.aselsan.com.tr
Member
SpidyMan
Posts: 34
0 Kudos

Re: 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 - Roberto question

Eren,

Interesting idea which I had not considered.

 

Roberto,

U rok ! Alot cleaner than my solution.

Thx

 

Diego