I want to convert UTF8String i.e.0x000060000292d290 to NSString i.e. Hx5oE/NUb7kyUfofpeQwIr1LYcRUWQGOGnsgJqSEREI=
In Objective C,
If I type po [#"Hx5oE/NUb7kyUfofpeQwIr1LYcRUWQGOGnsgJqSEREI=" UTF8String] on console then it gives me "0x000060000292d290".
How should I get reverse result?
NSData *data = [[NSData dataWithBytes:#"0x000060000292d290" length:32] base64EncodedDataWithOptions:(NSDataBase64Encoding64CharacterLineLength)];
NSString *numstr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"nonsens:%#",num);
:)
Related
I get data from a server with
NSData *data=[NSData dataWithContentsOfURL:url];
It looks like this:
2017-08-02 12:00:52.242397+0530 Trainer[608:107784] data=<0a0a2246 61696c65 6420746f 20526563 6f766572 20796f75 72207061 7373776f 72642c20 74727920 61676169 6e22>
Attempting to convert it to a string with one of the code below returns nil:
NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString* newStr = [NSString stringWithUTF8String:[data bytes]];
I packed an object (NSObject) to (NSData) and then encoded it with (NSASCIIStringEncoding) for sending it to a SQLite database with this code:
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:canvasView.trazoYorch];
//convert NSData object to plain text for sending it to DB
NSData *data2 = [[NSString stringWithFormat:#"%#",data] dataUsingEncoding:NSASCIIStringEncoding];
NSString *dataStr = [[NSString alloc] initWithData:data2 encoding:NSASCIIStringEncoding];
everything works ok, but when I want to do the Inverse process NSString to NSData I got different results, this is my code for inverse process
NSString *FirmaString = [self traerFirmadeBD]; //returns the string content of DB
NSData *data2 = [FirmaString dataUsingEncoding:NSASCIIStringEncoding];
FirmaYorch *firmaCompleta = [NSKeyedUnarchiver unarchiveObjectWithData:data2];
Any help solving this I will appreciate
i have Used NSMutableData to merge byte arrays
NSMutableData *payload;
payload = [[NSMutableData alloc] init];
[payload appendBytes:CFBridgingRetain((cm.msgBytes)) length:[cm.msgBytes length]];
NSString *cmdata = [[NSString alloc] initWithData:[payload mutableBytes] encoding:NSUTF8StringEncoding];
i want to keep on adding to payload until done
cmdata is always nil
as initWithData takes NSData so i converted the bytes to NSData but the result is still same
NSData *bytesData = [NSData dataWithBytes:[payload mutableBytes] length:[payload length]];
NSString *cmdata = [[NSString alloc] initWithData:bytesData encoding:NSUTF8StringEncoding];
I think you're confusing things in several places. Where an API says "bytes", it's talking about a C array of bytes; where it says "data", it's talking about an NSData object.
Assuming cm.msgBytes is an NSData object, which it appears to be given that you retrieve its .length, a better version of this code would be:
NSMutableData *payload;
payload = [[NSMutableData alloc] init];
[payload appendData:cm.msgBytes];
NSString *cmdata = [[NSString alloc] initWithData:payload encoding:NSUTF8StringEncoding];
If you wrote the class cm belongs to, you should probably rename msgBytes to msgData (or even messageData), to match the APIs better.
initWithData takes an NSData not bytes itself
NSString *cmdata = [[NSString alloc] initWithData:payload encoding:NSUTF8StringEncoding];
I found this method to convert an NSData to an NSString object.
NSData *data = //some data
NSString *string = [NSString stringWithFormat:#"%#", data];
How will the data be decoded? Is NSUTF8StringEncoding applied?
Thank you!
This is not the recommended approach. Instead use:
NSString *newString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
This will ensure you have a specified encoding for the data.
In this case, the stringWithFormat: method will send NSData object the description message, get the result, and use that for the content of the newly created string. Essentially, the result is identical to
NSString *string = [data description];
According to NSData's documentation, description returns
An NSString object that contains a hexadecimal representation of the receiver’s contents in NSData property list format.
You probably should be using NSString *string = [[NSString alloc] initWithData: data encoding:SomeFormOfEncoding];
You can view the method details here.
The forms of encoding can be seen here.
I am converting NSData to NSString with following code
NSData *videodata=-----?
NSString *urlstring = [[NSString alloc] initWithData:videodata encoding:NSUTF8StringEncoding] ;
I also tried
NSString *urlstring= [NSString stringWithUTF8String:[videodata bytes]];
But both returns null value.Is there any other process.Thanks
NSData *somedata = [NSData data];
NSString *str = [[NSString alloc] initWithData:somedata encoding:NSUTF8StringEncoding];
Or replace the encoding with the encoding of the data