编辑:这是转换后代码的更正版本
int scrambBase20[] = {1,2,3};
- (NSString *) descrambleNSString*) 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 block ,我正在尝试将其转换为 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 *) descrambleNSString*) 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 的转换中,有人看到错误吗?
Best Answer-推荐答案 strong>
由于 scrambBase20 是一个数组,因此您需要使用 count 而不是 sizeOf 。 Objective C 等价于 Java 中的 sizeOf() 是 count 。
- (NSString *) descrambleNSString*) 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;
}
对于一个NSString ,java中的length() 等价于objective c是[string length] 。对于cString ,它是strlen() 。
更新:
根据您的编辑,它是 C 数组而不是 NSArray。在这种情况下你需要使用,
- (NSString *) descrambleNSString*) 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;
}
关于java - 将 Java 转换为 Objective-c(Android 到 iOS),我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/13794347/
|