编辑:这是转换后代码的更正版本
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 的转换中看到错误吗?