如何获得字符串中文的编码格式,哪儿有原码,急救急救
定义一个字符串,如何得到中文编码格式
如何转换
gb2312 to unicode, UTF8
谢谢
问题点数:100、回复次数:10Top
1 楼wjmmml(笑着悲伤)回复于 2002-09-20 15:21:15 得分 25
public static String UnicodeToChinese(String s){
try{
if(s==null||s.equals("")) return "";
String newstring=null;
newstring=new String(s.getBytes("ISO8859_1"),"gb2312");
return newstring;
}
catch(UnsupportedEncodingException e)
{
return s;
}
}
public static String ChineseToUnicode(String s){
try{
if(s==null||s.equals("")) return "";
String newstring=null;
newstring=new String(s.getBytes("gb2312"),"ISO8859_1");
return newstring;
}
catch(UnsupportedEncodingException e)
{
return s;
}
}Top
2 楼happy0451(happy)回复于 2002-09-20 15:31:03 得分 25
public String getStr(String str)//中文处理
{
try
{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp=new String(temp_t);
return temp;
}
catch(Exception e)
{
e.printStackTrace() ;
}
return "null";
}Top
3 楼caoco(都这么大了。还一无所有。做程序员真的好苦)回复于 2002-09-20 17:01:41 得分 0
楼上两兄弟的都不好使。
在j2me中找不到gb2312的,我想找转化的原码
Top
4 楼caoco(都这么大了。还一无所有。做程序员真的好苦)回复于 2002-09-20 17:03:47 得分 0
楼上两兄弟的都不好使。
在moto j2me中找不到gb2312的,我想找转化的原码
Top
5 楼lifeiqhp(飛哥)回复于 2002-09-20 17:23:48 得分 25
給你一段代碼;
烤下來,一用就明白
package untitled1;
import javax.swing.UIManager;
public class FormatsChange {
boolean packFrame = false;
//Construct the application
public FormatsChange() {
Frame1 frame = new Frame1();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame)
frame.pack();
else
frame.validate();
frame.setSize(600,550);
frame.setVisible(true);
}
//Main method
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
}
new FormatsChange();
}
}
package untitled1;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.*;
import com.borland.jbcl.layout.*;
import java.io.*;
public class Frame1 extends JFrame implements ActionListener{
XYLayout xYLayout1 = new XYLayout();
JComboBox jComboBox1 = new JComboBox();
JLabel jLabel1 = new JLabel();
JButton jButton1 = new JButton();
JTextArea jTextArea1 = new JTextArea();
JLabel jLabel2 = new JLabel();
JComboBox jComboBox2 = new JComboBox();
JLabel jLabel3 = new JLabel();
JButton jButton2 = new JButton();
String[] selectEncoding=new String[7];
JScrollPane jcs=null;
JPanel jp=new JPanel();
//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
this.getContentPane().setLayout(xYLayout1);
this.setTitle("Frame Title");
xYLayout1.setHeight(576);
xYLayout1.setWidth(559);
jLabel1.setText("選擇打開文件的編碼方式");
jButton1.setText("瀏覽");
jTextArea1.setText("jTextArea1");
jLabel2.setText("按");
jLabel3.setText("另存文件");
jButton2.setText("ok");
jp.setLayout(new BorderLayout());
jp.add("Center",jTextArea1);
jcs=new JScrollPane(jp);
this.getContentPane().add(jComboBox1, new XYConstraints(161, 22, 218, 26));
this.getContentPane().add(jButton1, new XYConstraints(398, 21, 137, 28));
this.getContentPane().add(jLabel1, new XYConstraints(20, 25, 135, 23));
this.getContentPane().add(jcs, new XYConstraints(28, 78, 509, 373));
this.getContentPane().add(jLabel2, new XYConstraints(28, 475, 24, 29));
this.getContentPane().add(jComboBox2, new XYConstraints(48, 479, 168, -1));
this.getContentPane().add(jLabel3, new XYConstraints(223, 484, 56, 21));
this.getContentPane().add(jButton2, new XYConstraints(295, 479, 173, 27));
selectEncoding[0]="GB2312";
selectEncoding[1]="Big5";
selectEncoding[2]="EUC_TW";
selectEncoding[3]="CNS11643";
selectEncoding[4]="Cp948";
selectEncoding[5]="ISO2022CN";
selectEncoding[6]="Unicode";
jButton1.addActionListener(this);
jButton2.addActionListener(this);
for(int i=0;i<7;i++){
jComboBox1.addItem(selectEncoding[i]);
jComboBox2.addItem(selectEncoding[i]);
}
}
//Overridden so we can exit on System Close
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if(e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public void actionPerformed(ActionEvent event){
String encoding=(String)jComboBox1.getSelectedItem();
DataInputStream is=null;
InputStreamReader dis=null;
DataOutputStream os=null;
OutputStreamWriter dos=null;
if(event.getSource().equals(jButton1)){
FileDialog fd=new FileDialog(this,"SelectFile",0);
fd.show();
if(fd.getFile()!=null){
String path=fd.getDirectory();
String sfile=fd.getFile();
jTextArea1.setText("");
jTextArea1.setForeground(Color.blue);
try{
is=new DataInputStream(new FileInputStream(path+sfile));
dis=new InputStreamReader(is,encoding);
char[] ss=new char[1024];
int k=dis.read(ss);
while(k!=-1){
jTextArea1.append(new String(ss,0,k));
ss=new char[1024];
k=dis.read(ss);
}
dis.close();
is.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
if(event.getSource().equals(jButton2)){
encoding=(String)jComboBox2.getSelectedItem();
FileDialog fd=new FileDialog(this,"SaveFile",1);
fd.show();
if(fd.getFile()!=null){
String path=fd.getDirectory();
String sfile=fd.getFile();
try{
os=new DataOutputStream(new FileOutputStream(path+sfile));
dos=new OutputStreamWriter(os,encoding);
String tText=jTextArea1.getText();
dos.write(tText);
dos.close();
os.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
}
}
Top
6 楼xiao_yuer(小鱼儿)回复于 2002-09-20 18:25:22 得分 8
不可能得到字符串的编码方式的,只有在知道编码的情况下才能转化为其他的编码。否则都是按系统默认的编码方式读取。Top
7 楼farawayzheng_necas(遥远)回复于 2002-09-21 14:51:36 得分 8
J2ME提供国际化解决方案码?Top
8 楼littlecong(虫子)回复于 2002-09-21 15:26:37 得分 9
好像不行,至少不支持GBKTop
9 楼caoco(都这么大了。还一无所有。做程序员真的好苦)回复于 2002-09-21 15:33:23 得分 0
好象是iso8859_1 或 iso1046..什么之类的Top
10 楼caoco(都这么大了。还一无所有。做程序员真的好苦)回复于 2002-09-23 08:36:15 得分 0
哪位有没有iso8859_1 to utf8编码的代码Top




