Type conversion: wide-string to string not working
dbrady
Posts: 30
I'm trying to write a function that can accept either a char string or a widechar string as a parameter.
Here's some tests i've done just checking variables in debugger.
Conversion between single chars and widechars works fine but between strings it doesn't work at all. I can get it to work using CH_TO_WC but thats no good for the function i'm trying to write. Should this be working? Is it an error in the compiler or just a limitation? Looking at the if statement in my code above strings are converted to wide strings at some points so it is possible.
As a final note here's what it says about wide strings in the help file...
define_function myFunction(widechar wcString[])I want to be able to send a char string or widechar string to the function with the char string automatically being converted to a widechar string( all 8bit characters being converted to 16bit). However it doesn't seem to work or even compile.
Here's some tests i've done just checking variables in debugger.
define_variable integer i = 1; integer j = 0; char cChar1 = {'1'}; widechar wcChar1 = {666}; char cChar2 = {'1'}; widechar wcChar2 = {'a'}; char cString1[] = {'1','2','3','4','5'}; widechar wcString1[] = {'a','b','c','d','e'}; char cString2[] = {'1','2','3','4','5'}; widechar wcString2[] = {'a','b','c','d','e'}; define_function myFunction(widechar wc[]) { widechar c[5] c = wc; } define_program if(i) //Run Once { //myFunction(cString1); cChar1 = wcChar1; //Works - data loss as expected wcChar2 = cChar2; //Works //wcString1 = cString1; //Doesn't Work wont compile //cString2 = wcString2; //Doesn't Work wont compile if(wcString2 = 'abcde') //Works - Evaluates to true { j = 1; } //wcString2 = '12345'; //Doesn't Work wont compile i = 0; }
Conversion between single chars and widechars works fine but between strings it doesn't work at all. I can get it to work using CH_TO_WC but thats no good for the function i'm trying to write. Should this be working? Is it an error in the compiler or just a limitation? Looking at the if statement in my code above strings are converted to wide strings at some points so it is possible.
As a final note here's what it says about wide strings in the help file...
Wide Strings
A wide character string data type is provided for dealing with Unicode fonts, which use 16-bit character codes (used for many Far-Eastern fonts) instead of the standard 8-bit codes (used with most Western fonts). Here’s a syntax sample for a wide character string:
WIDECHAR WChar[40]
The statement above declares a wide character string containing 40 elements, for a total of 80 bytes. A wide character string can be used in the same manner as other character strings. It maintains a length field that can be set using SET_LENGTH_STRING and retrieved using LENGTH_STRING.
Example:
WIDECHAR StrExp[6]
INTEGER StrLen
StrExp = {STOP, 500, 'OFF', X}
StrLen = LENGTH_STRING(StrExp)
In the example above, if STOP is 2 and X is a wide character whose value is 1000, the string expression will evaluate to "2, 500, 79, 70, 70, 1000" and StrLen is 6. Each array element can now assume a value of up to 65,535 rather than the 255 limit imposed by the standard character string.
A CHAR string may be assigned or compared to a wide character string.
Example:
WChar = 'FFWD'
or
IF (WChar = 'REV')
{
(* statements *)
}
Each 8-bit character in the CHAR string is converted to 16-bit before the assignment or comparison operation is performed.
0
Comments
--John
wcString2 = '12345'; //Doesn't Work wont compile
would have to be wcString2 = _WC('12345') in order to compile. If you don't encase it in the _WC function then you'll get a dimension mismatch error.
--John
Thanks for the suggestion. I had tried that too(while also remembering to enable _WC preprocessor in preferences->netlinx compiler). However it doesn't accomplish what i want to do. I need the string to be converted to a wide string as a simple type conversion.
It works fine for a single char to widechar but not when they're strings/arrays?
Instead of this:
Try this:
And for this:
Try this:
You can TYPE_CAST if you want to get rid of the warnings when you stuff a widechar into a char.
HTH