image
image

Go Back   macosx.com > Design, Media, Programming & Scripting > Software Programming & Web Scripting

Reply
 
Thread Tools
  #1  
Old May 15th, 2008, 01:53 AM
Registered User
 
Join Date: Mar 2007
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
yogish is on a distinguished road
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.
Reply With Quote
  #2  
Old May 17th, 2008, 06:31 AM
Registered User
 
Join Date: Mar 2007
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
yogish is on a distinguished road
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?
Reply With Quote
  #3  
Old May 17th, 2008, 07:32 AM
Mikuro's Avatar
Crotchety UI Nitpicker
 
Join Date: Mar 2005
Posts: 2,505
Thanks: 4
Thanked 15 Times in 14 Posts
Mikuro is on a distinguished road
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.
__________________
Mac mini — 1.25GHz G4, 1GB RAM — OS 10.5.5

Useful programs: Privoxy, Butler, ffmpegX, VLC, Perian, Tofu, Wcalc
Reply With Quote
  #4  
Old May 17th, 2008, 10:00 AM
Registered User
 
Join Date: Mar 2007
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
yogish is on a distinguished road
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?
Reply With Quote
  #5  
Old May 17th, 2008, 11:57 AM
Mikuro's Avatar
Crotchety UI Nitpicker
 
Join Date: Mar 2005
Posts: 2,505
Thanks: 4
Thanked 15 Times in 14 Posts
Mikuro is on a distinguished road
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.
__________________
Mac mini — 1.25GHz G4, 1GB RAM — OS 10.5.5

Useful programs: Privoxy, Butler, ffmpegX, VLC, Perian, Tofu, Wcalc
Reply With Quote
  #6  
Old May 19th, 2008, 01:01 AM
Registered User
 
Join Date: Mar 2007
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
yogish is on a distinguished road
thank you for the information.
is there any api to convert a ascii string directly to utf16 or utf8?
Reply With Quote
  #7  
Old May 19th, 2008, 05:14 AM
Mikuro's Avatar
Crotchety UI Nitpicker
 
Join Date: Mar 2005
Posts: 2,505
Thanks: 4
Thanked 15 Times in 14 Posts
Mikuro is on a distinguished road
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.
__________________
Mac mini — 1.25GHz G4, 1GB RAM — OS 10.5.5

Useful programs: Privoxy, Butler, ffmpegX, VLC, Perian, Tofu, Wcalc
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Forum Jump


All times are GMT -5. The time now is 11:01 PM.


Mac Support® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2000-2008 DigitalCrowd, Inc.