c - convert a pointer of array in Java -


i'm trying convert program in c java. don't know c programming, i'd understand bit btw.

well i've got functions in c:

static int memcmp( byte *a, byte *b, int len) {     int i;     (i = 0; < len; i++)          if (a[i] != b[i])              return 0;     return 1; }  static int rfind(byte *data, int pos, int leng) {     int i;     (i = pos - leng; > -1; i--)          if (memcmp(data+i, data+pos, leng))              return i;     return pos; } 

that can't workout. seems function memcmp compare 2 blocks of memory. when size of data:

printf("size %d \n", sizeof(data)); 

i got 8 result while orignal size can 32 or 40 (any documentation pointers welcome).

anyone me translate bit java have gratitude (and more). thanks

java doesn't have pointers, can't translate code directly. in c, can treat pointers arrays, whereas in java need use indexes same array. more or less direct translation can come with:

public int rfind(byte[] data, int pos, int len) {     (int = pos - len; > -1; i--) {         if (memcmp(data, i, pos, len)) {             return i;         }     }     return pos; }  public boolean memcmp(byte[] data, int idx1, int idx2, int len) {     (int = idx1; <= len; i++) {         if (data[i] != data[idx2 + i]) {             return false;         }     }     return true; } 

edit

as ayush points out, if data array ascii string, can same result in simple way using library calls.

string str = new string(data); // convert data byte array string string sub = str.substring(pos, len); // substring pointed pos int rfindresult = str.lastindexof(sub); 

Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -