编辑:这是已转换代码的更正版本
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代码[works]
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的转换中看到错误?
答案 0 :(得分:2)
由于scrambBase20
是一个数组,因此您需要使用count
而不是sizeOf
。 Java中{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;
}
对于NSString
,java中与目标c相等的length()
为[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;
}
答案 1 :(得分:2)
您错误地使用了sizeof
: 是<{1}} Java的替代。
您应该在C字符串上使用length()
,例如strlen(cString)
返回的字符串,或UTF8String
个对象上的[str length]
。