// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.UnsupportedEncodingException;

public class Byte2String2Byte {
    public static void test(byte[] b, String cs) throws UnsupportedEncodingException {
        System.out.println(cs + ":");
        String s = new String(b, cs);
        byte[] b2 = s.getBytes(cs);
        if(b2.length != b.length) {
            System.out.println("length change " + (b2.length - b.length));
        }
        for(int i = 0; i < Math.min(b2.length, b.length); i++) {
            if(b2[i] != b[i]) {
                System.out.println(i + ": " + (b[i] & 0xFF) + " -> " + (b2[i] & 0xFF));
            }
        }
    }
    public static void main(String[] args) throws UnsupportedEncodingException {
        byte[] b = new byte[256];
        for(int i = 0; i < b.length; i++) {
            b[i] = (byte)i;
        }
        test(b, "UTF-8");
        test(b, "ISO-8859-1");
    }
}
