I have implemented some code to convert a NSString of “text” to an NSString of (ASCII) ints, like so:
@"Hello" is converted to @"72 101 108 108 111"
However, I am having quite a bit of difficulty doing the opposite. Starting with a string of ints (with the spaces) and converting back to the plain string of text.
What I need: @"72 101 108 108 111" must be converted to @"Hello"
I have tried breaking up the input string into an int array, iterating through it, and using repeatedly the following:
[NSString stringWithFormat:@"%c", decCharArray[i]]
However, the problem with that is that it parses each particular digit into ASCII, converting the 7, the 2, the space, the 1, etc.
Thanks a ton in advance.
There is no real magic on it. Since you’ll be using ASCII, to convert an int
to a char all you have to do is an assignment, as you may already know:
NSString has a very convenient method
componentsSeparatedByString:that willreturn an array of strings containing your numbers, and you can get an int
from a string with the
intValuemethod. Thus, all you have to do is to splitthe string and iterate through the components assigning their int value to a
char array. Here is an example function that does that:
And a simple use of it, with your
"Hello"example:Actually, it’s a bit different as your example was
"Hell^K"🙂