2

编辑:这是转换后代码的更正版本

int scrambBase20[] = {1,2,3};
- (NSString *) descramble:(NSString*) input{
    char *ret = [input UTF8String];
    int offset = -(sizeof scrambBase20);
    for(int i=0;i<[input length];i++){
        if(i%(sizeof scrambBase20)==0){
            offset+=(sizeof scrambBase20);
        }
        ret[scrambBase20[i%(sizeof scrambBase20)]+offset] = (char) ((Byte) [input characterAtIndex:i]^0x45);
    }
    NSString *realRet = [[NSString alloc] initWithUTF8String:ret];
    [realRet stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

    return realRet;
}

我有这个 Java 块,我正在尝试将其转换为 Objective-C。

我有一个要解密的加密字符串。

[descramble: @"6&eee *eee1ee1e eee!"];

应该成为

"testcode" (without quotes)

相反,我得到了输出

"6&sec *ee 1ee1e  ee!" (without quotes)

以下代码是我的Java代码[作品]

String descramble(String input){
    Log.i("APP", "input length: " + input.length());
    char[] ret; //= new ArrayList<Character>();
    ret = input.toCharArray();
    int offset = -scrambBase20.length;
    for(int i=0;i<input.length();i++){
        if(i%scrambBase20.length==0)
            offset+=scrambBase20.length;
        ret[scrambBase20[i%scrambBase20.length]+offset]=(char) ((byte) (input.charAt(i))^0x45);
    }

    String realRet = "";
    for (char x : ret){
        realRet+=x;
    }
    realRet = realRet.trim();
    return realRet;
}

以下代码是我转换为 Xcode 的代码 [不起作用]

- (NSString *) descramble:(NSString*) input{
   char *ret = [input UTF8String];
   int offset = -(sizeof scrambBase20);
   for(int i=0;i<(sizeof input);i++){
        if(i%(sizeof scrambBase20)==0){
            offset+=(sizeof scrambBase20);
        }
        ret[scrambBase20[i%(sizeof scrambBase20)]+offset] = (char) ((Byte) [input characterAtIndex:i]^0x45);
    }
    NSString *realRet = [[NSString alloc] initWithUTF8String:ret];
    [realRet stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    return realRet;
}

有人在从 Java 到 Objective-C 的转换中看到错误吗?

4

2 回答 2

2

由于scrambBase20是一个数组,因此您需要使用count而不是sizeOf. Java 中的 Objective C 等价物sizeOf()count.

- (NSString *) descramble:(NSString*) input{
   char *ret = [input UTF8String];
   int offset = (-1 * [scrambBase20 count]);
   for(int i=0;i<[input length];i++){
        if(i% [scrambBase20 count] == 0){
            offset+= [scrambBase20 count];
        }
        ret[scrambBase20[i%[scrambBase20 count]+offset] = (char) ((Byte) [input characterAtIndex:i]^0x45);
    }
    NSString *realRet = [[NSString alloc] initWithUTF8String:ret];
    [realRet stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    return realRet;
}

对于 a NSString,在 java 中相当于length()目标 c 是[string length]。对于cString,它是strlen()

更新:

根据您的编辑,它是一个 C 数组而不是 NSArray。在这种情况下,您需要使用,

- (NSString *) descramble:(NSString*) input{
    char *ret = [input UTF8String];
    int offset = -1 * ((sizeof scrambBase20) / (sizeof int));
    for(int i=0;i < [input length];i++){
        if(i%((sizeof scrambBase20) / (sizeof int))==0){
            offset+=((sizeof scrambBase20) / (sizeof int));
        }
        ret[scrambBase20[i%((sizeof scrambBase20) / (sizeof int))]+offset] = (char) ((Byte) [input characterAtIndex:i]^0x45);
    }
    NSString *realRet = [[NSString alloc] initWithUTF8String:ret];
    [realRet stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

    return realRet;
}
于 2012-12-10T02:57:19.133 回答
2

您使用sizeof不正确:它不是Java的替代品length()

您应该在 C 字符串上使用,strlen(cString)例如由UTF8String, 或对象返回的字符串。[str length]NSString

于 2012-12-10T02:58:23.280 回答