NSDictionary / Base64 String Logs - ios

I have this weird problem about NSDictionary and base64 String and I cannot locate where the problem is. Here's the code:
NSData *pictureData = [[NSData alloc]init];
pictureData = UIImagePNGRepresentation([UIImage imageNamed:#"logo"]);
NSString * picture = [pictureData base64EncodedString];
picture = [picture stringByReplacingOccurrencesOfString:#"+" withString:#"%2B"];
if(picture == nil){
picture = #"";
}
self.postDictionary = [[NSDictionary alloc]initWithObjectsAndKeys:picture,#"picture",#"some text",#"caption", nil];
What happens is If I log the string,"picture", the log is very fine and okay.
But when I try logging the dictionary it says something like this
{
caption = "some text";
picture = "iVBORw0KGgoAAAANSUhEUgAAAoAAAABYCAIAAADqXdEfAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAHGlET1QAAAACAAAAAAAAACwAAAAoAAAALAAAACwAAI2cl9ZnZQAAQABJREFUeAGM3euyXVl5HmDdWa7EuYec7sDBlZSrErtS+RP/clXiuCr+YzAYaKDpBmPABpr0S...and it gets CUT some part then... picture = iVBORw0KGgoAAAANSUhEUgAAAoAAAABYCAIAAADqXdEfAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAHGlET1QAAAACAAAAAAAAACwAAAAoAAA..till the end of the base64 string";
}
The problem is the word "picture" as key is found also in the string object inside picture. The base64 string was cut first then followed by the picture = then the full text of the base64 string
Does not happen on simulator but happens on device. Before was just fine. I didn't changed anything on the codes. and I cannot change the NSDictionary codes also.
If I don't log it it's still like that when the web service receives the POST.
So if you guys can help me find a way. Thanks :)

Related

How to show special character in UILabel iOS

I am trying to implement an app where I would like to show some text in Spanish format. For example I would like to show "España" but in my label it shows "Espa√ɬ±a" and also it changes the text for some of other text.
How to get rid of these. If anybody could help. Thanks.
Edit: When i am getting my response it logs that Below result
Message = (
"Espa\U221a\U00c9\U00ac\U00b1a:1.3\U221a\U00c7\U00ac\U00a2/min"
);
But when i extract the value according to key from Dictionary it shows
España:1.3¢/min
It means when i am getting the value from dictionary it cant do proper decoding.
how to resolve this. Any idea..?
First convert your response String to NSData using NSUTF8StringEncoding encoding, then again convert the same data to finalString like below.
NSString *string = #"España"; //Your response String goes here
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
NSString *finalString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
lblTemp.text = finalString;
UPDATE 1
I think there is some error from your response, Please see below
NSString *string = #"Nu\\u0161a Florjan\\u010di\\u010d";
NSString *finalString = [NSString
stringWithCString:[string cStringUsingEncoding:NSUTF8StringEncoding]
encoding:NSNonLossyASCIIStringEncoding];
NSLog(#"finalString = %#", finalString);
Output of above code is,
finalString = Nuša Florjančič
UPDATE 2
If you want output string like "España", your desired response should be "Espa\u00F1a", Find below,
NSString *string = #"Espa\\u00F1a";
NSString *finalString = [NSString
stringWithCString:[string cStringUsingEncoding:NSUTF8StringEncoding]
encoding:NSNonLossyASCIIStringEncoding];
NSLog(#"%#",finalString);
Output is España

Get unicode chars from webservice and display them in ios app

I need some help:
i get from WebService only a part from the uunicode value, and after this I append the prefix \u to finish the value. The .ttf is good, i tested with some hardcoded values.
NSString *cuvant = [[self.catData objectAtIndex:indexPath.row]objectAtIndex:9]; //Get data
//apend prefix (double \ to escape the \u command)
cuvant = [NSString stringWithFormat:#"\\u%#",cuvant];
// cell.catChar.text = [NSString stringWithUTF8String:"\ue674"]; --->this works very well
cell.catChar.text = [NSString stringWithUTF8String:[cuvant UTF8String]]; //---> this doesn't work
i searched the documentation, and other sites but i didn't found nothing usefull, all the hints are with hardcoded data... i need tot take the codes dinamically
Thanks!
All you need is just to feed this unicoded string as data first. So make a C-String then
NSData *dataFromUnicodedString = [NSData dataWithBytes:yourCUnicodedString length:strlen(yourCUnicodedString)];
and afterwards the resulting string will be
NSString *unicodedString = [[NSString alloc] initWithData:dataFromUnicodedString encoding:NSUTF8StringEncoding];

Proper way to get the byte array from the JSON

I am trying to get the image from the byte array. I can only get the image if enter the byte array values into the string directly as follows:
NSMutableString *imagen = [[NSMutableString alloc] initWithString:#"-1,-40,-1,-32,0,16,74,70,73,70,0,1,0,1,0,96,0,96,0,0,-1,-2,0,31,76,69,65,68,32,84,101,99,104,110,111,108,111,103,105,101,115,32,73,110,99,46,32,86,49,46,48,49,0,-1,-37,0,-124,0,5,5,5,8,5,8,12,7,7,12,12,9,9,9,12,13,12,12,12,12,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13];
//this way works fine
IF i try to get the byte array into string as follows,then I couldnt get the image:
NSString *logo=[NSString stringWithFormat:#"%#",[[JSON valueForKey:#"request"]valueForKey:#"logo" ]];
NSMutableString *imagen = [[NSMutableString alloc] initWithString:logo];
//this way doesnt work out
I am following this link to get the thing done.this link
Could you please tell me whats the proper way to get byte array from JSON?
This is not executable code, it is a method to use:
You get the JSON in self.receivedData
Convert it into an object with
NSDictionary *jsonObject = NSJSONSerialization JSONObjectWithData:
** Unknown how the image data is encoded in the JSON. If it is Base64 encoded:
Get the Base64image string with
NSString *imageString = jsonObject[#"request"][#"logo"]
Convert the Base64 string into data:
NSData *imageData = [NSData alloc] initWithBase64EncodedString: imageString options:
Get an image with
UIImage *logoImage = [imageData imageWithData]
All in all you have way to much code that accomplished nothing and converting the image data to an NSString is incorrect.
Converting to a string and then back to data accomplishes nothing.
This code seems confused? The link you posted has string encodings which contain negative numbers. You parse these as signed but then assign them to an array of unsigned uint8_t using bytes[i] = (uint8_t)byte;.
I think you need to post the string encoding format and an example?

A string encoding issuse on IOS

I came across a problem with string encoding in ios development. The story is as below:
I create some values in Chinese and then create a NSDictionary for those values, the dictionary is used as parameter for network request:
- (void)createActivity
{
NSString *actionTheme = titleF.text;
NSString *actionTitle = biaotiF.text;
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:actionTheme, #"actionTheme", actionTitle, #"actionTitle",nil];
[self networkrequest:];
}
Then some work has been done for the dictionary:
Transform the dictionary to the form of JSON as the type of NSString.
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:param options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
Encoding the string , because of Chinese word in the string.
NSString *urlEncodedString = [jsonString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
At last a full url was create with the string above:
http://app.ic100.com/action/add?paramJson=%7B%22actionTheme%22%3A%22%E6%88%96%E8%80%85%E5%92%8C%E7%94%9F%E6%B4%BB%22%2C%22actionSite%22%3A%22%E5%8E%A (something like this)
I use the third party "ASIFormDataRequest" for network request, and I also set the StringEncoding:
ASIFormDataRequest *requstHttp = [[ASIFormDataRequest alloc] init];
[requstHttp setStringEncoding:NSUTF8StringEncoding];
....
All the datas has been sent to the server successfully, but when I request these data from the server and show then on the iphone. It turn to be unreadable text:
I have carefully checked all the place that I should encode or decode the string, and only utf8 is used. What`s more , for the server side , no other encoding used either. And my colleague has tested sent data from Android platform, no problem. So I think maybe I have missed some points.
Any advise?
By using the class Base64 you can encode or decode the string.
Add Base64 class in your project from HERE
see the mehode in class to encode.
Encode:
+ (NSString *)stringWithBase64EncodedString:(NSString *)string;
- (NSString *)base64EncodedString;
Decode:
- (NSString *)base64DecodedString;

Decoding the scanned barcode value to int value

When I scan the barcode and I get some value if it is Equal=2 then I need to display with == and if it is Equal=3 then I need to display with = and if the value is 4 then invalid.
But Scanned Barcode are of integer value -- when decode using NSASCII it is displaying only till value 127 after that it is showing invalid results. Eg: if my Barcode value = 9699 the result value=jem then my added result value=jem= actualstring value=%åasc value id only showing 37
Here is my code:
- (void) readerView:(ZBarReaderView *)view didReadSymbols:(ZBarSymbolSet *)syms fromImage:(UIImage *)img
{
// do something useful with results -- cool thing is that you get access to the image too
for (ZBarSymbol *symbol in syms) {
[resultsBox setText:symbol.data];
if ([resultsBox.text length] == 2) {
addedresult.text = [resultsBox.text stringByAppendingString:#"=="];
} else if ([resultsBox.text length] == 3) {
addedresult.text = [resultsBox.text stringByAppendingString:#"="];
} if ([resultsBox.text length] >= 4) {
addedresult.text = #"Invalid";
}
[Base64 initialize];
NSString *myString = [[NSString alloc]initWithString:addedresult.text];
NSData * data = [Base64 decode:myString];
NSString * actualString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(#"%#",actualString);
labeltext.text= actualString;
int asc = [actualString characterAtIndex:0];
label.text = [NSString stringWithFormat:#"%d", asc];
[actualString release];
break;
}
}
Since someone revived this question's comments, i'll revive this entire post.
You shouldn't go through NSData to create an NSString from something you already have, and you're probably losing something along the way. Go directly to NSString using stringWithFormat. Also, ASCII will come back and byte you later, if you have a choice, use UTF8.
NSString *actualStringUTF8 = [NSString stringWithFormat:#"%#",[addedresult.text urlEncodeUsingEncoding:NSUTF8StringEncoding]];
NSString *actualStringASCII = [NSString stringWithFormat:#"%#",[addedresult.text urlEncodeUsingEncoding:NSUTF8StringEncoding]];
NSLog(#"%#",actualStringUTF8);
NSLog(#"%c",[actualStringUTF8 UTF8String]); //This is a const char*
Secondly, I looked into the SDK and it says symbol.data is already an NSString*. Depending on what you want, you may not need to do anything. If you do end up needing to change encoding, make sure you understand why you need to (one good reason is "the rest of the application uses NS****StringEncoding").
Also make sure you compare strings the correct "Objective-C" way:
[actualString isEqualToString: testString];
NOT actualString == testString;

Resources