今天在Android上测试压缩和解压缩。 获得压缩后的byte[]数组后,直接用 byte[].toString()方法取得字符串。

然后用这个字符串再反向来解压缩,还原数据。却发现还原回来的字符串有误。

    String str = "这是一个用于测试的字符串";

        try {
/*
* 压缩
*/
ByteArrayOutputStream out = new ByteArrayOutputStream();
ZipOutputStream zout = new ZipOutputStream(out); zout.putNextEntry(new ZipEntry("0")); zout.write(str.getBytes());
zout.closeEntry();
byte[] compressed = out.toByteArray(); // 返回压缩后的字符串的字节数组
Log.d(TAG,"compressed String" + compressed.toString());
/*
* 解压
*/
ByteArrayOutputStream rout = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(compressed);
ZipInputStream zin = new ZipInputStream(in);
zin.getNextEntry();
byte[] buffer = new byte[1024];
int offset = -1;
while ((offset = zin.read(buffer)) != -1) {
rout.write(buffer, 0, offset);
}
byte[] uncompressed = rout.toByteArray(); // 返回解压缩后的字符串的字节数组
Log.d(TAG,"uncompressed String" + uncompressed.toString()); } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
打出的不是字符串数据,而是类签名:
compressed String[B@42eabfb8
uncompressed String[B@42eaeb90

byte[]字节数组的toString()获得的字符串,和使用new String(byte[]) 构造一个新的字符串。得出的结果不同。

Java对象都继承于Object,Object中提供了toString方法,用于简单返回该类的类签名。在Java中,数组也可以看作是一种对象,显然byte[]也是一种继承与Object的对象,并且它没有重写Object的toString方法,因此使用byte[]的toString返回的字符串,仅仅是byte[]的类签名,而不是对应的值。

改为使用new String()构造方法,将byte[]转换为字符串,得到的就会是一个根据字节数组内容构造的字符串。

Log.d(TAG,"compressed String" + new String(compressed));
Log.d(TAG,"uncompressed String" + new String(uncompressed));

输出:

compressed StringPKԀ�F0$��这是一个用于测试的字符串P�j��)$
uncompressed String这是一个用于测试的字符串

byte[] 的toString() 和 new String(byte[]) 的区别的更多相关文章

  1. String详解, String和CharSequence区别, StringBuilder和StringBuffer的区别 (String系列之1)

    本章主要介绍String和CharSequence的区别,以及它们的API详细使用方法. 转载请注明出处:http://www.cnblogs.com/skywang12345/p/string01. ...

  2. String详解, String和CharSequence区别, StringBuilder和StringBuffer的区别

    本章主要介绍String和CharSequence的区别,以及它们的API详细使用方法. 转载请注明出处:http://www.cnblogs.com/skywang12345/p/string01. ...

  3. new String(byte[])和byte[]toString() 的区别

    byte[]字节数组的toString()获得的字符串和使用new String(byte[])构造一个新的字符串,这两个字符串是不一样的.Java对象都继承于Object,Object中提供了toS ...

  4. byte和hexstring,int,string等的转换类

    public class HexConversion { /** * 16进制数的字符串转字节数组(16进制转字节数组) * * @param hexString * 16进制字符串 * @retur ...

  5. 用java String类的getBytes(String charsetName)和String(byte[] bytes, String charsetName)解决乱码问题

    Java中String的数据是如何存储的,查看源代码就可以知道,String的数据是存储在char[] value这样一个成员变量中的,char类型的大小在java中是2个字节 我们还知道,现在普遍使 ...

  6. String(byte[] bytes, String charsetName)

    String str = new String("时之沙"); byte bytes[] = str.getBytes("GBK"); byte byte2[] ...

  7. byte[]->new String(byte[]) -> getByte()引发的不一致问题

    今天接短信接口,短信接口提供了sdk,我们可以直接用sdk发送请求然后发送对应短信. 但是想使用我们平台自定义的httpUtil实现. 然而忙了1天半,才解决这个问题,还是我同事帮忙找出问题并解决的. ...

  8. InputStream转换为String, byte[] data = new byte[1024]详解

    /** * This file created at 2018年2月28日. * * Copyright (c) 2002-2018 Bingosoft, Inc. All rights reserv ...

  9. C#string byte[] base64位互相转换

    byte表示字节,byte[]则表示存放一系列字节的数组 1个字符=2个字节(byte) 1个字节=8个比特(bit) 网速上所说的1M其实是指1兆的小b,1M= 1024b/8 = 128kb 下面 ...

随机推荐

  1. js装饰者模式

    装饰者模式是为已有的功能动态地添加更多功能的一种方式.当系统需要新功能的时候,是向旧的类中添加新的代码.这些新加的代码通常装饰了原有类的核心职责或主要行为,在主类中加入了新的字段,新的方法和新的逻辑, ...

  2. JS实现百叶窗效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. org.hibernate.TypeMismatchException: Provided id of the wrong type for class cn.itcast.entity.User. Expected: class java.lang.String, got class java.lang.Integer at org.hibernate.event.internal.Defau

    出现org.hibernate.TypeMismatchException: Provided id of the wrong type for class cn.itcast.entity.User ...

  4. 无法锁定管理目录(/var/lib/dpkg/),是否有其他进程正占用它

    dpkg应用程序被占用 错误提示: E: 无法获得锁 /var/lib/dpkg/lock – open (11: 资源暂时不可用) E: 无法锁定管理目录(/var/lib/dpkg/),是否有其他 ...

  5. EF-局部更新

    // ////1 public Task ReservedQuantity(long productId, long skuId, int reservedQuantity, long userId) ...

  6. getBytes()详解

    在java中,getBytes()方法如果不指定字符集,则得到的是一个操作系统默认的编码格式的字节数组:如果指定字符集,则得到的是在指定字符集下的字节数组,如: byte[] b_gbk = &quo ...

  7. Asp.net 异步调用WebService

    //服务代码 [WebMethod] public string Test(int sleepTimes, int val) { Thread.Sleep(sleepTimes); var log = ...

  8. Python之用虚拟环境隔离项目,并重建依赖关系

    下面将以安装django和mysqlclient介绍如何用虚拟环境隔离项目,并重建依赖关系.操作系统:windows 10:python版本:python3.7 1. 安装python虚拟环境 (1) ...

  9. LeetCode 369. Plus One Linked List

    原题链接在这里:https://leetcode.com/problems/plus-one-linked-list/ 题目: Given a non-negative number represen ...

  10. Python 修改ha配置文件

    任务要求: 1.用户输入字符串 {"backend": "test.oldboy.org","record":{"server&q ...