10-22-2008 05:36 PM
Hi,
Is the the equivilant function in CVI as the LabVIEW function byte array to string?
I want to take a hex number say 54 and load the string with "T".
10-22-2008 06:43 PM - edited 10-22-2008 06:45 PM
This depends on what exactly you have to start with. Regarding your example, if 0x54 is the first value in an array of bytes (char or unsigned char), then the array is a C string! There's no conversion necessary:
char myArray[] = { 0x54, 0x68, 0x65, 0x0 }; // myArray is the string "The"
Note that a C string must have an extra 0 byte (null terminator) at the end.
If, on the other hand, you have an array of some larger data type (short, unsigned, int, or combinations thereof), then you would have to copy each element one-by-one (and add the extra 0 at the end):
for (i = 0; i < lengthOfArray; ++i)
myString[i] = (char)myArray[i];
myString[i] = 0;
Another possibility is that your hex numbers are themselves represented as strings (i.e. "54" ), which would be the case if you've just read a bunch of text out of a file. In this case, you have to go element-by-element (just like in the previous example), but you must use a formatting function (like strtol, atoi, or sscanf) to convert each number-string into an actual byte.
Hope this helps.
Mert A.
National Instruments
10-23-2008 08:13 AM
Here is a simple code:
int num = 0x54;
char str[50] ;
Fmt (str, "%i\0", num) ;
printf (str);
Hope this helps,
10-23-2008 11:03 AM
10-23-2008 12:08 PM
Yeah, None of these work the way I want.
here's the LabVIEW code I'm trying to convert.
Okay, well I was going to put up the labview diagram here but it's next to impossible.
So, again no help from national.
And they wonder way I don't renew the SS plan
10-23-2008 12:23 PM
I'd be happy to help you, if you want help. I found the byte array to string VI in LabVIEW, and it takes an unsigned byte array as input and outputs a string. If you're converting some VI to C code, then presumably you're representing that input as a char (or unsigned char) array. As I said in my first reply to you, if you have an array of bytes, then you already have a C string. You don't have to call any function to do a conversion. You just need to be sure that the byte array is terminated with a 0.
Let me know how you want it to work, and I'll try to get you there.
Mert A.
National Instruments
10-23-2008 01:09 PM
OK, I have a number 4096 (x1000)
I convert and use the split number vi for get the High and low bytes - 10 and 00.
I place the bytes into an array
then use the byte array to string.vi to covert the values to the ascii characters.
I can't seem to duplicat this in CVI.
10-23-2008 01:18 PM
How about something like this:
void PrintBytesAsCString(unsigned short number)
{
// Split the low and high bytes by masking the 2-byte integer
unsigned char high = (number & 0xFF00) >> 8;
unsigned char low = number & 0x00FF;
// Place the bytes in the array
unsigned char byteArray[3];
byteArray[0] = high;
byteArray[1] = low;
// Null-terminate the array
byteArray[2] = 0;
// Ta-da, it's a C string.
printf("%s", byteArray);
}
10-23-2008 01:43 PM
10-23-2008 02:40 PM
If you passed in 4096, then there would not be any visible characters. The high byte of 4096 is 0x10, which is the ASCII code for "data link escape" and the low byte, 0x0, is just the null byte. Most of the visible ASCII characters are on the range 0x20 to 0x7E. You should get the same non-visible string from your LabVIEW code.