NSString to ByteArray and back - objective-c

I am implementing an interface, which specifies writeBytes() and readBytes() on a file.
The data has to be transported as JSON.
For readBytes I do the following:
NSFileHandle readDataOfLength - to read from a file
NSString initWithData: encoding: - to specify a given encoding
NSString getBytes:buffer
put each buffer[i] into a JSON array for transport : [116,101,115,116] for example "test" as UTF-8
On the other hand writeBytes should be doing about the same:
Parse the JSON array to a NSArray
NSArray getObjects:buffer - the conversion up to this point is successful
NSString initWithBytes:buffer length: encoding: - is not working, the return value is null
NSData dataUsingEncoding:
NSFileHandle writeData
Apparently NSString initWithBytes cannot handle buffers with content such as [116,101,115,116]. Is there any other way to convert a NSString into a byte array and back ?
Thanks

Have you verified that the bytes within the array are a valid UTF-8 representation of a string? (http://en.wikipedia.org/wiki/UTF-8)
If not, you'll get back nil:
#import <Foundation/Foundation.h>
int
main()
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
char ary[] = { 116, 101, 115, 116, 255 };
NSString *s = [[NSString alloc] initWithBytes: ary length: 5 encoding: NSASCIIStringEncoding];
NSString *s2 = [[NSString alloc] initWithBytes: ary length: 5 encoding: NSUTF8StringEncoding];
NSLog(#"s: %#, s2: %#", s, s2);
[pool release];
}
When run, this prints:
2010-01-13 14:08:23.315 a.out[50653:903] s: testÿ, s2: (null)

Are you using the NSUnicodeStringEncoding encoding ? If so, then you may have to prefix your bytes with a valid B.O.M (See NSString reference, section "Interpreting UTF-16-encoded data").
Have you made a test with the NSASCIIStringEncoding to check that your code works for simple case ?

Related

Copying contents of uint8_t[] into an NSString

I have a uint_8[] array of characters and I'd like to convert it to an NSString but I'm getting NULL back. What's the proper way to convert between these two types?
// Defined else where as:
uint8_t someValue[8];
someValue is not NULL and contains some valid characters
I've tried:
NSLog(#"converted using CString: %#", [NSString stringWithCString:(char const *)someValue encoding:NSUTF8StringEncoding]);
as well as:
NSMutableData *data = [[NSMutableData alloc] init];
[data appendBytes:someValue length:sizeof(someValue)];
converted = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"converted using NSData: %#", converted);
Using:
[NSString stringWithCString:(char const *)someValue encoding:NSUTF8StringEncoding];
only works if there is a null terminator in the someValue array.
Your other solution doesn't work because sizeof(someValue) does not return the number of characters in the array, it returns the size of the uint8_t pointer.
You can use:
NSUInteger len = ... // the actual number of characters in someValue
NSString *str = [[NSString alloc] initWithBytes:someValue length:len encoding:NSUTF8StringEncoding];
Of course this requires that you know how many characters are really in the array.

Null result on Converting NSData to NSString

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.
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
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.
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];
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];
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];

Encode and Decode a string on Objective C

I use below code to encode and decode a string on objective C. The encoding is good, I debug and see that it throw a hash string when input is #"1". But when I try to decode this hash string, it return nil.
Please help me.
+(NSString *)encrypt: (NSString*) input
{
//Base64 Encoding
char base64Result[32];
size_t theResultLength = 32;
Base64EncodeData(input, 20, base64Result, &theResultLength);
NSData *theData = [NSData dataWithBytes:base64Result length:theResultLength];
NSString *base64EncodedResult = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];
NSString* decryptedString = [self decrypt:base64EncodedResult];
return [base64EncodedResult autorelease];
}
+ (NSString *) decrypt:(NSString*) input{
Byte inputData[[input lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];//prepare a Byte[]
[[input dataUsingEncoding:NSUTF8StringEncoding] getBytes:inputData];//get the pointer of the data
size_t inputDataSize = (size_t)[input length];
size_t outputDataSize = EstimateBas64DecodedDataSize(inputDataSize);//calculate the decoded data size
Byte outputData[outputDataSize];//prepare a Byte[] for the decoded data
Base64DecodeData(inputData, inputDataSize, outputData, &outputDataSize);//decode the data
NSData *theData = [[NSData alloc] initWithBytes:outputData length:outputDataSize];//create a NSData object from the decoded data
NSString *result = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];
return [result autorelease];
}
In you encoding method, you have to convert the input string to a byte buffer and feed that to Base64EncodeData:
NSData *inputData = [input dataUsingEncoding:NSUTF8StringEncoding];
Base64EncodeData([inputData bytes], [inputData length], base64Result, &theResultLength, NO);
(The NSString *input argument in the encoding method points to an Objective-C structure, not to a C string. So your encoding method seems to work. It encodes something, but not the input string. The decoding method then fails at
NSString *result = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];
because the decoded data does not contain valid UTF-8.)

NSString to uint8_t doesn't match

I'm trying to convert an NSString to uint8_t. The problem I'm having is that the NSString and the resulting uint8_t variables do not match. Here is some example code:
NSLog(#"Key now: %#", key);
NSData* keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
const uint8_t *plainBuffer = (const uint8_t*)[data bytes];
size_t plainBufferSize = strlen((char *) plainBuffer);
NSLog(#"Plain buffer: %s", plainBuffer);
NSData* testData = [[NSData alloc] initWithBytes: plainBuffer length:plainBufferSize];
NSString* testString = [[NSString alloc] initWithData: testData encoding: NSUTF8StringEncoding];
NSLog(#"Test string: %#", testString);
And example output:
Key now: 9iIWBpf5R6yu5pJ93l218RsMdWBLidXt
Plain buffer: 9iIWBpf5R6yu5pJ93l218RsMdWBLidXtMdWBLidXt
Test string: 9iIWBpf5R6yu5pJ93l218RsMdWBLidXtMdWBLidXt
Clearly its the NSData -> uint8_t step thats going wrong, but I don't know why!
You're using strlen() to get the size of an NSData*. That's not going to work. The NSData* isn't NUL-terminated. So you're getting garbage on the end.
Don't use strlen(). Just ask the NSData* for its size directly.
Alternatively, don't use NSData* at all and just ask for [key UTF8String]. That hands back a NUL-terminated const char *.

Convert NSData bytes to NSString?

I'm trying to use the BEncoding ObjC class to decode a .torrent file.
NSData *rawdata = [NSData dataWithContentsOfFile:#"/path/to/the.torrent"];
NSData *torrent = [BEncoding objectFromEncodedData:rawdata];
When I NSLog torrent I get the following:
{
announce = <68747470 3a2f2f74 6f727265 6e742e75 62756e74 752e636f 6d3a3639 36392f61 6e6e6f75 6e6365>;
comment = <5562756e 74752043 44207265 6c656173 65732e75 62756e74 752e636f 6d>;
"creation date" = 1225365524;
info = {
length = 732766208;
name = <7562756e 74752d38 2e31302d 6465736b 746f702d 69333836 2e69736f>;
"piece length" = 524288;
....
How do I convert the name into a NSString? I have tried..
NSData *info = [torrent valueForKey:#"info"];
NSData *name = [info valueForKey:#"name"];
unsigned char aBuffer[[name length]];
[name getBytes:aBuffer length:[name length]];
NSLog(#"File name: %s", aBuffer);
..which retrives the data, but seems to have additional unicode rubbish after it:
File name: ubuntu-8.10-desktop-i386.iso)
I have also tried (from here)..
NSString *secondtry = [NSString stringWithCharacters:[name bytes] length:[name length] / sizeof(unichar)];
..but this seems to return a bunch of random characters:
扵湵畴㠭ㄮⴰ敤歳潴⵰㍩㘸椮潳
The fact the first way (as mentioned in the Apple documentation) returns most of the data correctly, with some additional bytes makes me think it might be an error in the BEncoding library.. but my lack of knowledge about ObjC is more likely to be at fault..
That's an important point that should be re-emphasized I think. It turns out that,
NSString *content = [NSString stringWithUTF8String:[responseData bytes]];
is not the same as,
NSString *content = [[NSString alloc] initWithBytes:[responseData bytes]
length:[responseData length] encoding: NSUTF8StringEncoding];
the first expects a NULL terminated byte string, the second doesn't. In the above two cases content will be NULL in the first example if the byte string isn't correctly terminated.
How about
NSString *content = [[[NSString alloc] initWithData:myData
encoding:NSUTF8StringEncoding] autorelease];
NSData *torrent = [BEncoding objectFromEncodedData:rawdata];
When I NSLog torrent I get the following:
{
⋮
}
That would be an NSDictionary, then, not an NSData.
unsigned char aBuffer[[name length]];
[name getBytes:aBuffer length:[name length]];
NSLog(#"File name: %s", aBuffer);
..which retrives the data, but seems to have additional unicode rubbish after it:
File name: ubuntu-8.10-desktop-i386.iso)
No, it retrieved the filename just fine; you simply printed it incorrectly. %s takes a C string, which is null-terminated; the bytes of a data object are not null-terminated (they are just bytes, not necessarily characters in any encoding, and 0—which is null as a character—is a perfectly valid byte). You would have to allocate one more character, and set the last one in the array to 0:
size_t length = [name length] + 1;
unsigned char aBuffer[length];
[name getBytes:aBuffer length:length];
aBuffer[length - 1] = 0;
NSLog(#"File name: %s", aBuffer);
But null-terminating the data in an NSData object is wrong (except when you really do need a C string). I'll get to the right way in a moment.
I have also tried […]..
NSString *secondtry = [NSString stringWithCharacters:[name bytes] length:[name length] / sizeof(unichar)];
..but this seems to return random Chinese characters:
扵湵畴㠭ㄮⴰ敤歳潴⵰㍩㘸椮潳
That's because your bytes are UTF-8, which encodes one character in (usually) one byte.
unichar is, and stringWithCharacters:length: accepts, UTF-16. In that encoding, one character is (usually) two bytes. (Hence the division by sizeof(unichar): it divides the number of bytes by 2 to get the number of characters.)
So you said “here's some UTF-16 data”, and it went and made characters from every two bytes; each pair of bytes was supposed to be two characters, not one, so you got garbage (which turned out to be mostly CJK ideographs).
You answered your own question pretty well, except that stringWithUTF8String: is simpler than stringWithCString:encoding: for UTF-8-encoded strings.
However, when you have the length (as you do when you have an NSData), it is even easier—and more proper—to use initWithBytes:length:encoding:. It's easier because it does not require null-terminated data; it simply uses the length you already have. (Don't forget to release or autorelease it.)
A nice quick and dirty approach is to use NSString's stringWithFormat initializer to help you out. One of the less-often used features of string formatting is the ability to specify a mximum string length when outputting a string. Using this handy feature allows you to convert NSData into a string pretty easily:
NSData *myData = [self getDataFromSomewhere];
NSString *string = [NSString stringWithFormat:#"%.*s", [myData length], [myData bytes]];
If you want to output it to the log, it can be even easier:
NSLog(#"my Data: %.*s", [myData length], [myData bytes]);
Aha, the NSString method stringWithCString works correctly:
With the bencoding.h/.m files added to your project, the complete .m file:
#import <Foundation/Foundation.h>
#import "BEncoding.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Read raw file, and de-bencode
NSData *rawdata = [NSData dataWithContentsOfFile:#"/path/to/a.torrent"];
NSData *torrent = [BEncoding objectFromEncodedData:rawdata];
// Get the file name
NSData *infoData = [torrent valueForKey:#"info"];
NSData *nameData = [infoData valueForKey:#"name"];
NSString *filename = [NSString stringWithCString:[nameData bytes] encoding:NSUTF8StringEncoding];
NSLog(#"%#", filename);
[pool drain];
return 0;
}
..and the output:
ubuntu-8.10-desktop-i386.iso
In cases where I don't have control over the data being transformed into a string, such as reading from the network, I prefer to use NSString -initWithBytes:length:encoding: so that I'm not dependent upon having a NULL terminated string in order to get defined results. Note that Apple's documentation says if cString is not a NULL terminated string, that the results are undefined.
Use a category on NSData:
NSData+NSString.h
#interface NSData (NSString)
- (NSString *)toString;
#end
NSData+NSString.m
#import "NSData+NSString.h"
#implementation NSData (NSString)
- (NSString *)toString
{
Byte *dataPointer = (Byte *)[self bytes];
NSMutableString *result = [NSMutableString stringWithCapacity:0];
NSUInteger index;
for (index = 0; index < [self length]; index++)
{
[result appendFormat:#"0x%02x,", dataPointer[index]];
}
return result;
}
#end
Then just NSLog(#"Data is %#", [nsData toString])"
You can try this. Fine with me.
DLog(#"responeData: %#", [[[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSASCIIStringEncoding] autorelease]);
Sometimes you need to create Base64 encoded string from NSData. For instance, when you create a e-mail MIME. In this case use the following:
#import "NSData+Base64.h"
NSString *string = [data base64EncodedString];
This will work.
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];