I have 1 Hex file, i want to read this file and parse it to NSString.
I used this code to read hex file but it only prinf hex code in console:
-(void)readHexfile
{
NSData *data = [NSData dataWithContentsOfFile:#"path file"];
NSLog(#"Patch File: %#",data);
}
Do you have any suggestions? Thanks in advance
Use stringWithContentsOfFile:encoding:error: instead of dataWithContentsOfFile to read it as NSString.
There is no such a thing like a "hex file". Hex, or hexadecimal, is a numerical system that is quite suitable to display binary data in octets (8-bit bytes) in some way suitable for humans.
What you currently do is displaying the description of the NSData object onth the console in hex.
Some quick and dirty hack could be just to use the description of the NSData.
NSString *hexString = [data description];
This will create some overhead that you could strip of using string manipulation methods.
There are smater ways that may require more work.
On the contrary, if you are not interested in a hex representation then use stringWithContentsOfFile to read the file directly into an NSString object. You can then apply various encodings depending on how your file is actually encoded.
You'd read that using a NSScanner (convert your data to a string first using [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] (assuming your text is pure ASCII or UTF-8) or read it directly using +[NSString stringWithContentsOfFile:encoding:error:]). See also the String Programming Guide on how to use scanners.
Edit: So it seems you want to read a file with null-terminated strings. A naive and inefficient way to do that would be:
NSData *data = [NSData dataWithContentsOfFile:#"file.path"];
NSMutableArray *strings = [NSMutableArray array];
const char *rawData = [data bytes];
NSUInteger dataLength = [data length];
NSMutableData *currentString = [NSMutableData data];
for (NSUInteger i = 0; i < dataLength; i++) {
if (rawData[i] == 0) {
if ([currentString length] > 0) {
[strings addObject:[[[NSString alloc] initWithData:currentString encoding:NSUTF8StringEncoding] autorelease]];
}
[currentString release];
currentString = [NSMutableData data];
} else {
[currentString appendBytes:&rawData[i] length:1];
}
}
// Handle the last string if it wasn't null-terminated.
if ([currentString length] > 0) {
[strings addObject:[[[NSString alloc] initWithData:currentString encoding:NSUTF8StringEncoding] autorelease]];
}
// "strings" now is a list of strings.
Following is my code
NSString *res = [valueArray valueForKey:#"key"];
NSData *newdata=[res dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
res=[[NSString alloc] initWithData:newdata encoding:NSNonLossyASCIIStringEncoding];
nameTxt.text = [NSString stringWithFormat:#"%#",res]; // Assigning to textfield
this works properly.
But sometimes it returns NULL data, mostly it happens when string contents large data.
Anyone have idea why this is happening?
NSString *res = [valueArray valueForKey:#"key"];
That's funny: using a valueForKey: on a variable named ...Array. Rather seems to be a dictionary?
NSData *newdata = [res dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
creating a raw UTF8 encoded data from a string always works: No need for allowLossyConversion.
res = [[NSString alloc] initWithData:newdata encoding:NSNonLossyASCIIStringEncoding];
Converting the UTF8 encoded raw data to ASCII does only work if the UTF8 did not contain any characters out of the very restricted ASCII range. Otherwise nil is returned.
This seems to be the only reason for this obfuscated bit of code: filter out non-ASCII strings. Otherwise the conversion does not make the slightest sense.
I have a string that I want as a byte array. So far I have used NSData to do this:
NSString *message = #"testing";
NSData *messageData = [message dataUsingEncoding:NSUnicodeStringEncoding allowLossyConversion:YES];
NSUInteger dataLength = [messageData length];
Byte *byteData = (Byte*)malloc( dataLength );
memcpy( byteData, [messageData bytes], dataLength );
But, I know that NSString has the getBytes:maxLength:usedLength:encoding:options:range:remainingRange: method that would allow me to skip using NSData all together. My issue is, I don't know how to properly set all the parameters.
I assume the pointer array passed in has to be malloc'ed - but I'm not sure how to find how much memory to malloc. I know there is [NSString lengthOfBytesUsingEncoding:] and [NSString maximumLengthOfBytesUsingEncoding:] but I don't know if those are the methods I need to use and don't fully understand the difference between them. I assume this would be the same value given to maxLength. The rest of the parameters make sense from the documentation. Any help would be great. Thanks.
The difference between lengthOfBytesUsingEncoding: and maximumLengthOfBytesUsingEncoding: is that the former is exact but slow (O(n)) while the latter is fast (O(1)) but may return a considerably larger number of bytes than is actually needed. The only guarantee that maximumLengthOfBytesUsingEncoding: gives is that the return value will be large enough to contain the string's bytes.
Generally, your assumptions are correct. So the method should be used like this:
NSUInteger numberOfBytes = [message lengthOfBytesUsingEncoding:NSUnicodeStringEncoding];
void *buffer = malloc(numberOfBytes);
NSUInteger usedLength = 0;
NSRange range = NSMakeRange(0, [message length]);
BOOL result = [message getBytes:buffer maxLength:numberOfBytes usedLength:&usedLength encoding:NSUnicodeStringEncoding options:0 range:range remainingRange:NULL];
...
free(buffer);
The title pretty much sums it up. I have a hebrew-containing String used in a NSUrl:
NSString * urlS = #"http://irrelevanttoyourinterests/some.aspx?foo=bar&this=that&Text=תל אביב"
I would like to convert in into:
Text=%u05EA%u05DC%20%u05D0%u05d1%u05d9%u05d1
and then send it as a GET request.
I have tried many encoding methods unsuccessfully, and eventually I tried statically inserting the encoded string into the URL, quoted above.
NSURL *url1 = [NSURL URLWithString:urlS];
Then I use startAsyncLoad which you may be familiar with (here), but when the URL is constracted with the static Unicode string, nothing gets sent (checked with Wireshark), although if I use the following line before the startAsyncLoad it sends (wrongly encoded, of course).
urlS = [urlS stringByAddingPercentEscapesUsingEncoding:NSWindowsCP1254StringEncoding];
Thanks in advance.
The percent escapes that you have given as an example are Unicode code points. This type of encoding is non-standard, so I think it is unlikely that there exists a method already that can do this. You may have to roll your own, but it's not too difficult.
In a header (perhaps called NSString+NonStandardPercentEscapes.h) put the following:
#import <Foundation/Foundation.h>
#interface NSString (NonStandardPercentEscapes)
- (NSString *) stringByAddingNonStandardPercentEscapes;
#end
And, in a source file (perhaps called NSString+NonStandardPercentEscapes.m) put the following:
#import "NSString+NonStandardPercentEscapes.h"
#implementation NSString (NonStandardPercentEscapes)
- (NSString *) stringByAddingNonStandardPercentEscapes
{
NSCharacterSet *mustEscape = [NSCharacterSet characterSetWithCharactersInString:#"<>~\"{}|\\-`^% "];
NSMutableString *result = [[NSMutableString alloc] init];
NSUInteger length = [self length];
unichar buffer[length];
[self getCharacters:buffer range:NSMakeRange(0, length)];
for (NSUInteger i = 0; i < length; i++)
{
if ([mustEscape characterIsMember:buffer[i]])
[result appendFormat:#"%%%02hhx", buffer[i]];
else if (buffer[i] > 0xFF)
[result appendFormat:#"%%u%04hx", buffer[i]];
else if (!isascii((int)buffer[i]))
[result appendFormat:#"%%%02hhx", buffer[i]];
else
[result appendFormat:#"%c", buffer[i]];
}
// return the mutable version, nobody will know unless they check the class
return [result autorelease];
// alternatively, you can force the result to be immutable
NSString *immutable = [[result copy] autorelease];
[result release];
return immutable;
}
#end
Then, wherever you need to encode your Hebrew string, you can do the following (as long as your source file includes the above header):
NSString * urlS = #"http://irrelevanttoyourinterests/some.aspx?foo=bar&this=that&Text=תל אביב";
urlS = [urlS stringByAddingNonStandardPercentEscapes];
NSUrl *url1 = [NSURL URLWithString:urlS];
Disclaimer:
I have no idea what characters need to be escaped and at what point the escaping should start (this just encodes the entire URL which is probably not what you want), but the above code should at least get you under way.
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];