Here you can find the source of cast(byte[] bytes)
Parameter | Description |
---|---|
bytes | The bytes to cast. |
public static char[] cast(byte[] bytes)
//package com.java2s; //License from project: Open Source License public class Main { /**/*w ww . j a v a2 s . com*/ * Byte-to-byte copy of an array of chars to an array of bytes without * conversion (a char contains 2 bytes). * * This method converts non-ASCII chars. * * @param chars * The chars to cast. * * @return The same input, but as bytes. */ public static byte[] cast(char[] chars) { byte[] bytes = new byte[chars.length << 1]; for (int i = 0; i < chars.length; i++) { int pos = i << 1; bytes[pos] = (byte) ((chars[i] & 0xFF00) >> 8); bytes[pos + 1] = (byte) (chars[i] & 0x00FF); } return bytes; } /** * Byte-to-byte copy of an array of bytes to an array of chars without * conversion. (a char contains 2 bytes). * * This method converts from non-ASCII chars. * * @param bytes * The bytes to cast. * * @return The same input, but as chars. */ public static char[] cast(byte[] bytes) { char[] chars = new char[bytes.length >> 1]; for (int i = 0; i < chars.length; i++) { int pos = i << 1; char c = (char) (((bytes[pos] & 0x00FF) << 8) + (bytes[pos + 1] & 0x00FF)); chars[i] = c; } return chars; } }