Help - Search - Members - Calendar
Full Version: how convert string8 to ansi?
Hydrogenaudio Forums > Hosted Forums > foobar2000 > Development - (fb2k)
hehe163
how convert string8 to ansi?

pfc::string8 text;
char *ch;
...
pc->playback_format_title_ex(handle, NULL, text, formatobj, NULL, playback_control::display_level_all);

I want convert "text" to "ch"?
I use "text = string_os_from_utf8(text.get_ptr())" before foobar0.8,but in foobar0.9,how convert?

kyck-ling
maybe?
pfc::stringcvt::convert_utf8_to_wide
pfc::stringcvt::convert_wide_to_ansi
NEMO7538
sprintf()
kjoonlee
I'm not sure I understand the question.

7bit ASCII is valid UTF-8. Won't converting be unnecessary? Unless you're using extended codepages or something.
foosion
QUOTE(kyck-ling @ Jun 22 2006, 05:51) *
maybe?
pfc::stringcvt::convert_utf8_to_wide
pfc::stringcvt::convert_wide_to_ansi
Correct, but not the most straightforward thing to do. You can use pfc::stringcvt::string_*_from_utf8, for example pfc::stringcvt::string_ansi_from_utf8. Also, is there a particular reason why you need ANSI instead of UTF-16?


QUOTE(NEMO7538 @ Jun 22 2006, 07:06) *
sprintf()
That function does not convert string encodings.
hehe163
QUOTE(foosion @ Jun 22 2006, 03:51) *

Correct, but not the most straightforward thing to do. You can use pfc::stringcvt::string_*_from_utf8, for example pfc::stringcvt::string_ansi_from_utf8. Also, is there a particular reason why you need ANSI instead of UTF-16?


I use pfc::stringcvt::string_ansi_from_utf8 but failured;
I want send strings from myplugs to another program,and those strings have some chinese,eg:"c:\\中文",but the progam receive strings are disorderly strings,my codes as follows:
//send message
pfc::string8 text;
......
pc->playback_format_title_ex(handle, NULL, text, formatobj, NULL, playback_control::display_level_all);
pfc::stringcvt::string_os_from_utf8 os_class_name(text);
HWND hWnd;
COPYDATASTRUCT Cds;
hWnd=FindWindow(NULL,_T("another progame"));
if(hWnd!=NULL)
{
memset(&Cds,0x00,sizeof(COPYDATASTRUCT));
Cds.dwData =CmdSongFileName;
Cds.cbData =os_class_name.length()+1;
Cds.lpData =(LPVOID)os_class_name.get_ptr();
SendMessage(hWnd,WM_COPYDATA,0,(LPARAM)&Cds);
}

//receive message
COPYDATASTRUCT * pcds = (COPYDATASTRUCT *)Message.CopyDataStruct;
switch(pcds->dwData)
{
case CmdSongFileName:
wchar_t *tt=new wchar_t[pcds->cbData];
CopyMemory(tt,pcds->lpData,pcds->cbData);
delete []tt;
break;
}

Finally variable "tt" result are disorderly strings!why?how to solve?
david_dl
Probably because you are taking an ANSI string and reading it as unicode (wchar_t).
Try string_wide_from_utf8 instead, or read the string as char* in the 'recieve message' part. However i don't think ANSI supports asian characters so i'd recommend using unicode.
foosion
QUOTE(david_dl @ Jun 23 2006, 12:34) *
Probably because you are taking an ANSI string and reading it as unicode (wchar_t).
No, that is not the problem, since string_os_from_utf8 is just a typedef for string_wide_from_utf8 as long as your are compiling for a Unicode target (which would always be the case for a foobar2000 0.9 component). Nonetheless, it would be a good idea to explicitly use either ANSI or Unicode encoding.

The real problem is here:
QUOTE(hehe163 @ Jun 23 2006, 10:50) *

Cds.cbData =os_class_name.length()+1;
cbData contains the number of bytes you pass using WM_COPYDATA. However os_class_name.length() is the length in characters. You also have to pay attention in the reveicing code to allocate and copy the correct number of bytes.
hehe163
QUOTE(foosion @ Jun 23 2006, 07:47) *


The real problem is here:
QUOTE(hehe163 @ Jun 23 2006, 10:50) *

Cds.cbData =os_class_name.length()+1;
cbData contains the number of bytes you pass using WM_COPYDATA. However os_class_name.length() is the length in characters. You also have to pay attention in the reveicing code to allocate and copy the correct number of bytes.


Foosion,thank you very much!
I 'm going to test!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.