当前位置: 技术问答>java相关
如何获得字符串中文的编码格式,哪儿有原码,急救急救
来源: 互联网 发布时间:2015-09-11
本文导语: 定义一个字符串,如何得到中文编码格式 如何转换 gb2312 to unicode, UTF8 谢谢 | public static String UnicodeToChinese(String s){ try{ if(s==null||s.equals("")) return ""; String newstring=null; ...
定义一个字符串,如何得到中文编码格式
如何转换
gb2312 to unicode, UTF8
谢谢
如何转换
gb2312 to unicode, UTF8
谢谢
|
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;
}
}
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;
}
}
|
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";
}
{
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";
}
|
給你一段代碼;
烤下來,一用就明白
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
烤下來,一用就明白
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