从 UTF-8 转 GBK失败
Java试从UTF-8 转 GBK 试过这些办法都失败了,网上的方法大多数试过,都不行
首先我尝试过检查String是什么编码的
Maven加这个
<dependency>
<groupId>com.googlecode.juniversalchardet</groupId>
<artifactId>juniversalchardet</artifactId>
<version>1.0.3</version>
</dependency>
byte[] mybankaccount=mybankaccount.getBytes();
String defaultEncoding = "UTF-8";
UniversalDetector detector = new UniversalDetector(null);
detector.handleData(mybankaccount, 0, mybankaccount.length);
detector.dataEnd();
String encoding = detector.getDetectedCharset();
detector.reset();
// log.info("字符编码是:{}", encoding);
if (encoding == null) {
encoding = defaultEncoding;
}
我检测出这个是UTF-8
然后我用下面方法,都不行
方法一
byte[] mybankaccountUTF8=mybankaccount.getBytes();
String mybankaccountUNI=new String(mybankaccountUTF8,"UTF-8");
String mybankaccountGBK=new String(mybankaccountUNI.getBytes("GBK"));
//上面这个方法有三分一的中文无法转过来
方法二
String mybankaccountGBK=charsetConvert(mybankaccount,"GBK");
private String charsetConvert(String str, String charset) {
try {
str = new sun.misc.BASE64Encoder().encode(str.getBytes(charset));
byte[] bytes = new sun.misc.BASE64Decoder().decodeBuffer(str);
str = new String(bytes, charset);
} catch(IOException e) {
e.printStackTrace();
}
return str;
}
//方法二这个办法完全无法转换,字符是乱码的
首先我尝试过检查String是什么编码的
Maven加这个
<dependency>
<groupId>com.googlecode.juniversalchardet</groupId>
<artifactId>juniversalchardet</artifactId>
<version>1.0.3</version>
</dependency>
byte[] mybankaccount=mybankaccount.getBytes();
String defaultEncoding = "UTF-8";
UniversalDetector detector = new UniversalDetector(null);
detector.handleData(mybankaccount, 0, mybankaccount.length);
detector.dataEnd();
String encoding = detector.getDetectedCharset();
detector.reset();
// log.info("字符编码是:{}", encoding);
if (encoding == null) {
encoding = defaultEncoding;
}
我检测出这个是UTF-8
然后我用下面方法,都不行
方法一
byte[] mybankaccountUTF8=mybankaccount.getBytes();
String mybankaccountUNI=new String(mybankaccountUTF8,"UTF-8");
String mybankaccountGBK=new String(mybankaccountUNI.getBytes("GBK"));
//上面这个方法有三分一的中文无法转过来
方法二
String mybankaccountGBK=charsetConvert(mybankaccount,"GBK");
private String charsetConvert(String str, String charset) {
try {
str = new sun.misc.BASE64Encoder().encode(str.getBytes(charset));
byte[] bytes = new sun.misc.BASE64Decoder().decodeBuffer(str);
str = new String(bytes, charset);
} catch(IOException e) {
e.printStackTrace();
}
return str;
}
//方法二这个办法完全无法转换,字符是乱码的
...全文
相关推荐
UTF-8编码转GBK编码
主要 用于单片机 中文编码 UTF-8转GBK, 1、UTF-8 无法转GBK 2、UTF-8 转 Uincode后,再在Uincode对应GBK表中查询,查找对应数值
GBK批量转utf8(支持整个目录)
GBK批量转utf8(支持整个目录),转换后,将原来的问题增加bak进行备份,也可以取消该选项。
网页文件批量gbk转UTF8
工具可以批量实现文件编码转换,自动备份元文件为.bak,网页文件批量gbk\UTF8互转
C语言UTF8转GBK函数到手即用
C语言UTF8转GBK函数到手即用
gbk转UTF8.zip
纯WIN32编写 , 将GBK编码的乱码字符串复制到上面的输入框 然后点击转换下面的输入框就会输出UTF-8的文字


Java SE
