Home AMX User Forum NetLinx Studio

Type conversion: wide-string to string not working

I'm trying to write a function that can accept either a char string or a widechar string as a parameter.
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.

Comments

  • Just glancing at your post and about to run out but string literals have to be encased in the _wc() macro. Here's the example from the helpfile: WIDECHAR wcData[] = wc(‘Unicode String’). Not sure if that's what you need to do here, but at first glance that appears to be missing.


    --John
  • I have a similar question out in dealing with widechar and unicode, so I'm certainly no expert. Most of the assignments look illegal like going from CHAR to a WIDECHAR or the other way around and will probably get you a type conversion error. One thing I did figure out is that you have to load the include file 'UnicodeLib.axi' (INCLUDE 'UnicodeLib.axi') in order to gain access to some of the unicode functions; and the following line

    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
  • dbradydbrady Posts: 30
    Hi 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?
  • Joe HebertJoe Hebert Posts: 2,159
    I can?t explain why the simple conversion method doesn?t work for arrays but if you can accept the assistance of a FOR loop you could use the following as a workaround.

    Instead of this:
    //wcString1 = cString1;  //Doesn't Work wont compile
    

    Try this:
    FOR (x=1; x<=LENGTH_ARRAY(cString1); x++) {
       wcString1[x] = cString1[x];  //Seems to work and will compile
    }
    SET_LENGTH_STRING(wcString1,LENGTH_ARRAY(cString1))
    

    And for this:
    //cString2 = wcString2;  //Doesn't Work wont compile
    

    Try this:
    FOR (x=1; x<=LENGTH_ARRAY(wcString2); x++) {
       cString2[x] = wcString2[x];  //Seems to work and will compile
    }
    SET_LENGTH_STRING(cString2,LENGTH_ARRAY(wcString2))
    

    You can TYPE_CAST if you want to get rid of the warnings when you stuff a widechar into a char.

    HTH
Sign In or Register to comment.