Convert a byte array to a Pascal string. - Java Collection Framework

Java examples for Collection Framework:Array Convert

Description

Convert a byte array to a Pascal string.

Demo Code


//package com.java2s;

import java.nio.charset.Charset;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] data = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(bytesToPascalString(data));
    }//from   ww w.j  a  v a2s . c o  m

    private final static Charset LATIN1 = Charset.availableCharsets().get(
            "ISO-8859-1");

    /**
     *  Convert a byte array to a Pascal string. The first byte is the byte count,
     *  followed by that many active characters.
     */
    public static String bytesToPascalString(byte[] data) {
        int len = (int) data[0];
        return new String(data, 0, len, LATIN1);
    }
}

Related Tutorials