作者:拍友2602921297 | 来源:互联网 | 2023-07-17 17:08
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 个解决方案