LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

byte array to string

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".

0 Kudos
Message 1 of 13
(8,220 Views)

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

Message Edited by Mert A. on 10-22-2008 06:45 PM
0 Kudos
Message 2 of 13
(8,217 Views)

Here is a simple code:

 

int num = 0x54;
char str[50] ;
 
Fmt (str, "%i\0", num) ;
printf (str);

 

Hope this helps, 

S. Eren BALCI
IMESTEK
0 Kudos
Message 3 of 13
(8,195 Views)
Unfortunately, if aml1138 wants the byte 0x54 represented as an ascii 'T', then using Fmt in that way is not going to work. Your code will result in the string "84" (which is the decimal value of 0x54).
0 Kudos
Message 4 of 13
(8,184 Views)

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

0 Kudos
Message 5 of 13
(8,177 Views)

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

0 Kudos
Message 6 of 13
(8,173 Views)

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.

0 Kudos
Message 7 of 13
(8,166 Views)

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);

}

0 Kudos
Message 8 of 13
(8,162 Views)
I did a cut and paste of the code and the array is empty after running it.  I can see the values go into each index but after the null termination the array is NULL.  Is this just a display issue and the values are there?
0 Kudos
Message 9 of 13
(8,154 Views)

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.

0 Kudos
Message 10 of 13
(8,145 Views)