LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

What is the simple way to convert a string to an unsigned char arrary?

What is a simple way to convert a string to an unsigned char arrary? For exampel: From "3A446B33" to a[0]=3A,a[1]=44,a[2]=6B,a[3]="33".
 
Thanks!
Jacky
0 Kudos
Message 1 of 5
(4,051 Views)

Hello Jacky,

you should just use the formatting function: Fmt (a, "3A446B33"). It's in the Formatting and IO Library.

0 Kudos
Message 2 of 5
(4,049 Views)

I suggest you use this lines of code to correctly decode your original string (in hex format, I suppose) into an unsigned integer array: the format function is scanned two digits at a time, interpreted as an hex value and transferred into the unsinged int array elements. This code can be directly pasted into the interactive window and tested.

#include <formatio.h>
#include <ansi_c.h>
static char a[64];
static int  i;
static unsigned int ui[10];

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



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?
0 Kudos
Message 3 of 5
(4,043 Views)

Thanks, but if I change the data type of ui to unsinged char, how can I get the same function? because I will make ui as the input parameter of ComWrt (selCOM,ui,i).

Another question is about parameter pass in Teststand to a DLL module. I built a DLL with the output function of Message_Send_Read, see below. 

int DLLEXPORT Message_Send_Read(unsigned char Tx_Msg[1000], int delay_time,unsigned char Rx_Msg[1000])
{.....

 

ComWrt (selCOM,Tx_Msg,i+3); 
......

  ComRd (selCOM, Rx_Msg, buffer_length);
......

}

.When it is called by Teststand, which kind of values should I pass to Tx_Msg and Rx_Msg. I'm sure I can use the data type of arrary of numbers (unsinged 8-bit integer), but not sure whether I can use the data type of String (C String Buffer), like char[1024]?

Thanks for your help.

0 Kudos
Message 4 of 5
(4,014 Views)

To format into an unsigend char simply change the Scan instruction this way:

static unsigned char uch[10];

Scan (a, "%s[i*w2]>%i[r16]", i * 2, (int *)(uch + i));



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?
0 Kudos
Message 5 of 5
(4,005 Views)