Unicode conversion

Oscar Castillo

Registered
lets says you have a string you want to print each character on a seperate line, since NSString is an array of Unicode characters I thought along these lines

int x;
NSString *str=@"an ordinary string";

for (x = 0; x < [str length]; x++)
{
NSlog(@"%d",[str characterAtIndex:x]);
}

Problem is chracterAtIndex: returns a (unichar), but I want the actual character. I thought about readin the values into an array, but thought there might be an easier way.
 
After experimenting a bit I actually found the answer to this problem of wanting to print a string vertically, one character at a time. Like this.

v
e
r
t
i
c
a
l

Here's a solution I came up with. Anyone have a more efficient way of doing this or any suggestions, please post.

int x;
NSString *str = @"Print this string vertically";
NSRange range;

for (x = 0; x < [str length]; x++)
{
range = NSMakeRange(x,1);
NSLog(@"%s",[[str substringWithRange:range] UTF8String]);
}
 
Originally posted by Oscar Castillo
Here's a solution I came up with. Anyone have a more efficient way of doing this or any suggestions, please post.
Code:
    int x;
    NSString *str = @"Print this string vertically";
    NSRange range;
       
    for (x = 0; x < [str length]; x++)
    {
        range = NSMakeRange(x,1);
        NSLog(@"%@",[str substringWithRange:range]);
    }

When using NSLog or -[NSString stringWithFormat:], you may use the sequence %@ to format ANY obj-c object. Those which support it by implementing -description / -descriptionWithLocale: output a description of that object, those that do not simply output the object's class and memory address. NSString does output its contents.
 
Originally posted by anarchie
Code:
    int x;
    NSString *str = @"Print this string vertically";
    NSRange range;
       
    for (x = 0; x < [str length]; x++)
    {
        range = NSMakeRange(x,1);
        NSLog(@"%@",[str substringWithRange:range]);
    }

When using NSLog or -[NSString stringWithFormat:], you may use the sequence %@ to format ANY obj-c object. Those which support it by implementing -description / -descriptionWithLocale: output a description of that object, those that do not simply output the object's class and memory address. NSString does output its contents.

Ok, I know that. But is there another more efficient way to do what I did above?
 
Well, another method which may or may not be more efficient is to insert newline characters throughout the string... Why are you trying to log a string vertically to begin with?
 
It's not so much printing vertically, but being able to manipulate individual characters within an NSString. It has to do with some database file where I receive an alphanumeric string and have to do calculations based on what is in a certain position within the string to produce a barcode string.
 
Well, you can use [str cString] to get a C string, and just index that to get a character at a certain position. You are not responsible for freeing its memory. If you're just doing alphanumerics and not foreign characters, this is probably good enough.
 
Back
Top