很多人在编程时,总是喜欢用一下方法将数组转为字符串:(a为byte数组)

    String s=a.toString();

可是每次返回的时候,新手看来返回的结果是乱码,比如说我,写RSA算法时,没有注意,就以为是解密出来的乱码(哈哈哈),但其实[B@1b6d3586 为@+hash值,这个时候要知道对于返回一个String对象,new一个是基本上不会错的,测试代码如下:

         Scanner scan=new Scanner(System.in);
String s="ghhhh";
byte[]a=s.getBytes();
String s1=a.toString();
String s2=new String(a);
System.out.println("s1:"+s1);
System.out.println("s2:"+s2);

测试结果:

 s1:[B@1b6d3586
s2:ghhhh

可以看到s1所对应的方法只是返回hash值,而s2才真正返回了a的实体值。

这是因为,String java.lang.Object.toString()返回的确实是hash值,介绍如下:

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

 getClass().getName() + '@' + Integer.toHexString(hashCode())

大体意思就是类Object的toString方法返回一个字符串,该字符串由对象为实例的类的名称、符号符号符号“@”和对象哈希代码的无符号十六进制表示组成。换句话说,此方法返回一个字符串。

  因此,下次不要用错方法咯!

Java中如何正确的将byte[]数组转化为String类型?的更多相关文章

  1. java中的堆、栈、常量池以及String类型的两种声明

    参考自http://blog.sina.com.cn/s/blog_798b04f90100ta67.html http://www.cnblogs.com/fguozhu/articles/2661 ...

  2. Java中两个或多个byte数组合并及int类型转数组

    Java中两个或多个byte数组合并及int类型转数组 // 用list好处是可以未知多个? public static byte[] test(List<byte[]> values) ...

  3. 代码实现:定义一个文件输入流,调用read(byte[] b)方法,将a.txt文件中的内容打印出来(byte数组大小限制为5)

    package com.loaderman.test; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; im ...

  4. Java中JNI的使用详解第三篇:JNIEnv类型中方法的使用

    转自: http://blog.csdn.net/jiangwei0910410003/article/details/17466369 上一篇说道JNIEnv中的方法的用法,这一篇我们就来通过例子来 ...

  5. Java之byte、char和String类型相互转换

    package basictype; /** * byte.char和String类型相互转换 */ public class CHJavaType { public static void main ...

  6. java中Arrays.sort()对二位数组进行排序

    int [][]a = new int [5][2]; //定义一个二维数组,其中所包含的一维数组具有两个元素 对于一个已定义的二位数组a经行如下规则排序,首先按照每一个对应的一维数组第一个元素进行升 ...

  7. 将ImageList中的图片转化成byte数组

    Image imgwd = this.imageList1.Images["wd.png"]; var bytes = ImageToBytes(imgwd); public by ...

  8. JAVA byte数组转化为16进制字符串输出

    最简单的方法: 利用javax.xml.bind包下的DatatypeConverter printHexBinary public static java.lang.String printHexB ...

  9. Java中的方法(形参及实参)return返回类型

    如何定义 Java 中的方法 所谓方法,就是用来解决一类问题的代码的有序组合,是一个功能模块. 一般情况下,定义一个方法的语法是: 其中: 1. 访问修饰符:方法允许被访问的权限范围, 可以是 pub ...

随机推荐

  1. selinux介绍/状态查看/开启/关闭

    SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制的实现,是 Linux历史上最杰出的新安全子系统--百度百科. 基于经验来说SELinux在 ...

  2. error: http://ppa.launchpad.net lucid Release: The following signatures couldn't be verified because

    ubuntu 命令行sudo apt-get update W: GPG error: http://ppa.launchpad.net lucid Release: The following si ...

  3. Zookeeper的实际应用

    Zookeeper是hadoop的一个子项目,虽然源自hadoop,但是我发现zookeeper脱离hadoop的范畴开发分布式框架的运用越来越多.今天我想谈谈zookeeper,本文不谈如何使用zo ...

  4. 网卡驱动-BD详解(缓存描述符 Buffer Description)

    DMA介绍(BD的引入) 网络设备的核心处理模块是一个被称作 DMA(Direct Memory Access)的控制器,DMA 模块能够协助处理器处理数据收发.对于数据发送来说,它能够将组织好的数据 ...

  5. Java日期时间,以及相互转换

    Java日期时间,以及相互转化 package com.study.string; import java.text.ParseException; import java.text.SimpleDa ...

  6. linux图形和命令界面切换

    一.系统不在虚拟机中的情况 使用ctrl+alt+F1~6切换到命令行界面:ctrl+alt+F7切换到图形界面 二.系统在虚拟机中的情况 Ctrl+Alt+shift+F1~6切换到命令行界面:使用 ...

  7. SpringMVC解析Json字符串

    不同第三方jar对json串的解析效果不同. 1. json包 <dependency> <groupId>org.json</groupId> <artif ...

  8. 第三篇 功能实现(2) (Android学习笔记)

    第三篇 功能实现(2) ●Activity的四种启动模式 Activity的启动模式有四种,分别是standard.singleTop.singleTask和singleInstance. 在Andr ...

  9. (C/C++学习笔记) 二十. 文件和流

    二十. 文件和流 ● 文件的概念 文件(file) 一. C/C++语言将文件作为字节序列(sequence of characters)来对待,但从编码角度,或说从对字节信息的解释来看,文件分为:文 ...

  10. Cracking The Coding Interview2.4

    删除前面的linklist,使用node来表示链表 // You have two numbers represented by a linked list, where each node cont ...