Java中如何正确的将byte[]数组转化为String类型?
很多人在编程时,总是喜欢用一下方法将数组转为字符串:(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类型?的更多相关文章
- java中的堆、栈、常量池以及String类型的两种声明
参考自http://blog.sina.com.cn/s/blog_798b04f90100ta67.html http://www.cnblogs.com/fguozhu/articles/2661 ...
- Java中两个或多个byte数组合并及int类型转数组
Java中两个或多个byte数组合并及int类型转数组 // 用list好处是可以未知多个? public static byte[] test(List<byte[]> values) ...
- 代码实现:定义一个文件输入流,调用read(byte[] b)方法,将a.txt文件中的内容打印出来(byte数组大小限制为5)
package com.loaderman.test; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; im ...
- Java中JNI的使用详解第三篇:JNIEnv类型中方法的使用
转自: http://blog.csdn.net/jiangwei0910410003/article/details/17466369 上一篇说道JNIEnv中的方法的用法,这一篇我们就来通过例子来 ...
- Java之byte、char和String类型相互转换
package basictype; /** * byte.char和String类型相互转换 */ public class CHJavaType { public static void main ...
- java中Arrays.sort()对二位数组进行排序
int [][]a = new int [5][2]; //定义一个二维数组,其中所包含的一维数组具有两个元素 对于一个已定义的二位数组a经行如下规则排序,首先按照每一个对应的一维数组第一个元素进行升 ...
- 将ImageList中的图片转化成byte数组
Image imgwd = this.imageList1.Images["wd.png"]; var bytes = ImageToBytes(imgwd); public by ...
- JAVA byte数组转化为16进制字符串输出
最简单的方法: 利用javax.xml.bind包下的DatatypeConverter printHexBinary public static java.lang.String printHexB ...
- Java中的方法(形参及实参)return返回类型
如何定义 Java 中的方法 所谓方法,就是用来解决一类问题的代码的有序组合,是一个功能模块. 一般情况下,定义一个方法的语法是: 其中: 1. 访问修饰符:方法允许被访问的权限范围, 可以是 pub ...
随机推荐
- 在Linux下面如何查看tomcat已经使用多少线程(Threads)
先用 ps aux |grep tomcat 查看tomcat的 PID 再用 ps -T -p <PID>|wc -l 查看线程
- dubbo源码分析(一)-从xml到我们认识的Java对象
项目中用的dubbo的挺多的,然后随着自己对dubbo的慢慢深入,自己也希望能够了解dubbo的底层实现,这半年来一直在看dubbo的源码,有点断断续续的,于是准备写一个dubbo源码系列的分析文章, ...
- Java Code Examples for org.codehaus.jackson.map.DeserializationConfig 配置
The following code examples are extracted from open source projects. You can click to vote up the e ...
- Unity运行错误代码处理
1.Unity在运行时出现如图错误,但不影响运行效果展示. 2.错误原因:代码不规范. 3.检查代码,查看变量是否定义正确.
- @ResponseBody中文乱码解决方案
java web项目,使用了springmvc4.0,用@ResponseBody返回中文字符串,乱码$$ 本以为很简单的问题,不过也找了一个小时. 网上有说这样配置的: <mvc:annota ...
- python全栈开发笔记---基本数据类型--数字型魔法
数字 int a1 =123 a2=456 int 讲字符串转换为数字 a = " #字符串 b = int(a) #将字符串转换成整形 b = b + 1000 #只有整形的时候才可以进 ...
- ON 子句和 WHERE 子句的不同
原文: https://www.cnblogs.com/zjfjava/p/6041445.html 即使你认为自己已对 MySQL 的 LEFT JOIN 理解深刻,但我敢打赌,这篇文章肯定能让你学 ...
- netcore 2.0 部署 到iis
.net Core2.0应用程序发布window服务器报错容易错过的配置. 1.应用程序发布. 2.IIS上新建网站. 3.应用程序池选择无托管代码. 4.服务器上安装DotNetCore.1.0.2 ...
- java8 字符串转换 list long Integer
String ids= "1,2,3,4,5,6"; List<Long> listIds = Arrays.asList(ids.split("," ...
- 国行 lg g3 D858 刷 lg g3 D858hk 教程(备忘)
纯手打,转载请注明出处~ 刷机有风险,出现问题概不负责! 本着自娱自乐的宗旨 ,分享一下,出了问题不负责! 准备的材料: 1,手机一枚(废话)国行lg g3 d858 2,root 工具 用来root ...