www.digitalmars.com         C & C++   DMDScript  

D - converting wide to c

reply Lewis <dethbomb hotmail.com> writes:
ok i looked thru the string functions an found toCharz() to convert a wide 
string to a null terminated string, but when i tried to use it it told me it 
wasnt appreciated :(  (depreciated lol) so then i of course i tried to write my 
own an failed miserably... would anyone care to give me a hint on how to make 
this work? or is there another function i can use somewhere... thanks

//Convert a wide string to a 'c' string
char[] WideToAscii(wchar[] WideArr) {
	
	int i = 0;
	int Count = 0;
	char[] CharArr;
	
	i.init;
	Count.init;
	
	if ( WideArr.length > 0 )  	{
		
            CharArr.length = (WideArr.length / 2) + 1;
	
             while ( i < WideArr.length )	{
	        CharArr[Count] = (char) WideArr[i];
	        i += 2;
	        Count += 1;
	    }

	}

   CharArr[CharArr.length - 1] = (char)r"0";
   return CharArr;

  }

:) yes i have a long way to go lol
Dec 18 2003
parent reply "Walter" <walter digitalmars.com> writes:
Check out std.utf.toUTF8(wchar[]).

"Lewis" <dethbomb hotmail.com> wrote in message
news:brtfti$20j8$1 digitaldaemon.com...
 ok i looked thru the string functions an found toCharz() to convert a wide
 string to a null terminated string, but when i tried to use it it told me

 wasnt appreciated :(  (depreciated lol) so then i of course i tried to

 own an failed miserably... would anyone care to give me a hint on how to

 this work? or is there another function i can use somewhere... thanks

 //Convert a wide string to a 'c' string
 char[] WideToAscii(wchar[] WideArr) {

 int i = 0;
 int Count = 0;
 char[] CharArr;

 i.init;
 Count.init;

 if ( WideArr.length > 0 )  {

             CharArr.length = (WideArr.length / 2) + 1;

              while ( i < WideArr.length ) {
         CharArr[Count] = (char) WideArr[i];
         i += 2;
         Count += 1;
     }

 }

    CharArr[CharArr.length - 1] = (char)r"0";
    return CharArr;

   }

 :) yes i have a long way to go lol

Dec 18 2003
parent reply Lewis <dethbomb hotmail.com> writes:
Walter wrote:

 Check out std.utf.toUTF8(wchar[]).
 

thanks walter, your the man!
Dec 18 2003
parent reply Lewis <dethbomb hotmail.com> writes:
wow i can see a bad habit starting, but how is it possible to have 3 functions 
with the same name? how do i specify which one to use (it just wants to use the 
first one and i get a type mismatch compile error) Do i have to comment out the 
ones that i dont want to use?

sorry if im being a pain
Dec 18 2003
next sibling parent reply J C Calvarese <jcc7 cox.net> writes:
Lewis wrote:
 wow i can see a bad habit starting, but how is it possible to have 3 
 functions with the same name? 

(wchar[]), (dchar[]).
 how do i specify which one to use (it just 
 wants to use the first one and i get a type mismatch compile error) Do i 
 have to comment out the ones that i dont want to use?

I'm not sure what the problem is. My blind guess is that you need to use a cast: toUTF8(cast(wchar[]) "your string") or toUTF8(cast(wchar[]) str) (By the way, it does help if you include a snippet of code. Sometimes, the solution is obvious if we see it.)
 
 sorry if im being a pain

recently. :) Justin
Dec 18 2003
parent Lewis <dethbomb hotmail.com> writes:
J C Calvarese wrote:

 Lewis wrote:
 
 wow i can see a bad habit starting, but how is it possible to have 3 
 functions with the same name? 

It's okay. They have 3 sets different argument litss: (char[]), (wchar[]), (dchar[]).
 how do i specify which one to use (it just wants to use the first one 
 and i get a type mismatch compile error) Do i have to comment out the 
 ones that i dont want to use?

No. You shouldn't have to comment out anything in phobos. I'm not sure what the problem is. My blind guess is that you need to use a cast: toUTF8(cast(wchar[]) "your string") or toUTF8(cast(wchar[]) str) (By the way, it does help if you include a snippet of code. Sometimes, the solution is obvious if we see it.)
 sorry if im being a pain

Don't worry about it. We've all starting programming in D fairly recently. :) Justin

Walter wrote:
 It picks one based on the type of the argument you supply to it.

wow, now these are features i like! Thanks for the help guys.
Dec 18 2003
prev sibling parent "Walter" <walter digitalmars.com> writes:
It picks one based on the type of the argument you supply to it.

"Lewis" <dethbomb hotmail.com> wrote in message
news:brtv0l$2mvh$1 digitaldaemon.com...
 wow i can see a bad habit starting, but how is it possible to have 3

 with the same name? how do i specify which one to use (it just wants to

 first one and i get a type mismatch compile error) Do i have to comment

 ones that i dont want to use?

 sorry if im being a pain

Dec 18 2003