EDIT: this is the corrected version of the converted code
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;
}
I have this block of Java which I am trying to convert to Objective-C.
I have an encrypted string which I am trying to decrypt.
[descramble: @"6&eee *eee1ee1e eee!"];
Should become
"testcode" (without quotes)
Instead, I get the output
"6&sec *ee 1ee1e ee!" (without quotes)
The following code is my Java code [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;
}
The following code is my converted code to Xcode [doesn't work]
- (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;
}
Does anyone see an error in the conversion from Java to Objective-C?
Since scrambBase20
is an array, you need to use count
instead of sizeOf
. Objective C equivalent of sizeOf()
in Java is 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;
}
For an NSString
, equivalent of length()
in java to objective c is [string length]
. For cString
, it is strlen()
.
Update:
As per your edit, it is a C array and not NSArray. In that case you need to use,
- (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;
}
You are using sizeof
incorrectly: it is not a replacement for length()
of Java.
You should use strlen(cString)
on C strings, such as strings returned by UTF8String
, or [str length]
on NSString
objects.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.