Objective-C ASCII to plain text
I am trying to get the ASCII numbers I load from a file to convert into plain text. I've looked at NSASCIIStringEncoding, but when I run it it just returns what I inputted.
Here's the code I'm using:
NSData *asciiData = [[myWords objectAtIndex:d] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *test = [[NSString alloc] initWithData:asciiData encoding:NSASCIIStringEncoding];
开发者_运维技巧
Here's an example:
ASCII string: 117 32 119 97
Expected output: u wa?
Based on the comments on your question, it sounds like your input file looks something like this:
65 66 67
That is technically eight ASCII characters: the character for the digit '6', the character for the digit '5', the character for a space ' ', etc.
If you want to read those as numbers, and then output the ASCII characters they represent, you should read the file using NSString's initWithContentsOfFile:, then use NSScanner to convert those character sequences into numbers. Then, you can output those numbers directly to a file, casting them as type char
, and the resulting file will be:
ABC
You don't actually need to declare that the numbers are ASCII when you output the file; ASCII is a system for interpreting a sequence of numbers as text. It's an ASCII file by virtue of the fact that it happens to look OK when you open it with a text editor.
精彩评论