2

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?

1
  • What is scrambBase20? NSString? Commented Dec 10, 2012 at 2:55

2 Answers 2

2

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;
}
Sign up to request clarification or add additional context in comments.

6 Comments

Oh, mybad..I should have mentioned that scrambBase20 is an integer array.
The other answer is working consistently...Thanks for the help though.
@AlexBedro, But you said that it is an integer array right? In that case, how will it work? You need to use count as mentioned in my answer. I didn't understand how the other answer is correct in that case. See the documentation here developer.apple.com/library/mac/documentation/Cocoa/Reference/…. That would mislead people who are trying to achieve the same.
@AlexBedro, Okay understood what you meant by array. So it is c array and not using NSArray. Updated the answer, for the benefit of anyone looking for the same.
@AlexBedro, I will check it. Just to let you know that the actual way to find count of items in c array is ((sizeof scrambBase20) / (sizeof int)) and not (sizeof scrambBase20). Since (sizeof int) is returning 1 you might have got it correctly, but actually this is how it is done.
|
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.