热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

带有希腊字符的NSString到char*-NSStringtochar*withgreekcharacters

Iamusingthefollowingcodetostorethedataofastringinachar*.我使用以下代码将字符串的数据存储在char*中。

I am using the following code to store the data of a string in a char*.

我使用以下代码将字符串的数据存储在char *中。

 NSString *hotelName = [components[2] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
hotelInfo->hotelName = malloc(sizeof(char) * hotelName.length + 1);
strncpy(hotelInfo->hotelName, [hotelName UTF8String], hotelName.length + 1); 
NSLog(@"HOTEL NAME: %s",hotelInfo->hotelName);

The problem is with the Greek characters that are printed strangely. I have also tried to use another encoding (e.g NSWindowsCP1253StringEncoding -it crashes- )

问题在于希腊字符的打印奇怪。我也尝试过使用其他编码(例如NSWindowsCP1253StringEncoding -it crashes-)

I tried even that:

我甚至试过了:

hotelInfo->hotelName = (const char *)[hotelName cStringUsingEncoding:NSUnicodeStringEncoding]; 

but it also produces strange characters.

但它也会产生奇怪的角色。

What do I miss?

我错过了什么?

EDIT: After some suggestions I tried the following:

编辑:经过一些建议我尝试了以下内容:

if ([hotelName canBeConvertedToEncoding:NSWindowsCP1253StringEncoding]){
    const char *cHotelName = (const char *)[hotelName cStringUsingEncoding:NSWindowsCP1253StringEncoding];
    int bufSize = strlen(cHotelName) + 1;
    if (bufSize >0 ){
        hotelInfo->hotelName = malloc(sizeof(char) * bufSize);
        strncpy(hotelInfo->hotelName, [hotelName UTF8String], bufSize);
        NSLog(@"HOTEL NAME: %s",hotelInfo->hotelName);
    }
}else{
    NSLog(@"String cannot be encoded! Sorry! %@",hotelName);
    for (NSInteger charIdx=0; charIdxhotelName = x;
    }
    NSLog(@"HOTEL NAME: %s",hotelInfo->hotelName);
}

But still nothing!

但仍然没有!

1 个解决方案

#1


1  

First of all, it is not guaranteed that any NSString can be represented as a C character array (so-called C-String). The reason is that there is just a limited set of characters available. You should check if the string can be converted (by calling canBeConvertedToEncoding:).

首先,不能保证任何NSString都可以表示为C字符数组(所谓的C-String)。原因是只有一组有限的字符可供使用。您应该检查字符串是否可以转换(通过调用canBeConvertedToEncoding :)。

Secondly, when using the malloc and strncpy functions, they rely on the length of the C-String, not on the length of the NSString. So you should first get the C-String from the NSString, then get it's length (strlen), and use this value to the function calls:

其次,当使用malloc和strncpy函数时,它们依赖于C-String的长度,而不是NSString的长度。所以你应该首先从NSString获取C-String,然后获取它的长度(strlen),并将此值用于函数调用:

const char *cHotelName = (const char *)[hotelName cStringUsingEncoding:NSWindowsCP1253StringEncoding];
int bufSize = strlen(cHotelName) + 1;
hotelInfo->hotelName = malloc(sizeof(char) * bufSize);
strncpy(hotelInfo->hotelName, cHotelName, bufSize); 

推荐阅读
author-avatar
拍友2602921297
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有