Android Byte Array to Unicode Convert bytesToAsciiMaybe(byte[] data)

Here you can find the source of bytesToAsciiMaybe(byte[] data)

Description

bytes To Ascii Maybe

Declaration

public static String bytesToAsciiMaybe(byte[] data) 

Method Source Code

//package com.java2s;

public class Main {
    public static int PRINTABLE_ASCII_MIN = 0x20;
    public static int PRINTABLE_ASCII_MAX = 0x7E;

    public static String bytesToAsciiMaybe(byte[] data) {
        return bytesToAsciiMaybe(data, 0, data.length);
    }/*  w ww . jav  a2s .  c  om*/

    public static String bytesToAsciiMaybe(byte[] data, int offset,
            int length) {
        StringBuilder ascii = new StringBuilder();
        boolean zeros = false;
        for (int i = offset; i < offset + length; i++) {
            int c = data[i] & 0xFF;
            if (isPrintableAscii(c)) {
                if (zeros) {
                    return null;
                }
                ascii.append((char) c);
            } else if (c == 0) {
                zeros = true;
            } else {
                return null;
            }
        }
        return ascii.toString();
    }

    public static boolean isPrintableAscii(int c) {
        return c >= PRINTABLE_ASCII_MIN && c <= PRINTABLE_ASCII_MAX;
    }
}

Related

  1. byteArrayBigEndian2Int(byte[] bs)
  2. byteArrayLittleEndian2Int(byte[] bs)
  3. bytesToAsciiMaybe(byte[] data, int offset, int length)
  4. lowerToUpper(byte[] b)
  5. lowerToUpperLatin(byte[] b)
  6. jisx0208ToString(byte[] b)