String newStr = new String("中文测试","UNICODE"); 其实JAVA的默认编码方式就是UNICODE.
不行,执行程序报错 "testDatabaseBean.java": Error #: 300 : constructor String(java.lang.String, java.lang.String) not found in class java.lang.String at line 42, column 23 这是什么原因????????
没有“UNICODE”这个编码方式,标准的编码格式如下: US-ASCII Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set ISO-8859-1 ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1 UTF-8 Eight-bit Unicode Transformation Format UTF-16BE Sixteen-bit Unicode Transformation Format, big-endian byte order UTF-16LE Sixteen-bit Unicode Transformation Format, little-endian byte order UTF-16 Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order (either order accepted on input, big-endian used on output)
String newStr = new String("中文测试".getBytes("ISO-8859-1"),"UNICODE");
// American Standard Code for Information Interchange public static final String ENC_ASCII = "ASCII"; // Windows Latin-1 public static final String ENC_CP1252 = "Cp1252"; // ISO 8859-1, Latin alphabet No. 1 public static final String ENC_ISO8859_1 = "ISO8859_1"; // Sixteen-bit Unicode Transformation Format, big-endian byte order // with byte-order public static final String ENC_UTF16_BEM = "UnicodeBig"; // Sixteen-bit Unicode Transformation Format, big-endian byte order public static final String ENC_UTF16_BE = "UnicodeBigUned"; // Sixteen-bit Unicode Transformation Format, little-endian byte order // with byte-order public static final String ENC_UTF16_LEM = "UnicodeLittle"; // Sixteen-bit Unicode Transformation Format, little-endian byte order public static final String ENC_UTF16_LE = "UnicodeLittleUned"; // Eight-bit Unicode Transformation Format public static final String ENC_UTF8 = "UTF8"; // Sixteen-bit Unicode Transformation Format, byte order specified by // a mandatory initial byte-order public static final String ENC_UTF16 = "UTF-16";
其实JAVA的默认编码方式就是UNICODE.
"testDatabaseBean.java": Error #: 300 : constructor String(java.lang.String, java.lang.String) not found in class java.lang.String at line 42, column 23
这是什么原因????????
没有“UNICODE”这个编码方式,标准的编码格式如下:
US-ASCII Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set
ISO-8859-1 ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1
UTF-8 Eight-bit Unicode Transformation Format
UTF-16BE Sixteen-bit Unicode Transformation Format, big-endian byte order
UTF-16LE Sixteen-bit Unicode Transformation Format, little-endian byte order
UTF-16 Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order (either order accepted on input, big-endian used on output)
public static final String ENC_ASCII = "ASCII";
// Windows Latin-1
public static final String ENC_CP1252 = "Cp1252";
// ISO 8859-1, Latin alphabet No. 1
public static final String ENC_ISO8859_1 = "ISO8859_1";
// Sixteen-bit Unicode Transformation Format, big-endian byte order
// with byte-order
public static final String ENC_UTF16_BEM = "UnicodeBig";
// Sixteen-bit Unicode Transformation Format, big-endian byte order
public static final String ENC_UTF16_BE = "UnicodeBigUned";
// Sixteen-bit Unicode Transformation Format, little-endian byte order
// with byte-order
public static final String ENC_UTF16_LEM = "UnicodeLittle";
// Sixteen-bit Unicode Transformation Format, little-endian byte order
public static final String ENC_UTF16_LE = "UnicodeLittleUned";
// Eight-bit Unicode Transformation Format
public static final String ENC_UTF8 = "UTF8";
// Sixteen-bit Unicode Transformation Format, byte order specified by
// a mandatory initial byte-order
public static final String ENC_UTF16 = "UTF-16";
人家就需要3行输出,中文测试
\u4e2d\u6587\u6d4b\u8bd5
中文测试谁行?
请教
但是由于没有办法显示出unicode的码值,所以对于现实中的应用,可能没有什么实际意义。
欢迎批评指教
public class Test{
public static void main(String[] args){
try {
String m=new String("中文测试") ;
System.out.println(m);
String a = new String(m.getBytes("GBK"), "UnicodeBigUned");
System.out.println(a);//这个a其实就是unicode码的了,
//但是没有办法正确输出成\uxxxx\uyyyy形式.
//就像ISO-8859-1里面的中文一样。
System.out.println(new String(a.getBytes("UnicodeBigUned"),"GBK"));
} catch (Exception e) {
System.out.println(e.toString()); }
}
}
import sun.io.CharToByteConverter;public class test{
public test(){
} public static void main(String[] args){
try{
ByteToCharConverter converter = ByteToCharConverter.getC
onverter("GB2312");
CharToByteConverter converter1 = CharToByteConverter.get
Converter("GB2312");
byte[] b = "中文测试".getBytes();
System.out.println(new String(b));
char[] c = new char[4];
c = converter.convertAll(b);
for (int i=0;i<c.length;i++){
System.out.print("\\u"+Integer.toHexString(c[i])
);
}
System.out.println();
byte[] b1 = new byte[8];
char[] c1 = {'\u4e2d','\u6587','\u6d4b','\u8bd5'};
b1 = converter1.convertAll(c1);
System.out.println(new String(b1)); }
catch (Exception e){
}
}
}
public class Test{
public static void main(String[] args){
try {
String m=new String("中文测试");
System.out.println(m);
char[] c=m.toCharArray();
for(int i=0;i<c.length;i++)
System.out.println(new String(a.getBytes("UnicodeBigUned"),"GBK"));
} catch (Exception e) {
System.out.println(e.toString()); }
}
}
其实我觉着不许要把编码转来转去的,因为默认生成的就是unicode码,需要做的就是把它的unicode码打印出来,这方面我有一些经验:)
public class Test{
public static void main(String[] args){
try {
String m=new String("中文测试");
System.out.println(m);
char[] c=m.toCharArray();
for(int i=0;i<c.length;i++)
System.out.println(Integer.toHexString((int)c[i]))
} catch (Exception e) {
System.out.println(e.toString()); }
}
}
import java.net.*;public class DeEncoder {
public static void main(String[] args) throws Exception {
String plain = "中文";
String urlEncoded = java.net.URLEncoder.encode(plain);
String urlDecoded = java.net.URLDecoder.decode(urlEncoded);
System.out.println(plain + " → " + urlEncoded + " → " + urlDecoded );
}
}
关键就在这个Integer.toHexString方法上
又学了一招
不过,实在想不到居然要用到Integer的方法
我在Character和String里面着了一个遍也没找到合用的。
你的这个恐怕不是unicode吧。
其实我当初和你差不多,在String找了个遍也没找
呵呵
我觉得他想要的输出结果我这段程序已经实现了.我已经试验通过了.
大家何必硬要转换呢.
是用get方式提交信息的时候用到的,如果不用他,有些特殊字符,如%就没法正确传递
所以它肯定不是unicode码
public static void main(String[] args) throws Exception {
String plain = "你";
String urlEncoded = java.net.URLEncoder.encode(plain);
String urlDecoded = java.net.URLDecoder.decode(urlEncoded);
System.out.println(plain + " → " + urlEncoded + " → " + urlDecoded );
}
}输出是
你 → %C4%E3 → 你
你不会把这个叫unicode吧,你字的unicode是\u4f60
byte[] mystr1=new byte[500];
mystr1 = rs.getBytes("aaa");
s2 = new String(mystr1, "UTF8");
return s2;
aaa是字段名称,可是显示出来是乱码,为什么???
我弄错了.谢谢前辈指教.
我以前就试过,结果得到一个空串
是在不成你只好自己写代码了,不过
你能不能告诉我你为什麽非得转成utf8,unicode不好吗
{
byte[] buf = null;
try
{
if(rEncoding!=null)
buf = rStr.getBytes(rEncoding);
else
buf = rStr.getBytes();
}catch(Exception e)
{e.printStackTrace();}
StringBuffer sb = new StringBuffer();
for(int i=0;i<buf.length;i++)
{
sb.append(dumpHEXStr(buf[i])+" ");
}
System.out.println(sb.toString());
return sb.toString();
}
public static String dumpHEXStr (byte b)
{
return (""+"0123456789ABCDEF".charAt(0xf&b>>4)+"0123456789ABCDEF".charAt(b&0xf));
}
http://www.csdn.net/Expert/topic/509/509268.shtm