最近用华为和亚信cmpp api,都发现一个问题:再把一个String转化一个byte[]的时候,居然没有指定编码方式!也就是说使用的是s.getBytes()方法!
这个方法是以系统的默认编码,来进行这个转换操作。而在不同的计算机上,这个值可能被设置为不同。比如,我的座机xp,默认编码就是gbk。而我的一个服务器,默认编码就是utf-8。为了确保你的程序更加可移植,你得认真考虑是否使用s.getBytes()。我建议你明确地指定编码方式进行转换。
有兴趣的朋友,可以用下面的代码试试。然后你更改你的windows的区域设置。再运行一下。
package test;
class charset {
public static void main(String[] args) throws Exception {
String s = "汉字-123-abc";
printIT(s.getBytes());
printIT(s.getBytes("GBK"));
printIT(s.getBytes("ASCII"));
printIT(s.getBytes("UTF-8"));
printIT(s.getBytes("UnicodeBigUnmarked"));
}
static void printIT(byte[] theByte) {
for (byte b : theByte) {
System.out.print(b);
System.out.print(" ");
}
System.out.println();
}
}
http://blog.ccidnet.com/blog.php?do=showone&uid=14592&type=blog&itemid=52726
分享到:
相关推荐
java.lang.reflect.Method method = clazz.getMethod("close", null); method.invoke(resource, null); } catch (Exception e) { // e.printStackTrace(); } } /** * 执行 SELECT 等 SQL 语句并返回结果集. ...
java中使用公钥加密私钥解密原理. KeyGenerater类: public class KeyGenerater { private byte[] priKey; private byte[] pubKey; public void generater() { try { KeyPairGenerator keygen = KeyPairGenerator ...
if(java.lang.Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) { t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3); t4+=t2[0]; } else t4+=java.lang.Character.toString(t1[i]); } ...
import java.security.spec.*; import javax.crypto.*;... + java.lang.Integer.toHexString(data[i]&0xFF); else str = str + java.lang.Integer.toHexString(data[i]&0xFF); } return str.toUpperCase(); } } }
import java.lang.*; public class Server extends ServerSocket { private static ArrayList User_List = new ArrayList(); private static ArrayList Threader = new ArrayList(); private static LinkedList ...
java.lang.String strCon; //数据库连接字符串 java.lang.String strSQL; //SQL语句 int intPageSize; //一页显示的记录数 int intRowCount; //记录总数 int intPageCount; //总页数 int intPage; //待显示页码 ...
public static byte[] getBytes(String filePath) { byte[] buffer = null; try { File file = new File(filePath); FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream ...
java.lang.String类是final类型的,因此不可以继承这个类、不能修改这个类。为了提高效率节省空间,我们应该用StringBuffer类 3、int 和 Integer 有什么区别 Java 提供两种不同的类型:引用类型和原始类型(或内置...
if (java.lang.Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) { t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3); t4 += t2[0]; } else { t4 += java.lang.Character.toString(t1[i]); ...
private final String windowsParseMacAddress(String ipConfigResponse) throws java.text.ParseException { String localHost = null; try { localHost = java.net.InetAddress.getLocalHost().getHostAddress(); ...
import java.lang.*; import java.util.*; import java.security.*; public class EncryptionExample { protected String calg = "Blowfish"; // AES. DES, Blowfish protected int keyLen = 128; // 128 for ...
java.lang.String类是final类型的,因此不可以继承这个类、不能修改这个类。为了提高效率节省空间,我们应该用StringBuffer类 6、int 和 Integer 有什么区别 Java 提供两种不同的类型:引用类型和原始类型(或...
以文本信息为例,响应文本信息只需要在你的``onText``方法中使用``responseText(content)``即可(参见上面的代码例子) ##wechat4j示例项目 * [wechat4jDemo](https://github.com/repoproject/wechat4jDemo) 如果...
7. <id name="fileId" type="java.lang.String" column="FILE_ID"> 8. <generator class="uuid.hex"/> 9. 10. 11. type="org.springframework.orm.hibernate3.support.BlobByteArrayType" 12. column="FILE_...
import java.lang.reflect.*; public class MD5 { /* 下面这些S11-S44实际上是一个4*4的矩阵,在原始的C实现中是用#define 实现的, 这里把它们实现成为static final是表示了只读,切能在同一个进程空间内的多个 ...