gpt4 book ai didi

java - 将 Java 转换为 Objective-c(Android 到 iOS)

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:48:09 26 4
gpt4 key购买 nike

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

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 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 *) 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 的转换有错误吗?

最佳答案

由于 scrambBase20 是一个数组,因此您需要使用 count 而不是 sizeOf。 Java 中 sizeOf() 的 Objective C 等价物是 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;
}

对于 NSString,java 中的 length() 等同于 objective-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;
}

关于java - 将 Java 转换为 Objective-c(Android 到 iOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13794347/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com