0
点赞
收藏
分享

微信扫一扫

public class CharUtil {

Star英 2022-07-27 阅读 120



import java.nio.ByteBuffer;



public class CharUtil {



public static final int LARGE_CHARS_THRESHOLD = 1024;



public static char[] merge(char[] chars0, char[] chars1) {

if (chars0.length == 0) {

return chars1;

} else if (chars1.length == 0) {

return chars0;

}

int capacity = chars0.length + chars1.length;

char[] chars = new char[capacity];

int pos = 0;

int length = chars0.length;

System.arraycopy(chars0, 0, chars, pos, length);

pos += length;

length = chars1.length;

System.arraycopy(chars1, 0, chars, pos, length);

return chars;

}



public static byte[] toBytes(char[] chars) {

return CharToByte.toBytes(chars);

}



public static byte[] toAsciiBytes(char[] chars) {

return CharToByte.toAsciiBytes(chars);

}



public static String toUnicodeString(char c) {

return Integer.toHexString((int) c);

}



public static String toUnicodeString(String s) {

StringBuilder builder = new StringBuilder();

for (int i = 0; i < s.length(); i++) {

if (i > 0) {

builder.append(' ');

}

builder.append(toUnicodeString(s.charAt(i)));

}

return builder.toString();

}





 

public static void main(String[] args) throws Exception {

String s = "fudan1988";

char[] chars = s.toCharArray();

byte[] bytes = toBytes(chars);

ByteBuffer byteBuffer = ByteBuffer.allocate(bytes.length);

byteBuffer.put(bytes);

byteBuffer.flip();

char[] chars1 = new char[chars.length];

for (int i = 0; i < chars1.length; i++) {

chars1[i] = byteBuffer.getChar();

}

for (int i = 0; i < chars.length; i++) {

assert chars[i] == chars1[i];

}

}



}

举报

相关推荐

0 条评论