-8

This is my code in C.

int test(unsigned char* input, unsigned char* output, int in_len)
{
    int i, out_len = 0;

    for(i = 0; i < in_len; i++)
    {
        if (*(input+i) == 0x23)
        {
            i++;
            *output++ = *(input+i) ^ 0x40;
        }
        else
        {
            *output++ = *(input+i);
        }
        out_len++;
    }
    return(out_len);
}

I want to convert my code to Java.

I have base64 values before calling test() method.

After decoding, I am calling test() method by char*

However, Java has no char* type.

How Can I use this code by using Java?

Help me.... Thanks..

public static byte[] test(byte[] input, byte[]  output, int in_len)
{
   int i, out_len = 0;

   for(i = 0; i < in_len; i++)
   {
       if ((input[i]) == 0x23) 

           i++;
           output[i] = (byte) (input[i] ^ 0x40);
       }
       else
       {
           output[i] = input[i];
       }
       out_len++;
   }
   return(output);
}
ad absurdum
  • 19,498
  • 5
  • 37
  • 60
  • Thank so mush^^ I didn't know how to quest. because I am not good at English..haha so I will just do myself. – peooooople Jul 13 '17 at 10:50

2 Answers2

0

Java does have char, it hasn't pointers, so char* should be char[]. The other difference in java char is that is someway similare to C wide-char, 2 bytes long. So it's better you use java byte that's very similar to C char, but cannot be unsigned, java byte are always signed, but the way you want to use doesn't matter.

ugo
  • 284
  • 1
  • 2
  • 10
0

You can use an array of char in Java (char[]). However, your data will be a greater size (2 bytes per char in Java vs 1 byte per char in C).

In the event you need to convert input to String, common characters will be stored as 1 byte. You can read a more here: Size of char in Java

Otherwise, your application seems to be fine.

MikeChav
  • 104
  • 7