java中怎么把实现汉字和字符与int型ascll码的相互转换
2个回答
2015-01-09
展开全部
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class ConvertFrame extends JFrame
{
private static final long serialVersionUID = 1L;
public ConvertFrame ( String title )
{
setTitle (title);
setLayout (new FlowLayout (FlowLayout.CENTER));
setSize (420, 280);
setResizable (false);
setLocationRelativeTo (null);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
private void addComponents ()
{
final JTextArea jta1 = new JTextArea (15, 15);
jta1.setToolTipText ("native");
jta1.setLineWrap (true);
jta1.setWrapStyleWord (true);
JScrollPane scroller1 = new JScrollPane (jta1);
JButton jButton1 = new JButton (">>");
jButton1.setToolTipText ("native2ascii");
JButton jButton2 = new JButton ("<<");
jButton2.setToolTipText ("ascii2native");
JPanel jPanel = new JPanel ();
final JTextArea jta2 = new JTextArea (15, 15);
jta2.setToolTipText ("ascii");
jta2.setLineWrap (true);
jta2.setWrapStyleWord (true);
JScrollPane scroller2 = new JScrollPane (jta2);
jButton1.addActionListener (new ActionListener ()
{
@Override
public void actionPerformed ( ActionEvent e )
{
jta2.setText (native2ascii (jta1.getText ()));
}
});
jButton2.addActionListener (new ActionListener ()
{
@Override
public void actionPerformed ( ActionEvent e )
{
jta1.setText (ascii2native (jta2.getText ()));
}
});
BoxLayout bl = new BoxLayout (jPanel, BoxLayout.Y_AXIS);
jPanel.setLayout (bl);
jPanel.add (jButton1);
jPanel.add (jButton2);
add (scroller1);
add (jPanel);
add (scroller2);
}
private String native2ascii ( String nativecode )
{
char[] natives = nativecode.toCharArray ();
String ascii = "";
for ( int i = 0; i < natives.length; i++ )
{
int code = String.valueOf (natives[i]).codePointAt (0);
if (code > 127)
{
String charAscii = Integer.toHexString (code);
charAscii = "0000".substring (charAscii.length (), 4) + charAscii;
ascii += "\\u" + charAscii;
}
else
{
ascii += natives[i];
}
}
return ascii;
}
private String ascii2native ( String asciicode )
{
String[] asciis = asciicode.split ("\\\\u");
String nativeValue = asciis[0];
try
{
for ( int i = 1; i < asciis.length; i++ )
{
String code = asciis[i];
nativeValue += (char) Integer.parseInt (code.substring (0, 4), 16);
if (code.length () > 4)
{
nativeValue += code.substring (4, code.length ());
}
}
}
catch (NumberFormatException e)
{
return asciicode;
}
return nativeValue;
}
public static void main ( String[] args )
{
SwingUtilities.invokeLater (new Runnable ()
{
@Override
public void run ()
{
ConvertFrame cf = new ConvertFrame ("NATIVE/ASCII\u7f16\u7801\u4e92\u8f6c");
cf.addComponents ();
cf.setVisible (true);
}
});
}
}
展开全部
char a = 'a';
int b = a; // b就是'a'的ASC码
追问
字符这个弄是可以的,但是汉字不行
追答
汉字你试试这个看行不行
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
int intAsciiCode = (int)asciiEncoding.GetBytes(character)[0];
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询