LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Convert LStrHandle to C++ Char

Hello,

I use CIN and I have a problem.

I have a problem to use SPrintf with my C++ project. In my declaration, I wrote extern "C".

What is the solution to convert a Labview String (LStrHandle) to a C++ string (char[]).

Thanks for help.

Sam.
0 Kudos
Message 1 of 6
(7,856 Views)
> What is the solution to convert a Labview String (LStrHandle) to a C++
> string (char[]).
>

In extcode.h there is the definition

typedef struct {
int32 cnt; /* number of bytes that follow */
uChar str[1]; /* cnt bytes */
} LStr, *LStrPtr, **LStrHandle;

The string is a counted string since NULL termination doesn't work well
with binary strings. The char[] and uChar[] are compatible, and so you
can either copy cnt characters of the string to a C++ object from the
str field, or if you really know what you are doing, you can use the
pointer as a C++ string after adding a NULL to it. Its easiest to add
the NULL on the diagram before calling the CIN, but there are functions
to do it from the CIN.

Greg McKaskle
0 Kudos
Message 2 of 6
(7,856 Views)
Thank for your answer.

But I'm a beginning to C++ programmer and I don't understand fully the answer.

Do you have a code to replace :
" LStrHandle data;
char cmde[512]="";

SPrintf(cmde, "%H",data);"

Thanks

SAM.
0 Kudos
Message 3 of 6
(7,856 Views)
On Mon, 3 Mar 2003 09:45:26 -0600 (CST), midtisam wrote:

>Thank for your answer.
>
>But I'm a beginning to C++ programmer and I don't understand fully the
>answer.

Take a more close look at the struct in the previous post (extcode.h)
and follow and interpret the actual behaviour of a smart compiler
after typing in the character string "(*data)->"

>Do you have a code to replace :
>" LStrHandle data;
> char cmde[512]="";
>
> SPrintf(cmde, "%H",data);"
>
>Thanks
>
>SAM.

char cmde[512]="";
int i;
char* cmdd = (char*) malloc((*data)->cnt*sizeof(uChar)+1);
for (i=0; i<(*data)->cnt; ++i)
cmdd[i] = (*data)->str[i];
cmdd[(*data)->cnt]=0;
sprintf(cmde, "%H",cmdd);
free(cmdd);

Jac.
0 Kudos
Message 4 of 6
(7,856 Views)
Thanks Jac,

the idea are good but cmdd is char and (*data)->str[] is uChar. We have a problem to convert and the string cmde is not correct.
0 Kudos
Message 5 of 6
(7,856 Views)
Hi,

In extcode.h there are two macros for LStrHandle :

LHStrLen give the number of char and LHStrBuf give the char * ( str[0] )


LStrHandle a
LHStrLen(a) = (*a)->cnt
LHStrBuf(a) = (*a)->str[0]

Then you can use a printf with LHStrBuf

Bye , Eric
0 Kudos
Message 6 of 6
(7,856 Views)