Skip to main content

get ascii code from string in xcode for iphone app



hey just a couple quick noob questions about writing my first ios app. Ive been searching through the questions here but they all seem to address questions more advanced than mine so im getting confused.





(1) All I want to do is turn a string into an array of integers representing the ASCII code. In other words, I want to convert:







"This is some string. It has spaces, punctuation, AND capitals."







into an array with 62 integers.





(2) How do I get back from the NSArray to a string?





(3) Also, are these expensive operations in terms of memory or computation time? It seems like it might be if we have to create a new variable at every iteration or something.





I know how to declare all the variables and im assuming I run a loop through the length of the string and at each iteration I somehow get the character and convert it into a number with some call to a built in command.





Thanks for any help you can offer or links to posts that might help!


Comments

  1. NSString has a method to get the characters in an array:

    NSString *string = "This is some string. It has spaces, punctuation, AND capitals.";

    unichar *buffer = malloc(sizeof(unichar) * [string lenght]);

    [string getCharacters:buffer range:NSMakeRange(0, [string length])];


    If you check the definition of unichar, it's an unsigned short.

    ReplyDelete
  2. if you want to store the ascii values in an nsarray it is going to be expensive. NSArray can only hold objects so you're going to have to create an NSNumber for each ASCII value:

    unsigned len = [string length];
    NSMutableArray arr = [NSMutableArray arrayWithCapacity:len];
    for (unsigned i = 0; i < len; ++i) {
    [arr addObject:[NSNumber numberWithUnsignedShort:[string characterAtIndex:i]]];
    }

    2) to go back to an NSString you'll need to use an MSMutableString and append each byte to the NSMutableString.

    After saying that I'd suggest you don't use this method if you can avoid it.

    A better approach would be to use @EmilioPelaez's answer. To go back from a memory buffer to an NSString is simple and inexpensive compared to iterating and concatting strings.


    NSString * stringFromMemory = [[NSString alloc] initWithBytes:buffer length:len encoding: NSASCIIStringEncoding];

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex