第二步:将StringBuffer转换为字节数组 // 获取字符串内容Stringstr=stringBuffer.toString();// 将字符串转换为字节数组,注意编码方式byte[]byteArray=str.getBytes("UTF-8");// 使用UTF-8编码方式 1. 2. 3. 4. 5. 这里,我们先将StringBuffer转换为String类型,然后使用getBytes方法将字符串转换为字节数组。...
通过上述步骤,我们实现了利用StringBuffer添加字符串并将其编码为 UTF-8 格式的过程。完整代码如下: importjava.util.Arrays;publicclassStringBufferExample{publicstaticvoidmain(String[]args){// 创建一个 StringBuffer 对象StringBuffersb=newStringBuffer();// 添加字符串sb.append("Hello, ");sb.append("World!
String(byte[] byte, int begin, int length, String chatsName) 含义为:获取字节数组中从第“begin”开始到“length”长度结束字符,形成字符串对象,然后按照“charname”字符集输出。 字符集一般有:“us-ascii”, "iso-8859-1","utf-8","utf-16be","utf-16le","utf-16"等样式。 byte[] byte = {9...
public Abstract StringBuilder append(StringBuffer sb) { if (sb == null) return appendNull(); int len = sb.length(); ensureCapacityInternal(count + len); sb.getChars(0, len, value, count); count += len; return this; } 其实就是干了2件事扩容和放字符。 StringBuffe...
str.getBytes("UTF-8"); 意思是以UTF-8的编码取得字节 new String(XXX,"UTF-8"); 意思是以UTF-8的编码生成字符串 举例:public static String getUTF8XMLString(String xml) { // A StringBuffer Object StringBuffer sb = new StringBuffer();sb.append(xml);String xmString = "";String...
StringBuffer sb = new StringBuffer(); String user = “test”; String pwd = “123”; sb.append(“select * from userInfo where username=“) .append(user) .append(“ and pwd=”) .append(pwd); 这样对象sb的值就是字符串“select * from userInfo where username=test and pwd=123”。
可通过以下代码转:/ Get XML String of utf-8 return XML-Formed string / public static String getUTF8XMLString(String xml) { // A StringBuffer Object StringBuffer sb = new StringBuffer();sb.append(xml);String xmString = "";String xmlUTF8="";try { xmString = new String(...
StringBuffer strBuff = new StringBuffer(""); try { byte b[] = str.getBytes("UTF-16"); for (int n = 0; n < b.length; n++) { str = (Integer.toHexString(b[n] & 0XFF)); if (str.length() == 1) { strBuff.append("0").append(str); ...
StringBuffer,StringBuilder 问题档次并不低,如果仔细思考的话,这是一个历史悠久,并且至今仍在困扰很多...
publicbyte[] gbk2utf8(String chenese) { charc[] = chenese.toCharArray(); byte[] fullByte =newbyte[3* c.length]; for(inti =0; i < c.length; i++) { intm = (int) c[i]; String word = Integer.toBinaryString(m); StringBuffer sb =newStringBuffer(); ...