create an UTF-8 string with BOM - objective-c

I'm using MD5 function and Base64 Encoding to generate a User Secret (used to login to data layer of the used API)
I did the code in javascript and it's fine, but in Objective C I'm strugling with the BOM
my code is:
NSString *str = [[NSString alloc]
initWithFormat:#"%#%#%#%d",
[auth uppercaseString],
[user uppercaseString],
[pwd uppercaseString],
totalDaysSince2000];
NSString *sourceString = [[NSString alloc] initWithFormat:#"%02x%02x%02x%#",
0xEF,
0xBB,
0xBF,
str];
NSString *strMd5 = [sourceString MD5];
NSData *sourceData = [strMd5 dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64EncodedString = [[sourceData base64EncodedString] autorelease];
using the code above I'm getting into the memory:
(source: balexandre.com)
witch is not what I really need...
I even tried with
"%c%c%c%#", (char)239, (char)187, (char)191, str
with no luck...
using UTF8String does not seam to append the BOM automatically as in C# :-(
How can I append the BOM correctly ?

Try embedding the BOM directly in the format string as escaped character literals:
NSString *sourceString = [[NSString alloc] initWithFormat:#"\357\273\277%#", str];

You might have to add the BOM to the NSData object, not the NSString. Something like this:
char BOM[] = {0xEF, 0xBB, 0xBF};
NSMutableData* data = [NSMutableData data];
[data appendBytes:BOM length:3];
[data appendData:[strMd5 dataUsingEncoding:NSUTF8StringEncoding]];

I had similar issue with Swift and opening CSV fime in Excel.
This question also helped me a lot.
Simple solution for swift with CSV file:
let BOM = "\u{FEFF}"
csvFile.append(BOM)

Related

Unable to decode gzipped Base64 NSString

So I'm trying to decode a base64 encoded string but I'm not having any luck.. decodedString is always null! I've tried for hours and totally out of ideas. Even tried third party libraries.
Code:
NSString *input = #"H4sIAAAAAAAEALWSTW/TQBCG/0q15wTN7Je9voU2RFFUFCnuASGE9mMcTB0n9QcSQvx3xm6QWsSFAzev53l3Zp9dcb8+HFab9fZOFGJ/3Ny+bbvdrt+MX56ePp9OeDyKhSi3TJWr+z0zEtAtQS1RligLowowbxSAzqWdyA/7NUMP+7tVueb12FPXi5vi5uOP+XubuJoHkpjQOghBW5c5Dzpo0sZj8FYaztVtP6MSeHG+XIMKpJQEQQcgnSobrHVRxco6qY2WcgoOdHq4NkKzENV7fyKOrnx3brneXNeHoavjY+PbxD+jb6hNfg7BQlCqh7Kesb+eNkfjZM65R/o+zxUpwxAzTyoinyL5hLnPJBkbK6h8YPTSnb9SHGbcGcgQfJDBWY0qhop5ixoJdRYUMZ6oedf44zzO5G1ftxxEqbSx4uenufVvr/9vile3MJndPbeaxPaD715YskswS4WlxAKhgCnASv+wKPMSTYHuuf7NN3XyA92ex3bgTdU/mB8vU/Iw+GHsOfpa2MvrFEGBTSEjrPiRVImCT8T7qJSMs5VJbPMX5JgDcAQDAAA=";
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:input options:0];
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
NSLog(#"Decode String Value: %#", decodedString);
The Base64 decoded output is not a string, it is binary data.
You are getting nil returned because it is not a UTF-8 string and arbitrary data is generally not valid UTF-8.
Decode using NSDataBase64DecodingIgnoreUnknownCharacters to avoid the decoder discarding non-Base-64 bytes.
NSData *decodedData = [[NSData alloc]initWithBase64EncodedString:input
options:NSDataBase64DecodingIgnoreUnknownCharacters];
Once you have decodedData, you can decompress the buffer with whatever function or library you decide on, then you can read the string. You may want to look at the answer here for an idea of how to decompress your data.
decodedData is not nil.
It's value is: <1f8b0800 ...
1f8b let me think it's a GZip.
Any hex data is not convertible as such into a String using UTF8 Encoding. UTF8 doesn't have value for each possible hex. There are non-valid combination, and so your final string is nil.
For instance, transform an UIImage into PNG HexData (using UIImagePNGRepresentation()) and try it to convert it into a NSString using NSUTF8StringEncoding. You'll see.
Using the code of https://github.com/nicklockwood/GZIP/ (first one I found):
NSData *unzippedData = [decodedData gunzippedData];
NSString *decodedString = [[NSString alloc] initWithData:unzippedData encoding:NSUTF8StringEncoding];
Output:
"MESSAGEID":"PgGCBnrKKsGuhqq_mm1gg","TIMESTAMP":"2019-03-12T12:53:05.3004826","TYPE":"UPDATE","users" : [{"userId":"8be21d1690bb46979a04b4e45a1ba625","insId":"20","operId":"30222e0b4b0e4df6b669c3cf69245422","itemUserId":15,"fName":"Aaron","lName":"Strickland","calendarId":0,"editTime":"2019-03-12T12:53:05.3815928","keyId":"ce71bc7ae3c145adad18a72e56cf0fab","projectId":"950710ab2b96413cbfd186141e147b3e","delFlag":0,"userPin":"123456"}],"keys" : [{"keyId":"ce71bc7ae3c145adad18a72e56cf0fab","projectId":"950710ab2b96413cbfd186141e147b3e","insId":"20","itemKeyId":15,"startTime":"2016-05-31T21:10:00","endTime":"2019-03-28T15:19:00","validateCount":13,"editTime":"2019-03-12T12:53:05.3815928","updateStatus":1,"delFlag":0,"calendarId":"b306db7e1f924fdebade3813dd596f5d"}]
Seems almost like JSON. I would have add "{" and "}" around it.
With a little of workaround:
NSMutableData *finalData = [[NSMutableData alloc] initWithData:[#"{" dataUsingEncoding:NSUTF8StringEncoding]];
[finalData appendData:unzippedData];
[finalData appendData:[#"}" dataUsingEncoding:NSUTF8StringEncoding]];
NSData *dictionary = [NSJSONSerialization JSONObjectWithData:finalData options:0 error:nil];
NSLog(#"dictionary: %#", dictionary);
It's up to you to know why the syntax is strange, but with that you a "usable" NSDictionary.

How to read Hex file in cocoa

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.

How to encode NSData raw bytes directly to UTF-16 (Unicode)?

Environment: iOS Xcode Objective-C Garbled Unicode UTF-16
My Question:
I got the file content from a remote file and then stored it into NSData object, then I want to convert the NSData to Unicode encoding (note: not UTF-8,UTF-16 will okay), the problem is that I do not know the original file content encoding, what can I do to convert the unknown raw bytes into Unicode (UTF-16)?
I have tried so many method, but no one successfully touch my target, the code:
NSData *chunk = [NSData dataWithContentsOfURL:url];
NSString* newStr = [[[NSString alloc] initWithData:chunk encoding:NSUTF16LittleEndianStringEncoding] autorelease];
NSData* d = [newStr dataUsingEncoding:NSUnicodeStringEncoding];
[d writeToFile:newFilePath atomically:YES];
got the file into NSData, encode it into Unicode and write to file.
NSString* converted_str = [[NSString alloc] initWithData:chunk encoding:NSUTF8StringEncoding];
NSData * d = [(id)CFStringCreateExternalRepresentation(NULL, (CFStringRef)converted_str, kCFStringEncodingUnicode, 0) autorelease];
[d writeToFile:newFilePath atomically:YES];
convert the NSData to NSString, then restore it into NSData for write to file, not work.
[converted_str writeToFile:newFilePath atomically:YES encoding:NSUnicodeStringEncoding error:&error];
write to file directly using NSString object, garbled.
CFStringRef converted_str = CFStringCreateWithBytes(NULL, [chunk bytes],[chunk length], kCFStringEncodingUTF8,false);
CFStringRef newStr = [chunk dataUsingEncoding:NSUTF8StringEncoding];
NSData * d = [(id)CFStringCreateExternalRepresentation(NULL, (CFStringRef)big5Str, NSUnicodeStringEncoding, 0) autorelease];
[d writeToFile:newFilePath atomically:YES];
Using cString to do the encoding work, but also failure.
char converted[([chunk length] + 1)];
[chunk getCString:converted maxLength:([chunk length] + 1) encoding: NSWindowsCP1251StringEncoding];
NSLog(#"converted:%s", converted);
NSString *converted_str = [NSString stringWithCString:converted encoding:NSUnicodeStringEncoding];
NSLog(#"converted_str:%#", converted_str);
I also using usedEncoding:&xxx to get the original file encoding, then try to decode it into string object using the returned encoding, ..., failure again.
NSStringEncoding encoding;
NSString *chunk = [[NSString alloc] initWithContentsOfURL:[rb fileURL] usedEncoding:&encoding error:&error];
...
I then try to using kCFStringEncodingUTF16 to get the Unicode encoded buffer, but failure.
NSStringEncoding encode = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF16);
NSString *chunk = [NSString stringWithContentsOfURL:url encoding:encode error:&error];
In the end, I want to try libiconv for iOS( iPhone,iPad), but the trouble is the encoding of source content is unknown, the usedEncoding:&xxx function can not always work.
I have no idea of this trouble ... :(
Is there someone meet the similar question, please give me some direction, any answer will be most appreciated , thank you.
Sincerely wishing you and your family happiness and health.

convert base64 decoded NSData to NSString

I'm trying to encode and decode base64 data. but while decoding the base64 data, it returns bunch of hex values, but i couldn't display or printout using NSlog to the original readable strings. The below code couldn't print anything, just empty.
Can anyone help ? thanks
>
>
NSString* msgEncoded = [[NSString alloc] initWithFormat:#"Q1NNKE1DTC9TTUEgUkNWL2FkbWluQHNldGVjcy5jb20gT1JHLyBUVkIvNDNkYzNlMzQwYWQ3Yzkp:"];
NSData* decoded = [[NSData alloc] initWithData:[self decodeBase64WithString:msgEncoded]];
NSString* plainString = [[NSString alloc]initWithData:decoded encoding:NSUTF8StringEncoding];
NSLog(#"\n Decoded string: %# \n", plainString );
There is a built in function in NSData
[data base64Encoding];
[data base64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength];
If you are still having issues, try out this library: https://github.com/l4u/NSData-Base64
use it like so:
#import "NSData+Base64.h"
NSData *someData //load your data from a file, url or photo as needed
NSData *file = [NSData dataWithContentsOfFile:#"mytextfile.txt"];
NSData *photo = UIImageJPEGRepresentation(self.photo.image,1);
//encode it
NSString *base64string = [photo base64EncodedString];
NSString *base64file = [file base64EncodedString];
//decode it
NSData *back = [NSData dataFromBase64String:base64string];
Try Google's GTMStringEncoding class. You'll need GTMDefines.h too.
GTMStringEncoding *coder = [GTMStringEncoding rfc4648Base64StringEncoding];
NSString *encodedBase64 = [coder encodeString:#"Mary had a little lamb"];
// will contain the original text
NSString *decodedText = [coder decodeString:encodedBase64];
To encode NSData* to NSString* and back to NSData*, use the encode: + decode: methods instead of encodeString: + decodeString:.
As a bonus you get a lot of additional useful encodings, such as the url-safe variant of Base64.

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