1

我正在尝试将字母转换为一串数字或整数。我可以这样做,但我想知道是否有更好的方法?我必须将 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 ]];

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

谢谢,尼克

4

3 回答 3

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

于 2012-07-02T15:58:42.303 回答
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];
于 2012-07-02T16:37:20.297 回答
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...
}
于 2012-07-02T16:05:21.760 回答