1

I am converting/migrating a good old clipper xbase program to Java. As the old program is still running, I need to be compatible, when writing data in Java to the databases. It works really well, until now.

For that I need the functions i2bin and bin2i from clipper to be running in Java.

See here for i2Bin: http://www.marinas-gui.org/projects/harbour_manual/i2bin.htm

I can read the clipper written i2bin data with: cData is a String containing the an int coded as char beginning after first char:

char [] array = cData .substring(1).toCharArray();
int nLen = (int) array [0];

But I can not write a correct int coded as char in Java. So I got a

int len = 8;
char clen = (char) len;

and I get a '\b' but with only one character. The clipper function bin2i can not interpret it correctly. If I add a space to the char

String data = clen + " " 

I get the right length for bin2i but it is not the length 8 anymore. Maybe it is coded via Ascii 32?

Anyway, how can I do it right?

regards, Peter

PeterK
  • 11
  • 3
  • My Java is not really good but you can try: `String data = ((char)(len % 256)) + ((char)((int)(len / 256) % 256)` – Phil Krylov Oct 14 '15 at 20:30

1 Answers1

0

you want this:

public byte[] i2bin(short i)
{
 byte[] ret = new byte[2];
 ret[0] = (byte)(i & 0xff);
 ret[1] = (byte)((i>>>8) & 0xff);
 return ret;
}

do not use char or string as they are locale/charset dependent

Terefang
  • 13
  • 2