Friday, January 10, 2014

udp receive data display Chinese garbled, we help guide the guide, thank you

Written in C language UDP communication structure, the received data exists char *, passed through the outer layer of the callback function using
If the received data to Chinese when, NSLog print garbled
Solution 1: Use c language interface to iconv conversion encoding (GBK-UTF8)
Use the same c linux library receives data through iconv transcoding, you can get the normal Chinese display
But the display is not normal in ios, and still garbled
Solution 2: Use the online examples, first char * convert NSData, through transcoding
NSData * data = [NSData dataWithBytes: char * a, strlen (a)];
NSStringEncoding gbk = CFStringConverNSStringEncodingToEncoding (kCFStringEncodingGB_2312_80);
NSString * str = [[NSString alloc] initWithBytes: [data bytes] length: [data length] encoding: NSUTF8StringEncoding];
Or garbled

Use
NSString * str1 = [NSString stringWithCString: char * A encoding: gbk];
const char * str2 = [str1 cStringUsingEncoding: NSUTF8StringEncoding];
NSLog (@ "str2:% s", str2); / / result is garbled

The char * Chinese online conversion data test
char testStr1 [20] ;/ / = "how transcoding";
memset (testStr1, 0x0, sizeof (testStr1));
strcpy (testStr1, "how transcoding");
NSLog (@ "testStr1:% s", testStr1);
NSString * sv = [[NSString alloc] initWithCString: testStr1 encoding: gbk];
NSLog (@ "sv:% @", sv) ;/ / this method can not display Chinese
normal in my project

Reply:
Unclear ah!!!!!!
Reply:
Nobody knows?
Reply:
Before the server sends to convert the data into UTF8 encoding try
Reply:
reference to the third floor hslinux reply:
before the server sends to convert the data into UTF8 encoding try


Server is not that we do not modify the server's encoding format, now only after receiving transcoding, but tried several ways is a failure
Reply:
Hexadecimal value of a stream of bytes of data sent some out, I try it.
Is the hexadecimal value of a piece of data with the following format impress:
ff 09 12 32 34

Then the corresponding characters are actually impress.
Reply:
Has been resolved, use iconv NSString by the use of initWithCString encoding GBK after turn UTF8: UTF8 you can normally show

No comments:

Post a Comment