问题
I am facing a problem when converting NSData
to NSString
. I'm using UTF8Enconding
but the result is null
!!
Here is the data I receive <100226ab c0a8010b 00000000 00000000>
it must be either 192.168.1.11
or 192.168.1.17
.
This is the method I use to convert :
NSString *ipAddress = [[NSString alloc] initWithData:address encoding:NSUTF8StringEncoding];
Is there anything wrong?!
By the way, This the did receive data
delegate of GCDAsyncUdpSocket
library.
回答1:
From the documentation of GCDAsyncUdpSocket
:
The
localAddress
method returns a sockaddr structure wrapped in a NSData object.
The following code unwraps the data to a sockaddr structure and converts the IP address to a NSString
. It works with IPv4 and IPv6 addresses.
#include <sys/socket.h>
#include <netdb.h>
NSData *data = ...; // your data
NSLog(@"data = %@", data);
// Copy data to a "sockaddr_storage" structure.
struct sockaddr_storage sa;
socklen_t salen = sizeof(sa);
[data getBytes:&sa length:salen];
// Get host from socket address as C string:
char host[NI_MAXHOST];
getnameinfo((struct sockaddr *)&sa, salen, host, sizeof(host), NULL, 0, NI_NUMERICHOST);
// Convert C string to NSString:
NSString *ipAddress = [[NSString alloc] initWithBytes:host length:strlen(host) encoding:NSUTF8StringEncoding];
NSLog(@"strAddr = %@", ipAddress);
Output:
data = <100226ab c0a8010b 00000000 00000000>
strAddr = 192.168.1.11
回答2:
This is not a string response. This is binary data. If you consider <100226ab c0a8010b 00000000 00000000>
, look at the coa8010b
: c0
in hex is equal to 192 in decimal, a8
= 168, 01
= 1, and 0b
= 11. In short, this is a binary representation, not a string representation, of 192.168.1.11.
You may want to more carefully examine the GCDAsyncUdpSocket
documentation for the nature of response you should get, as it's apparently binary data, not a string.
回答3:
I guess there is problem with the encoding . I have also faced similar issue and solved by:
NSString *responseString =[[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUnicodeStringEncoding];
回答4:
Found a much quicker way to do it, using inet_ntoa()
:
//Get the bytes from the data and cast it to the correct struct
struct sockaddr_in *addr = (struct sockaddr_in *)[address bytes];
//inet_ntoa converts from the binary format to a C string
NSString *IP = [NSString stringWithCString:inet_ntoa(addr->sin_addr) encoding:NSASCIIStringEncoding];
回答5:
First try with other encoding formats available
1. NSASCIIStringEncoding
2. NSMacOSRomanStringEncoding
3. NSShiftJISStringEncoding.
even if it not works,try like the following ways
1. NSData *data = [NSData dataWithContentsOfURL:URL];
// Assuming data is in UTF8.
NSString *string = [NSString stringWithUTF8String:[data bytes]];
2. This is like as you done.
// if data is in another encoding, for example ISO-8859-1
NSString *string = [[NSString alloc]
initWithData:data encoding: NSISOLatin1StringEncoding];
来源:https://stackoverflow.com/questions/14037129/null-result-on-converting-nsdata-to-nsstring