黑马程序员-JAVA学习第7课-字符编码的操作
2011-05-24 00:49
369 查看
操作内容:
查看中文字符的GB2312码
查看中文字符的UTF-8码
查看中文字符的Unicode码
在Windows记事本中用不同编码格式存储文本文件
编程体验字符编码:
CharCode.java
public class CharCode {
/**
* Method main
*
*
* @param args
*
*/
public static void main(String[] args) throws Exception {
// TODO: 在这添加你的代码
System.setProperty("file.encoding","GBK");
System.getProperties().list(System.out);
String strChina="中国";//JAVA中使用的是Unicode码
for(int i=0;i<strChina.length();i++)
{
System.out.println(Integer.toHexString((int)strChina.charAt(i)));//取出每一个字符,转换成int就是字符的Unicode码
}
byte [] buf=strChina.getBytes();
for(int i=0;i<buf.length;i++)
{
System.out.println(Integer.toHexString(buf[i]));
}
for(int i=0;i<buf.length;i++)
{
System.out.write(buf[i]);
}
System.out.println();
System.out.println("中国");
}
}
CharDecode.java
public class CharDecode {
/**
* Method main
*
*
* @param args
*
*/
public static void main(String[] args) throws Exception {
// TODO: 在这添加你的代码
System.getProperties().put("file.encoding","iso8859-1");
System.out.println("please enter a chinese");
byte buf[]=new byte[1024];
int pos=0;
String strInfo=null;
while(true)
{
int ch=System.in.read();
System.out.println(Integer.toHexString(ch));
switch(ch)
{
case '/r':
break;
case '/n':
strInfo=new String(buf,0,pos,"gb2312");
for(int i=0;i<strInfo.length();i++)
{
System.out.println(Integer.toHexString(strInfo.charAt(i)));
}
break;
default:
buf[pos++]=(byte)ch;
}
}
}
}
查看中文字符的GB2312码
查看中文字符的UTF-8码
查看中文字符的Unicode码
在Windows记事本中用不同编码格式存储文本文件
编程体验字符编码:
CharCode.java
public class CharCode {
/**
* Method main
*
*
* @param args
*
*/
public static void main(String[] args) throws Exception {
// TODO: 在这添加你的代码
System.setProperty("file.encoding","GBK");
System.getProperties().list(System.out);
String strChina="中国";//JAVA中使用的是Unicode码
for(int i=0;i<strChina.length();i++)
{
System.out.println(Integer.toHexString((int)strChina.charAt(i)));//取出每一个字符,转换成int就是字符的Unicode码
}
byte [] buf=strChina.getBytes();
for(int i=0;i<buf.length;i++)
{
System.out.println(Integer.toHexString(buf[i]));
}
for(int i=0;i<buf.length;i++)
{
System.out.write(buf[i]);
}
System.out.println();
System.out.println("中国");
}
}
CharDecode.java
public class CharDecode {
/**
* Method main
*
*
* @param args
*
*/
public static void main(String[] args) throws Exception {
// TODO: 在这添加你的代码
System.getProperties().put("file.encoding","iso8859-1");
System.out.println("please enter a chinese");
byte buf[]=new byte[1024];
int pos=0;
String strInfo=null;
while(true)
{
int ch=System.in.read();
System.out.println(Integer.toHexString(ch));
switch(ch)
{
case '/r':
break;
case '/n':
strInfo=new String(buf,0,pos,"gb2312");
for(int i=0;i<strInfo.length();i++)
{
System.out.println(Integer.toHexString(strInfo.charAt(i)));
}
break;
default:
buf[pos++]=(byte)ch;
}
}
}
}
相关文章推荐
- 黑马程序员-JAVA学习第7课-字符编码
- 黑马程序员_java操作xml学习
- 黑马程序员——Java基础——对象序列化、管道流、RandomAccessFile类、操作基本数据类型的流对象、字符编码
- 黑马程序员之java中操作字符串的常用类String和StringBuffer学习
- 黑马程序员——Java学习总结:转换流和流的操作总结
- 黑马程序员-JAVA学习第7课-Java程序与其它进程的数据通信
- 黑马程序员--IO流和文件操作--java学习日记8(基础知识)
- 黑马程序员--Java基础学习笔记【文件操作、递归】
- 黑马程序员学习笔记之七(Java IO操作)
- 黑马程序员——JAVA学习笔记十一(文件操作File)
- 黑马程序员——Java学习总结:转换流和流的操作总结
- 黑马程序员-JAVA学习第7课-RandomAccessFile类
- 黑马程序员_java基础篇操作数据的,操作字节数组的,字符编码Day6
- 黑马程序员-JAVA学习第7课--包装类
- 黑马程序员--Java基础学习笔记【重载、数组操作】
- 黑马程序员--Java基础学习(IO流及字符编码)第二十一天
- 黑马程序员—java基础学习--JavaBean的内省操作
- 黑马程序员--Java基础学习笔记【数组操作、基本数据类型包装类】
- 黑马程序员java学习—流的操作
- 黑马程序员--Java基础学习之IO流之字节流、字符流、读取写入文件、Copy文件、键盘输入输出、流操作的基本规律