java 里面的string 和byte[] 怎么互转?

1.string 转 byte[]
byte[] midbytes=isoString.getBytes("UTF8");
//为UTF8编码
byte[] isoret = srt2.getBytes("ISO-8859-1");
//为ISO-8859-1编码
其中ISO-8859-1为单字节的编码
2.byte[]转string
String isoString = new String(bytes,"ISO-8859-1");
String srt2=new String(midbytes,"UTF-8");
说明:
在网络传输或其它应用中常常有同一的中间件,假设为String类型。因此需要把其它类型的数据转换为中间件的类型。
将字符串进行网络传输时,如socket,需要将其在转换为byte[]类型。这中间如果采用用不同的编码可能会出现未成预料的问题,如乱码。
下面举个例子:
我们用socket传输String类型的数据时,常常用UTF-8进行编码,这样比较可以避免一个“中文乱码”的问题。
发送端:
String sendString="发送数据";
byte[] sendBytes= sendString .getBytes("UTF8");
.......socket发送
接受端:
String recString=new String( sendBytes ,"UTF-8");

但是,这里往往又会出现这样一个问题。就是想要发送的数据本身就是byte[]类型的。
如果将其通过UTF-8编码转换为中间件String类型就会出现问题
如:
byte[] bytes = new byte[] { 50, 0, -1, 28, -24 };
String sendString=new String(  bytes ,"UTF-8");
byte[] sendBytes= sendString .getBytes("UTF8");
然后再发送
接受时进行逆向转换
String recString=new String( sendBytes ,"UTF-8");
byte[] Mybytes=isoString.getBytes("UTF8");
这时Mybytes中的数据将是[50, 0, -17, -65, -67, 28, -17, -65, -67]
因此,需要采用单字节的编码方式进行转换
String sendString=new String(  bytes ,"UTF-8");   改为       String sendString=new String(  bytes , "ISO-8859-1" );
byte[] Mybytes=isoString.getBytes("UTF8");  改为   byte[] Mybytes=isoString.getBytes(  "ISO-8859-1" );

这样所需要的字节就有恢复了。

其他回答

Note1: String 可转为 byte[] -- String str="abc"; byte[] data=str.getBytes();  Note2: byte[] 可转为 String -- byte[] data=..... String s=new String(data); Note3: 为避免发生编码的困扰, 建议不要转成 byte[]. 与char[] 互转比较好. Note4: String 可转为 char[] -- String str = "abc"; 相当於是 char data[] = {'a', 'b', 'c'};  String str = new String(data);  Note5: char[] 可转为 String -- String str="abc"; char[] data=str.toCharArray();

JAVA中3种将byte转换为String的方法

HttpClient 类库中GetMethod类的getResponseBody方法返回的是byte[]类型,要操作起来不方便,我想把它转化成String类型。

查了网上的资料,有说法认为用这种方法比较好 BASE64Encoder enc=new BASE64Encoder(); String 转换后的string=enc.encode(byte数组);

参考http://hi.baidu.com/zhaolm/blog/item/397b0808bc6023d362d986f3.html/cmtid/e3a206f43cb6f9e87609d746

但是有的人说这种

BASE64Encoder是非官方JDK里面的类。虽然可以在JDK里能找到并使用,但是在API里查不到。这两个可能是SUN公司内部人使用的。SUN开头的包里面的类都找不到相关文档,所以里面可能都是非官方的类。出现警告也是非常合理和正常的,因为以后SUN可能会更新或这删除那些非官方的类,建议不要使用。

有这个缺点,又要导入jar包,挺麻烦的,所以就放弃采用它了。 

于是又查了一个英文网站上说了3个方法,都比较简单。我用了第3种,目前看没什么问题。

摘自http://www.javadb.com/convert-byte-to-string

/**  *  * @author javadb.com  */ public class Main {          /**      * Example method for converting a byte to a String.      */     public void convertByteToString() {                  byte b = 65;                  //Using the static toString method of the Byte class         System.out.println(Byte.toString(b));         //Using simple concatenation with an empty String         System.out.println(b + "");                  //Creating a byte array and passing it to the String constructor         System.out.println(new String(new byte[] {b}));         可以将byte转换成a     }          /**      * @param args the command line arguments      */     public static void main(String[] args) {         new Main().convertByteToString();     } }

http://blog.csdn.net/liuzheng2684/article/details/6920769

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值