Hex in a stringWithFormat BAD ACCESS - objective-c

Here is a part of my code:
I have a string which is formed by a stringWithFormat like this:
NSString * DestChoice = [NSString stringWithFormat:#"%#", Dest1String];
The Dest1String is formed like this:
NSString * Dest1String = [NSString stringWithFormat:#"%lX", [Dest1OutField integerValue]];
The Dest1OutField is a NSTextField
When I put "10" in my text field, the DestChoice string result in "A" so my code is working.
When I put "11" in my text field, the DestChoice string result in "B", this is good too.
But If put "31" i'm supposed to get "1F" but I have a "EXC_BAD_ACCESS" crash...
I don't understand... If someone can light my way... :)
Thans in advance!
EDIT:
So, I'm always stuck...
I edited my code to be "more understandable" and I give you the exact code which I use, not an sample:
So, I make two strings like this:
DestChoice = [NSString stringWithFormat:#"%lX", [DestField integerValue]];
SourceChoice = [NSString stringWithFormat:#"%lX", [SourceField integerValue]];
So the user write the Source/Dest in Decimal value but they are stocked as hex in a string, and then I have a button which reformat the source & dest like this:
NSString * CommandReadyToSend = [NSString stringWithFormat:#"X:0/%#,%#\r", DestChoice, SourceChoice];
My code is working, BUT, strangly some values makes a EXC_BAD_ACCESS at the CommandReadyToSend part...
Example: If I write 1 in my text field => I receive 1 (hex value) as result in DestChoice
If I write 10 in my text field => I receive A in DestChoice
BUT If I write 31 in the text field, I'm supposed to get 1F, but I get a EXC_BAD_ACCESS...
Same thing with 29 (dec value)

NSString * DestChoice = [NSString stringWithFormat:stringWithFormat:#"%#%#", Dest1String];
You are indicating that you want to format two objects with %#%#, but you are only supplying a single object (Dest1String).

The only problem I am seeing with your code is
NSString * DestChoice = [NSString stringWithFormat:stringWithFormat:#"%#", Dest1String];
Instead, you could use
NSString * DestChoice = [NSString stringWithString:#"%#", Dest1String];
Other than that, there is no problem. I am getting correct result.

Related

How to Localize NSString with format [duplicate]

This question already has an answer here:
iOS: How to localize strings with multiple interpolated parameters?
(1 answer)
Closed 5 years ago.
How to localize the NSString with format.
int value = 20;
NSString *str = #"hello";
textLabel.text = [NSString stringWithFormat:#"%d %#", value, str];
I tried
textLabel.text = [NSString stringWithFormat:NSLocalizedString(#"%d %#", #"%d %#"), value, str];
but didn't work. Any help is appreciated.
Your localized string itself must be a format pattern:
"ValueAndStringFMT" = "Value %1$d and string %2$#";
And in your code:
textLabel.text = [NSString
stringWithFormat:NSLocalizedString(#"ValueAndStringFMT"),
value, str
];
Why %1$d and not just %d? So you can change the order. E.g. in some language you may like to have the order swapped:
"ValueAndStringFMT" = "Cadena %2$# y valor %1$d";
Of course, that is somewhat dangerous as if someone uses more placeholders than your string call offers or uses the wrong types, your app may crash. If you want to be safe, you do a search an replace instead:
"ValueAndStringFMT" = "Value [[VALUE]] and string [[STRING]]";
And in your code:
NSString * string = NSLocalizedString(#"ValueAndStringFMT");
string = [string stringByReplacingOccurrencesOfString:#"[[VALUE]]"
withString:#(value).stringValue
];
string = [string stringByReplacingOccurrencesOfString:#"[[STRING]]"
withString:str
];
textLabel.text = string;
That way the worst case scenario is that a placeholder is not expanded, meaning the placeholder is visibly printed on screen but at least your app won't crash because someone messed up the localization strings file.
If you need to localize one of the format variables, then you need to do that first in an own step:
NSString * str = NSLocalizedString(#"hello");

How to Use An Escape URL String With Formatting in Objective-C

I need to gather data from a website based on the user's input.
searchString is the user inputted value, such as "search this string".
NSString *withoutSpaces = [searchString stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
Here, I need to replace spaces with %20
Next, I need to put the new string without spaces (replaced with %20) into another string.
NSString *unescapedSearchString = [NSString stringWithFormat:
#"website.com/query?=%22%#%22", withoutSpaces];
The site I need is not really "website.com", but that's just an example. I also need the %22 to remain at the beginning and end.
As you can see, I need the %# to format the new withoutSpaces user input into the website URL.
I did a search and found examples but I could not find any with formatting such as in my case using %#.
What's the best way to "escape" the characters and keep my formatted string? Currently, when I try to access data from the website, it comes back as null. However, when I try a string without the %# formatting and an actual value, I successfully retrieve the data from a website.
Any help is greatly appreciated.
You should do things this way:
NSString *searchString = ... // the raw search string with spaces and all
NSString *quoted = [NSString stringWithFormat:#"\"%#\"", searchString];
NSString *escaped = [quoted stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat:#"website.com?query=%#&value=all", escaped];
BTW - the URL seems a little off. There should be a variable name before the = and after the ?.

EXC_BAD_ACCESS strange Hex to String conversion [duplicate]

This question already has answers here:
Hex in a stringWithFormat BAD ACCESS
(2 answers)
Closed 8 years ago.
I'm stuck.
I give you the exact code which I use, not a sample:
So, I make two strings like this:
DestChoice = [NSString stringWithFormat:#"%lX", [DestField integerValue]];
SourceChoice = [NSString stringWithFormat:#"%lX", [SourceField integerValue]];
So the user write the Source/Dest in Decimal value but they are stocked as hex in a string, and then I have a button which reformat the source & dest like this:
NSString * CommandReadyToSend = [NSString stringWithFormat:#"X:0/%#,%#\r", DestChoice, SourceChoice];
My code is working, BUT, strangly some values makes a EXC_BAD_ACCESS at the CommandReadyToSend part...
Example: If I write 1 in my text field => I receive 1 (hex value) as result in DestChoice
If I write 10 in my text field => I receive A in DestChoice
BUT If I write 31 in the text field, I'm supposed to get 1F, but I get a EXC_BAD_ACCESS...
Same thing with 29 (dec value)
It seems that certains hex value can't be reformatted to string, I don't understand...
Seems to be working for me, the cause must be elsewhere.
NSString *destChoice = [NSString stringWithFormat:#"%lX", 1];
NSString *sourceChoice = [NSString stringWithFormat:#"%lX", 31];
NSString *commandReadyToSend = [NSString stringWithFormat:#"X:0/%#,%#\r", destChoice, sourceChoice];
NSLog(#"%#", commandReadyToSend);
2012-05-10 13:56:29.092 test[9383:707] X:0/1,1F
-
btw; If DestField is a UITextField then you should be using [DestField.text integerValue]; - this is probability your problem.

Padding NSString not working

I have read that to left-pad an NSString all you need to do is this:
NSString *paddedStr = [NSString stringWithFormat:#"%-20.20# %-20.20#",
aString, anotherSting];
But, that does not work !! I donĀ“t know why. I have tried a lot of combinations without success. Examples:
NSString *paddedStr = [NSString stringWithFormat:#"%-20s#", " ", myString];
but that way is ugly and ... ugly. It just append 20 times the char (" ") before the string (myString) and that is not what we need right?
The goal is to have an NSString formatted to present two or more columns of 20 chars each one no matter the length of the string within a row.
Example Goal Output:
Day Hour Name Age
Does anybody know how to do this right?
I'm using ARC and iOS 5.
And actually, the formatted string is going to be written to file using NSFileHandle.
Thanks to all of you folks !!
Edit:
I have noticed that this works:
NSString *str = [NSString stringWithFormat:#"%-10.10s %-10.10s",
[strOne UTF8String], [strTwo UTF8String]];
But... We don't want C-style strings either.
Here is a way to do that :
NSString *paddedStr = [NSString stringWithFormat:#"%#%#",
[#"day" stringByPaddingToLength:20
withString:#" "
startingAtIndex:0],
[#"Hour" stringByPaddingToLength:20
withString:#" "
startingAtIndex:0]];

simple question concerning NSString adding multiple strings

I have a fairly simple question concerning NSString however it doesn't seem to do what I want.
this is what i have
NSString *title = [NSString stringWithformat: character.name, #"is the character"];
This is a line in my parser takes the charactername and inserts in into a plist , however it doesn't insert the #"is the character" is there something I'm doing wrong?
Your code is wrong. It should be :
NSString *title
= [NSString stringWithformat:#"%# is the character", character.name];
assuming that character.name is another NSString.
Read the Formatting String Objects paragraph of the String Programming Guide for Cocoa to learn everything about formatting strings.
stringWithFormat takes a format string as the first argument so, assuming character.name is the name of your character, you need:
NSString *title = [NSString stringWithformat: #"%s is the character",
character.name];
What you have is the character name as the format string so, if it's #"Bob" then Bob is what you'll get. If it was "#Bob %s", that would work but would probably stuff up somewhere else that you display just the character name :-)
Note that you should use "%s" for a C string, I think "%#" is the correct format specifier if character.name is an NSString itself.