`

char与byte转换

 
阅读更多
public static void main(String[] args) {
		String str="我";
		char[] ch=str.toCharArray();
		byte[] by =charToByte(ch[0]);
		for (int i = 0; i < by.length; i++) {
			System.out.println(by[i]);
		}
		
	}
	public static byte[] charToByte(char c) {
        byte[] b = new byte[2];
        b[0] = (byte) ((c & 0xFF00) >> 8);
        b[1] = (byte) (c & 0xFF);
        return b;
    }


    public static char byteToChar(byte[] b) {
        char c = (char) (((b[0] & 0xFF) << 8) | (b[1] & 0xFF));
        return c;
    }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics