62,621
社区成员
发帖
与我相关
我的任务
分享
String text = "阿是";
byte[] bytes = text.getBytes();//[-80, -94, -54, -57]
byte[] abytes = new byte[text.length() * 2];
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
//在这里通过c,该怎么做,才能将String转成byte[]
//System.out.println((byte)((c + 0xA0)) );
//System.out.println((byte)(0x96));
//System.out.println((byte)(0xA0 + (c >> 6)));
//System.out.println((byte)(0xa0 + (c & 0x3F)));
}
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.Arrays;
public class GBKCharUtils {
public static final Charset charset = Charset.forName("GBK");
public static byte[] getBytes(char c) {
CharBuffer charBuffer = CharBuffer.allocate(1);
charBuffer.put(c);
charBuffer.flip();
ByteBuffer byteBuffer = charset.encode(charBuffer);
return byteBuffer.array();
}
public static byte[] getBytes(char[] chars) {
CharBuffer charBuffer = CharBuffer.wrap(chars);
ByteBuffer byteBuffer = charset.encode(charBuffer);
return byteBuffer.array();
}
public static void main(String[] args) {
CharBuffer charBuffer = CharBuffer.allocate(3);
charBuffer.put('c');
charBuffer.put('2');
charBuffer.put('a');
System.out.println(Arrays.toString(getBytes('雷')));
System.out.println(Arrays.toString(getBytes(new char[]{'雷'})));
}
}
所以还是直接使用nio吧。
下面是GBK18030.java中抽出去的方法。
public void encode(CharBuffer src, ByteBuffer dst) {
//int hiByte = 0, loByte = 0;
while (src.hasRemaining()) {
char c = src.get();
if (c >= 0x0000 && c <= 0x007F) {
dst.put((byte)c);
} else if (c <= 0xA4C6 || c >= 0xE000) {
int outByteVal = getGB18030(encoderIndex1, encoderIndex2, c);
//hiByte = (outByteVal & 0xFF00) >> 8;
//loByte = outByteVal & 0xFF;
dst.put((byte)((outByteVal & 0xFF00) >> 8));
dst.put((byte)(outByteVal & 0xFF));
}
}
}
其中getGB18030就是做gbk编码和unicode映射的。
具体的源码,jdk中好像没有,可以下载openjdk的源码看。
package study.string.length;
import java.io.UnsupportedEncodingException;
import sun.io.CharToByteConverter;
import sun.io.MalformedInputException;
public class StrLenght {
public static void main(String[] args) throws UnsupportedEncodingException, MalformedInputException {
String str = "a中";
byte[] chars = str.getBytes();
for (int x = 0; x < chars.length; x++) {
System.out.println(chars[x]);
}
print(str);
}
public static void print(String str) throws UnsupportedEncodingException, MalformedInputException {
byte[] result = new byte[str.getBytes().length];
int p = 0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
byte l = (byte) c;
byte h = (byte) (c >> 8);
if (h == 0) {
result[p++] = l;
} else {
char[] cs = new char[1];
cs[0] = c;
CharToByteConverter converter = CharToByteConverter.getConverter("GBK");
byte[] br = converter.convertAll(cs);
result[p++] = br[0];
result[p++] = br[1];
}
}
for (int x = 0; x < result.length; x++) {
System.out.println(result[x]);
}
}
}

(byte)(0xE0 + (chr >> 12));
(byte)(0x80 + ((chr >> 6) & 0x3F));
(byte)(0x80 + (chr & 0x3F));
上面代码中的chr是一个字符。
如果是字符,直接
(byte)chr;
即可。
现在的困惑是GBK,的位运算该怎么写。
请大神们,不吝赐教。
[/quote]
这里没有问题。。。。你说错了吧?。。。。[/quote]
恩,我说的不准确,这么写其实也是可以的。
[/quote]
这里没有问题。。。。你说错了吧?。。。。