将完整的字母转换为OBJ-C中的数字

时间:2012-07-02 15:53:52

标签: iphone objective-c ios xcode

我正在尝试将字母表转换为数字或整数字符串。我可以这样做,但我想知道是否有更好的方法吗?我必须将4个字母转换为相应的数字,因此我创建了一个带字母表的NSMutableArray,然后完成此操作,它正在读取一个字符串,然后将字符串拉成一个一件。

编辑:所以如果我的消息是“MNOP”我想要一个字符串“13141516”

 alphabetArray = [[NSMutableArray alloc] init];
[alphabetArray insertObject:@"0" atIndex:0];
[alphabetArray insertObject:@"A" atIndex:1];
[alphabetArray insertObject:@"B" atIndex:2];
[alphabetArray insertObject:@"C" atIndex:3];
[alphabetArray insertObject:@"D" atIndex:4];
[alphabetArray insertObject:@"E" atIndex:5];
[alphabetArray insertObject:@"F" atIndex:6];
[alphabetArray insertObject:@"G" atIndex:7];
[alphabetArray insertObject:@"H" atIndex:8];
[alphabetArray insertObject:@"I" atIndex:9];
[alphabetArray insertObject:@"J" atIndex:10];
[alphabetArray insertObject:@"K" atIndex:11];
[alphabetArray insertObject:@"L" atIndex:12];
[alphabetArray insertObject:@"M" atIndex:13];
[alphabetArray insertObject:@"N" atIndex:14];
[alphabetArray insertObject:@"O" atIndex:15];
[alphabetArray insertObject:@"P" atIndex:16];
[alphabetArray insertObject:@"Q" atIndex:17];
[alphabetArray insertObject:@"R" atIndex:18];
[alphabetArray insertObject:@"S" atIndex:19];
[alphabetArray insertObject:@"T" atIndex:20];
[alphabetArray insertObject:@"U" atIndex:21];
[alphabetArray insertObject:@"V" atIndex:22];
[alphabetArray insertObject:@"W" atIndex:23];
[alphabetArray insertObject:@"X" atIndex:24];
[alphabetArray insertObject:@"Y" atIndex:25];
[alphabetArray insertObject:@"Z" atIndex:26];

NSRange range1 = NSMakeRange(0, 1);
NSRange range2 = NSMakeRange(1, 1);
NSRange range3 = NSMakeRange(2, 1);
NSRange range4 = NSMakeRange(3, 1);


NSString *letter1 = [msg substringWithRange:range1];  
NSString *letter2 = [msg substringWithRange:range2];
NSString *letter3 = [msg substringWithRange:range3]; 
NSString *letter4 = [msg substringWithRange:range4];

NSString *msgAsInt = [[NSString alloc]
             initWithFormat:@"%d%d%d%d",
             [alphabetArray  indexOfObject:letter1 ],
             [alphabetArray  indexOfObject:letter2 ],
             [alphabetArray  indexOfObject:letter3 ],
             [alphabetArray  indexOfObject:letter4 ]];

任何建议都会很棒。我还没有测试过,但它看起来还不错

谢谢, 尼克

3 个答案:

答案 0 :(得分:3)

如果您只是想要'message as int',请这样做(假设为UTF-8):

NSMutableString *msgAsInt = [[NSMutableString alloc] init];
for (int i = 0; i < msg.length; i++)
    [msgAsInt appendFormat:@"%02d", [msgAsInt characterAtIndex:i] - 'A'];

此外,如果您希望稍后恢复该邮件,最好在02之前添加%d

答案 1 :(得分:1)

如果性能很重要(例如,您要转换数千个字符串),则可以使用缓冲读写:

NSString *string = @"MNOP";

NSUInteger bufferSize = 4;
NSRange range = NSMakeRange(0, MIN(bufferSize,[string length]));

unichar inBuffer[bufferSize];
unichar outBuffer[bufferSize * 2];

[string getCharacters:inBuffer range:range];
NSUInteger outLength = 0;

for ( NSUInteger i = 0; i < range.length; i++)
{
    unichar character = inBuffer[i];
    if ( character >= 'A' && character <= 'Z' ) {
        int value = character - 'A' + 1;
        outBuffer[outLength++] = (value / 10) + '0';
        outBuffer[outLength++] = (value % 10) + '0';
    }
    else {
        // error management
    }
}

NSString *result = [NSString stringWithCharacters:outBuffer length:outLength];

答案 2 :(得分:0)

如果您只处理从A到Z的大写字母,则可以依赖字符的UTF8值。做类似的事情:

NSMutableString *msgAsInt = [NSMutableString string];
char *cMsg = calloc(5);
if ([msg getCString:cMsg maxLength:4 encoding:NSUTF8StringEncoding]) {
    for (int i = 0; i < 4; i++) {
        [msgAsInt appendFormat:@"%d", (cMsg[i] - ('A' - 1))];
    }
}
else {
    // there was a problem...
}