Android GB2312 to String Convert gb2312ToString(byte[] b, int offset, int len)

Here you can find the source of gb2312ToString(byte[] b, int offset, int len)

Description

gb To String

Declaration

public static String gb2312ToString(byte[] b, int offset, int len) 

Method Source Code

//package com.java2s;
import java.io.UnsupportedEncodingException;

public class Main {

    public static String gb2312ToString(byte[] b) {
        return gb2312ToString(b, 0, b.length);
    }// ww w  .  ja va 2  s  .c  o m

    public static String gb2312ToString(byte[] b, int offset, int len) {
        byte[] buf = new byte[len];
        // GB2312 -> EUC-CN
        for (int i = 0; i < len / 2; i++) {
            if (b[offset + i * 2] != '\0') {
                buf[i * 2] = (byte) (b[offset + i * 2] | 0x80);
                buf[i * 2 + 1] = (byte) b[offset + i * 2 + 1];
            } else {
                buf[i * 2] = '\0';
                buf[i * 2 + 1] = '\0';
            }
        }
        String str = null;
        try {
            str = new String(buf, "EUC-CN");
        } catch (UnsupportedEncodingException e) {
            str = new String(buf);
        }
        return str.trim();
    }
}

Related

  1. gb2312ToString(byte[] b)