EXC_BAD_ACCESS strange Hex to String conversion [duplicate] - objective-c

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.

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 do I convert a decimal to hexadecimal using Objective C?

I've been working on a calculator and I wanted to implement conversions from decimal to octal and from decimal to hexadecimal. I'm new to Xcode and Objective C, but I've managed to get a conversion from decimal to octal, it just doesn't seem to work with hexadecimal.
Here's the code I've written to convert a double to octal:
double result = 0;
...
double decToOct = [self popOperand];
NSString *oct = [NSString stringWithFormat:#"%llo", (long long)decToOct];
result = [oct doubleValue];
Using the same scheme (obviously that includes changing #"%llo" with #"%llx") the conversion to hexadecimal works up to a certain point. It does numbers 0 through 9 just fine, but once it hits 10, it comes up as 0. To test, I also input 5395 and it displayed 1513, the desired result.
Because of this, I can only assume that for some reason my code does not want to input the actual letters of the hexadecimal values (e.g. 11 would convert to B but it shows up as 0) .
Any suggestions? Thanks in advance.
UPDATE:
In addition, I have also been using this to display the result:
double result = [self.brain performOperation:operation];
self.display.text = [NSString stringWithFormat:#"%g", result];
result, as listed from the top, is an argument which is eventually returned here, to self.brain performOperation:operation. This is supposed to handle the display of all operations, including: addition, multiplication, etc. but also octal and hexadecimal. Again, it works fine with octal, but not with hexadecimal.
Try this, May be it will help you. Please do let me know if i am wrong here:--->
NSString *decStr = #"11";
NSString *hexStr = [NSString stringWithFormat:#"%lX",
(unsigned long)[dec integerValue]];
NSLog(#"%#", hexStr);
If you know your string only contains a valid decimal number then the simplest way would be:
NSString *dec = #"254";
NSString *hex = [NSString stringWithFormat:#"0x%lX",
(unsigned long)[dec integerValue]];
NSLog(#"%#", hex);

Convert NSUInteger to string with ARC [duplicate]

This question already has answers here:
NSUInteger should not be used in format strings?
(4 answers)
Closed 8 years ago.
I'm trying to cast a NSUInteger to a string so I can print a message. From searching, it seems like I need to use stringWithFormat, but I am getting an error that an implicit cast not allowed with ARC.
Here's the line in question:
NSString *text = [[NSString stringWithFormat: (#"%li", NSUInteger)];
I've tried changing the format specifier to %lu with no help.
Thanks.
You probably have a variable of type NSUInteger, something like
NSUInteger myNumber;
Then you can convert it to a string like this:
NSString *text = [NSString stringWithFormat:#"%li", myNumber];
A solution that I prefer now is this:
NSString *text = [NSString stringWithFormat:#"%#", #(myNumber)];
This helps avoid compile warnings about incorrect number formatting codes (after a long time I still get confused in them).

Hex in a stringWithFormat BAD ACCESS

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.

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.