|
#1
| |||
| |||
| convert a utf16 to utf8 to use in fopen Hi, i am converting a ascii to utf16 using following code. void asciitounicode(unsigned short *unicode,char *str) { char *tmpstr; unsigned short *tmpunicode = unicode; tmpstr = (char *)str; while( *tmpstr!= '\0' ) { *tmpunicode = (unsigned short)*tmpstr; tmpstr++; tmpunicode ++; } *tmpstr= '\0'; } now a want to convert the utf16 to utf8 so that it can be used in fopen which supports utf8 string. thanks. |
|
#2
| |||
| |||
| i want to open a file in korean name using fopen i want to open a file in korean name using fopen. so i am converting it to utf16 .Then i want to convert utf16 to utf8 so i can use it in fopen. Do anyone have a sample code for this? |
|
#3
| ||||
| ||||
| If you're using Cocoa it's fairly easy with NSString. Just load the UTF16 data in using -initWithBytes:length:encoding: (or perhaps -initWithCString:encoding:) and then get a UTF8 version by calling UTF8String on the result. |
|
#4
| |||
| |||
| const char * filepath = "/Volumes/TigerNew/abcd"; unsigned short *unifilepath; asciitounicode(unifilepath,filepath); NSString *nstr = [[NSString alloc] initWithBytes:unifilepath length:length encoding:NSUTF8StringEncoding]; fopen([nstr UTF8String],"r"); Will the above code work? |
|
#5
| ||||
| ||||
| If asciitounicode() returns UTF16 data, you will need to pass UTF16 string encoding to initWithBytes. I'm not sure whether you should use NSUnicodeStringEncoding, NSUTF16BigEndianStringEncoding or NSUTF16LittleEndianStringEncoding, since I don't know exactly what asciitounicode() does. Aside from that, yeah, that looks about right. |
|
#6
| |||
| |||
| thank you for the information. is there any api to convert a ascii string directly to utf16 or utf8? |
|
#7
| ||||
| ||||
| UTF8 is backwards-compatible with ASCII. That means any ASCII string is valid as a UTF8 string, so there is no need to convert it. You can also use those same NSString methods I mentioned to convert from just about any text encoding to just about any other. Check the NSString documentation for more info on the available string encodings. |