高分求助:8859_1 与 GBK 之间的转换

eastxiang 2004-07-11 03:39:25
现在有一文本文件,里面的汉字是8859_1的编码, 如何通过程序将它转换成GBK的汉字!

----------- 文本文件内容 如下格式--------------
"\311\317\264\253\316\304\274\376", "\315\250\320\305\263\314\320\362\325\375\303\246,\262\273\304\334\275\370\320\320\311\317\264\253\265\304\267\376\316\361"

---------------------------------------

...全文
202 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
herogao 2004-07-15
  • 打赏
  • 举报
回复
我也这么用
String str = new String(text.getBytes("ISO-8859-1"), "GBK");
Tasia 2004-07-14
  • 打赏
  • 举报
回复
为什么那些编码要放在文本文件里呢?没必要这样吧
Leemaasn 2004-07-14
  • 打赏
  • 举报
回复
我一般这么用
String str = new String(text.getBytes("ISO-8859-1"), "GBK");
wangsheng1028 2004-07-14
  • 打赏
  • 举报
回复
convert是什么东西?
lian1971 2004-07-14
  • 打赏
  • 举报
回复
要用 convert 命令
lian1971 2004-07-14
  • 打赏
  • 举报
回复
把HTML文件改为JSP文件
eastxiang 2004-07-13
  • 打赏
  • 举报
回复
向高手们求助!!!!
yunuo2010000 2004-07-12
  • 打赏
  • 举报
回复
学习
阎罗 2004-07-12
  • 打赏
  • 举报
回复
String str = new String(text.getBytes("ISO-8859-1"), "GBK");就行了
Tasia 2004-07-12
  • 打赏
  • 举报
回复
源代码如下:

import java.io.UnsupportedEncodingException;

public class Convert{
public static String toGBK(String source){
String GBKstr = "";
try{
GBKstr = new String(source.getBytes("iso-8859-1"),"GBK");
}catch(UnsupportedEncodingException e){
System.out.println("源字符串不是 iso-8859-1");
}
return GBKstr;
}

public static void main(String[] args){
String str1 = "\311\317\264\253\316\304\274\376";
String str2 = "\315\250\320\305\263\314\320\362\325\375\303\246";
String str3 = "\262\273\304\334\275\370\320\320\311\317\264\253\265\304\267\376\316\361";

System.out.println("str1 = " + toGBK(str1));
System.out.println("str2 = " + toGBK(str2));
System.out.println("str3 = " + toGBK(str3));
}
}

运行结果是:
str1 = 上传文件
str2 = 通信程序正忙
str3 = 不能进行上传的服务
Press any key to continue...
ecaol 2004-07-12
  • 打赏
  • 举报
回复
UP
Tasia 2004-07-12
  • 打赏
  • 举报
回复
对,是有这个问题。
我用FileInputStream从txt文件中读出byte[] buf来,再构建成String都不行。
不知道怎么办了。
eastxiang 2004-07-12
  • 打赏
  • 举报
回复
大佬们的方法我都试了,都不行!因为我是从文本文件取出来的8859_1编码,而不是直接给变量赋值,如果直接给变量赋值,你们方法是对的,但从文件里取出来字符串来,就有问题了!
allenhe 2004-07-11
  • 打赏
  • 举报
回复
这个很麻烦哦
不过你给出了中文后稍微好一点

举个例子
"\311\317\264\253\316\304\274\376"
读进来\311
把311转为byte b
得到byte[] ch
然后new String(ch,"ISO8859_1")

写出去的时候getByte("GBK");
pvii 2004-07-11
  • 打赏
  • 举报
回复
"\311\317\264\253\316\304\274\376"是“上传文件”
"\315\250\320\305\263\314\320\362\325\375\303\246是“通讯正忙”
\262\273\304\334\275\370\320\320\311\317\264\253\265\304\267\376\316\361"是“不能进行上传服务”
pvii 2004-07-11
  • 打赏
  • 举报
回复
String text = ....;
String convert = new String(text.getBytes("ISO-8859-1"), "GBK");
就可以了
eastxiang 2004-07-11
  • 打赏
  • 举报
回复
UP

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧