Want to convert String to Byte in iphone - iphone

How can we convert string to byte array in iPhone?
Please help I am new to iphone

If you want it in NSData then use:
NSData* bytes = [str dataUsingEncoding:NSUTF8StringEncoding];
If you want a char array then use:
[str getCString:chrBuff maxLength:[str length] encoding:NSASCIIStringEncoding];

Related

Saving a uint8_t ciphertext into DataModel

I am building a simple iphone (SDK6.1) application that encrypts some user's notes, stores the into a database and when user enters a password (does not need to be encrypted) it will decrypt his notes and show them to him.
For the database i am using Core Data (.xcdatamodel). The encrypted text at the moment is declared as String in the data model and in the Notes.h fil,e it is declared as NSString.
For the Encryption i am using apple's sample code from CryptoExercise which works perfectly.
The problem is that when i try to save the encrypted text in the database and then decrypt it i am not getting the desired results.. basically i am getting an empty string back.
Obviously i am using the following code to convert from uint8_t to NSString so i can store it into the data model and i understand that this is my main problem.
uint8_t *cipherBuffer = NULL;
SecKey *encrypt = [[SecKey alloc]init];
NSString *et = [[NSString alloc]init];
[encrypt generateKeyPairRSA];
// Encrypt the plain text
cipherBuffer = [encrypt Encryption:plainTextField.text];
// Convert uint8_t to NSString
NSMutableData *data = [[NSMutableData alloc]init];
[data appendBytes:cipherBuffer length:strlen((char*)cipherBuffer)+1];
NSString *string = [[NSString alloc]initWithData:data encoding: NSASCIIStringEncoding];
// Save to Data Model
[self.currentNote setEncryptedText:string];
[self.delegate addNewNoteViewControllerDidSave];
// Retrieve encrypted text from database
et = [self.currentNote encryptedText];
// Convert back to uint_8
NSData *someData = [et dataUsingEncoding:NSUTF8StringEncoding];
const void *bytes = [someData bytes];
uint8_t *crypto_data = (uint8_t*)bytes;
// Decrypt Data
[encrypt Decryption:crypto_data];
As i said before i understand that converting uint8_t is the main problem here and i would like to know which is the correct way to do this?
Is it possible with Data Model at all, or should i go to SQLite??
You cannot convert arbitrary bytes sequences to NSString and back like that. For example,
if data contains the single byte 128 (hex 0x80), then
NSString *string = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
creates a string with one Unicode character U+0080. When you convert this back to NSData with
NSData *d1 = [string dataUsingEncoding:NSUTF8StringEncoding];
then d1 will contain the bytes 0xC2 0x80 (which is the UTF-8 for U+0080). But
NSData *d2 = [string dataUsingEncoding:NSASCIIStringEncoding];
does also not work (d2 = nil), because the string cannot be converted to 7-bit
ASCII.
So you should either
store the encrypted data as "Binary Data" in Core Data, or
store the encrypted data as String, but choose a different conversion strategy,
for example Base64.
So just to answer the question so i can use code and everything..
I changed the EncryptedText in the Data Model to Binary Data and then the code would look like this:
uint8_t *cipherBuffer = NULL;
SecKey *encrypt = [[SecKey alloc]init];
[encrypt generateKeyPairRSA];
cipherBuffer = [encrypt Encryption:plainTextField.text];
NSMutableData *data = [[NSMutableData alloc]init];
[data appendBytes:cipherBuffer length:strlen((char*)cipherBuffer)+1];
// Save cipher into the Data Model
[self.currentNote setEncryptedText:data];
[self.delegate addNewNoteViewControllerDidSave];
// Retrieve cipher back from Data Model
NSData *etData = [[NSData alloc]init];
etData = [self.currentNote encryptedText];
// Convert back to uint8_t
const void *bytes = [etData bytes];
uint8_t *crypto_data = (uint8_t*)bytes;
// De cypher
[encrypt Decryption:crypto_data];

how to convert NSData to Binary data?

I need convert NSData to Binary data for send to php server
You don't need to convert NSData. Just set the post body of the NSMutableURLRequest.
const void *p = [nsDataObject bytes];
NSUInteger length = [nsDataObject length];

how to decode UTF8 string in iphone

I want to decode an UT8 encoded string.
The input string is "øæ-test-2.txt"
and after decoding it should become
"øæ-test-2.txt"
I found many API to encode the NSString or NSData to UT8 (NSUTF8StringEncoding) but was not able to find the way to decode it.
What I have tried until now:-
NSString *str = [[NSString alloc] initWithUTF8String:[strToDecode cStringUsingEncoding:NSUTF8StringEncoding]];
AND
[strToDecode stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
AND
[NSString stringWithUTF8String:[strToDecode cStringUsingEncoding:[NSString defaultCStringEncoding]]]
I have tried the same input string and I get the proper output in third party decoder.
But was not able to get success
Any hint in right direction would be highly appreciated.
I use this one.
NSString *encoded_string = #"ü";
const char *ch = [encoded_string cStringUsingEncoding:NSISOLatin1StringEncoding];
NSString *decode_string = [[NSString alloc]initWithCString:ch encoding:NSUTF8StringEncoding];
NSLog(#"%#",decode_String)
[NSString stringWithUTF8String:]

incorrect file size when converting file to NSdata in Objective C

i has the following code which suppose to convert file to data but the problem is the byte size is not the size after the conversion can someone help me on this one? i'm suppose to send the NSdata using NSStream to a server.
NSString *file = [[NSString alloc] initWithContentsOfFile:filepath;
NSLog(#"length = %d",[file length]); // file length is 248563 (Actual file size);
NSdata *data = [file dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:Yes];
NSLog(#"length = %d",[datalength]);// file length is 249199 (increase in file size);
Thanks in advance.
Why load it into a string at all in the first place?
[NSData dataWithContentsOfFile:]
The problem with your code is that the file probably wasn't ASCII originally, so its reencoding things.

NSData to NSString by changing the value null is returned. I need your help

*cipher.h, cipher.m all code : http://watchitlater.com/blog/2010/02/java-and-iphone-aes-interoperability
Cipher.m
-(NSData *)encrypt:(NSData *)plainText{
return [self transform:KCCEncrypt data:plainText;
}
step1.
Cipher *cipher = [[Cipher alloc]initWithKey:#"1234567890"];
NSData *input = [#"kevin" dataUsingEncoding:NSUTF8StringEncoding];
NSData *data = [cipher encrypt:input];
data variables NSLog print : <4d1c4d7f 1592718c fd588cec 84053e35>
step2.
NSString *changeVal = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
data variables NSLog print : null
NSData to NSString by changing the value null is returned. By converting NSString NSURLConnection want to transfer. I need you help
Kevin, I actually take the result of the cipher encrypt method and use an extension to NSData to convert it to a Base64 encoded string. The cipher text does NOT convert to a valid UTF8 string by itself.