I have some NSMutableData holding some ASCII code.
I would like to replace some bytes in a specific range with other bytes made up of an NSString:
NSString *myString = #"\r"
const char *myChar = [myString UTF8String];
[myData replaceBytesInRange:myRange withBytes:&myChar];
Now, when logging myData I get different values for the exchanged byte every time I run the App! Aren't I supposed to get <0D>?!
What am I doing wrong?
Related
I have an Image that is converted into NSData and which in-turn is converted into base64 encoded NSString. Now i have a service that accepts this base64 encoded string in only Byte Array(Java Supported). I tried different options but i am not able to convert the encoded string into Byte Array type. Can someone please help me on how to convert the encoded string into Byte Array(Java supported)?
Below is my answer for this. I came to know that the easiest way is to convert the image data into byte array directly without base64encoding the data.
NSError* error;
NSString* str=[[NSBundle mainBundle] pathForResource:#"black-circle" ofType:#"png"];
NSData* data= [NSData dataWithContentsOfFile:str options:NSDataReadingUncached error:&error];
NSInteger length=[data length];
const char* dataBytes=[data bytes];
Now you can traverse each byte using dataBytes pointer.
Hope this helps.
You can try out :
Use-[NSString UTF8String] which returns const char*.
or this link will help you :
Convert NSString into byte array iphone
We can get the NSdata from the NSStirng and then convert NSData into Byte Array. The easier way is to convert the captured image data into Byte Array using uint8_t. Below is the code snippet that does the trick.
NSData to Byte Array:-
NSData *data = UIImagePNGRepresentation(imageCaptured);
uint8_t byteArray[[data length]];
[data getBytes:&byteArray];
NSMutableString *byteArrayString = [[NSMutableString alloc]init];
for (int i = 0 ; i < sizeof(byteArray); i++) {
[byteArrayString appendString:[NSString stringWithFormat:#"%d,",byteArray[i]]];
}
// Removing last extra comma
[byteArrayString deleteCharactersInRange:NSMakeRange([byteArrayString length]-1, 1)];
I am reading my app directory like this
NSArray *pathss = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectorys = [pathss objectAtIndex:0];
NSError * error;
NSMutableArray * directoryContents = [[NSFileManager defaultManager]
contentsOfDirectoryAtPath:documentsDirectorys error:&error];
the output i get:
"Forms_formatted.pdf",
"fund con u\U0308u\U0308.pdf",
"hackermonthly-issue.pdf",
these are the files name. my question, how come i able to convert this name "fund con u\U0308u\U0308.pdf" to correct format. thanks in advance
Maybe this will help:
NSString *documentsDirectorys = [pathss objectAtIndex:0];
const char *documentsDirectoryCstring = [documentsDirectorys cStringUsingEncoding:NSASCIIStringEncoding];
EDIT:
const char *documentsDirectoryCstring = [documentsDirectorys cStringUsingEncoding:NSUTF8StringEncoding];
Convert the NSString to NSData with UTF-16 encoding, convert back to a NSString with UTF-8 encoding.
It is probably UTF-16 big endian with no BOM. The character is not ASCII.
The character u\U0308 is not ASCII, it is a Combining Diacritical Mark ̈ü, the character is also known as double dot above, umlaut, Greek dialytika and double derivative. The \U0308 puts the umlaut above the u character.
You can try this. NSLog use description method of NSArray to print NSArray object, which deals with unicode characters differently than the description on objects its contains such as NSString objects. Or you can loop through the array and NSLog each item.
NSLog(#"%#", [NSString stringWithCString:[[array description] cStringUsingEncoding:NSASCIIStringEncoding] encoding:NSNonLossyASCIIStringEncoding]);
I'm trying to save and load a custom file format in an iOS app. The format stores various data, but among it is the name a user entered for a map. I found that at random, as I'm trying to read this map name (stored as chars in the file), my NSData object will read garbage. Here is my code for reading the map name (start is already set to the correct start location):
NSData* data = ....;
uint mapNameLength;
char* mapNameChar;
NSString* mapNameString;
[data getBytes:&mapNameLength range:NSMakeRange(start, 4)];
start += 4;
mapNameChar = (char *)malloc(sizeof(char) * mapNameLength);
[data getBytes:mapNameChar range:NSMakeRange(start, mapNameLength)];
mapNameString = [NSString stringWithUTF8String:mapNameChar];
NSLog(#"mapNameLength: %u, mapNameChar: %s, Map name string: %#", mapNameLength, mapNameChar, mapNameString);
As you can see, I am reading the length of the name, then reading that many char values, then converting it into an NSString. Here is the output of the NSLog when it works (I just hit the keyboard a bunch to make a long name):
mapNameLength: 49, mapNameChar: kandfianeifniwbvuandivbwirngiwnrivnwrivnwidnviwdv, Map name string: kandfianeifniwbvuandivbwirngiwnrivnwrivnwidnviwdv
Here is the output of the NSLog when it doesn't work:
mapNameLength: 49, mapNameChar: kandfianeifniwbvuandivbwirngiwnrivnwrivnwidnviwdvfi?p?, Map name string: (null)
Those "?" are actually special characters that don't display here, so I added them. The first is "ETX" and the second is "SOH" in Sublime Text.
To create the file, this is what I am doing:
NSString* mapName = ....;
uint mapNameLength = (uint)mapName.length;
NSMutableData* data = ....;
//...
//Write file type and version here
//...
[data appendBytes:&mapNameLength length:4];
[data appendData:[mapName dataUsingEncoding:NSUTF8StringEncoding]];
//...
//Write other stuff
//...
NSString* path = [FileManager applicationDocumentsDirectory];
path = [path stringByAppendingFormat:#"/%#", fileName];
BOOL success = [data writeToFile:path options:NSDataWritingAtomic error:&error];
I've only written the file once, so I know the data is always the same. Why then, would my data object sometimes get random bytes from it?
I am not certain, however I don't think you are storing a terminating NUL character, so you need to add one to the buffer once you've read the string in:
mapNameChar = (char *)malloc(sizeof(char) * (mapNameLength + 1)); // Add +1
[data getBytes:mapNameChar range:NSMakeRange(start, mapNameLength)];
mapNameChar[mapNameLength] = '\0'; // Terminate with NUL
mapNameString = [NSString stringWithUTF8String:mapNameChar];
Better still forget about malloc() and the C-String and create the NSString directly from the NSData object:
mapNameString = [[NSString alloc] initWithBytes:(const char *)([data bytes]) + start
length:mapNameLength
encoding:NSUTF8StringEncoding];
In my application i have NSString that get String from the web:
高瀬 - 虎柄の毘沙門天
Now i want to copy this string to a local NSString in my Object so i wrote:
self.metaDataString = [NSString stringWithString:tempMetaDataString];
And now in metaDataString i have :
é«ç¬ - èæã®æ¯æ²é天
What can make this problem?
i tried this too:
self.metaDataString = [NSString stringWithUTF8String:[tempMetaDataString UTF8String]];
How i get tempMetaDataString:
NSMutableString *tempMetaDataString = [NSMutableString stringWithCapacity:0];
//This line i loop over the bytes array size
[tempMetaDataString appendFormat:#"%c", bytes[i]];
And this is the bytes array:
UInt8 bytes[kAQBufSize];
length = CFReadStreamRead(stream, bytes, kAQBufSize);
This line cannot work for multi-byte characters:
[tempMetaDataString appendFormat:#"%c", bytes[i]];
If you have a multi-byte character, this is going to split it up into individual ASCII characters (as you're seeing).
It's unclear from this code what bytes really is. Is the string of a fixed length, or is the string NULL terminated? If it's of a fixed length, then you want (assuming this is UTF8):
self.metaDataString = [[NSString alloc] initWithBytes:bytes
length:kAQBufSize
encoding:NSUTF8StringEncoding];
If this is a NULL terminated UTF8 string:
self.metaDataString = [NSString stringWithUTF8String:bytes];
If some other encoding (for example NSJapaneseEUCStringEncoding or NSShiftJISStringEncoding):
self.metaDataString = [NSString stringWithCString:bytes encoding:theEncoding];
I'm trying to get information from a SQLite file, and I when I run a query, the information is returned with ASCII encoding. I'm using the code below to put the returned information into a string.
[NSString stringWithCString:(char *)sqlite3_column_text(compiledStatement, 2) encoding:NSASCIIStringEncoding];
When I try to use UTF8 encoding to put the returned information into a string, it doesn't work. The code below is used in the app currently on the store.
[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
The string is null. How can I convert the SQLite file so that it is encoded with UTF8? The current version of the app on the store uses the latter code above, so I need to figure out how to convert the file, instead of changing the code in the app.
You can use const void *sqlite3_column_text16(sqlite3_stmt*, int iCol); with stringWithCharacters:length: (UTF16)
const unichar *text = sqlite3_column_text16(_sqlite_stmt, columnIndex);
if (text) {
NSUInteger length = sqlite3_column_bytes16(_sqlite_stmt, columnIndex)/sizeof(unichar);
article = [NSString stringWithCharacters:text length:length];
}
Try this :
char *title = (char *)sqlite3_column_text(compiledStatement, 2);
NSString *str = [NSString stringWithUTF8String:title];
Working fine for me !