博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何将Java String转换为byte []?
阅读量:2379 次
发布时间:2019-05-10

本文共 3148 字,大约阅读时间需要 10 分钟。

本文翻译自:

Is there any way to convert Java String to a byte[] ( not the boxed Byte[] )? 有没有办法将Java String转换为byte[]不是盒装的Byte[] )?

In trying this: 在尝试这个:

System.out.println(response.split("\r\n\r\n")[1]);System.out.println("******");System.out.println(response.split("\r\n\r\n")[1].getBytes().toString());

and I'm getting separate outputs. 而我正在获得单独的输出。 Unable to display 1st output as it is a gzip string. 无法显示第一个输出,因为它是一个gzip字符串。

******[B@38ee9f13

The second is an address. 第二个是地址。 Is there anything I'm doing wrong? 有什么我做错了吗? I need the result in a byte[] to feed it to gzip decompressor, which is as follows. 我需要在byte[]中将结果提供给gzip decompressor,如下所示。

String decompressGZIP(byte[] gzip) throws IOException {    java.util.zip.Inflater inf = new java.util.zip.Inflater();    java.io.ByteArrayInputStream bytein = new java.io.ByteArrayInputStream(gzip);    java.util.zip.GZIPInputStream gzin = new java.util.zip.GZIPInputStream(bytein);    java.io.ByteArrayOutputStream byteout = new java.io.ByteArrayOutputStream();    int res = 0;    byte buf[] = new byte[1024];    while (res >= 0) {        res = gzin.read(buf, 0, buf.length);        if (res > 0) {            byteout.write(buf, 0, res);        }    }    byte uncompressed[] = byteout.toByteArray();    return (uncompressed.toString());}

#1楼

参考:


#2楼

String example = "Convert Java String";  byte[] bytes = example.getBytes();

#3楼

The object your method decompressGZIP() needs is a byte[] . 您的方法decompressGZIP()需要的对象是byte[]

So the basic, technical answer to the question you have asked is: 因此,您提出的问题的基本技术答案是:

byte[] b = string.getBytes();byte[] b = string.getBytes(Charset.forName("UTF-8"));byte[] b = string.getBytes(StandardCharsets.UTF_8); // Java 7+ only

However the problem you appear to be wrestling with is that this doesn't display very well. 然而,你似乎正在努力解决的问题是,这并不能很好地显示出来。 Calling toString() will just give you the default Object.toString() which is the class name + memory address. 调用toString()只会给你默认的Object.toString() ,这是类名+内存地址。 In your result [B@38ee9f13 , the [B means byte[] and 38ee9f13 is the memory address, separated by an @ . 在你的结果[B@38ee9f13[B表示byte[]38ee9f13是内存地址,用@分隔。

For display purposes you can use: 出于显示目的,您可以使用:

Arrays.toString(bytes);

But this will just display as a sequence of comma-separated integers, which may or may not be what you want. 但这只会显示为逗号分隔的整数序列,这可能是您想要的也可能不是。

To get a readable String back from a byte[] , use: 要从byte[]获取可读的String ,请使用:

String string = new String(byte[] bytes, Charset charset);

The reason the Charset version is favoured, is that all String objects in Java are stored internally as UTF-16. Charset版本受青睐的原因是Java中的所有String对象都在内部存储为UTF-16。 When converting to a byte[] you will get a different breakdown of bytes for the given glyphs of that String , depending upon the chosen charset. 转换为byte[]您将获得该String的给定字形的不同字节细分,具体取决于所选的字符集。


#4楼

您可以使用String.getBytes()返回byte[]数组。


#5楼

Try using String.getBytes(). 尝试使用String.getBytes()。 It returns a byte[] representing string data. 它返回表示字符串数据的byte []。 Example: 例:

String data = "sample data";byte[] byteData = data.getBytes();

#6楼

Simply: 只是:

String abc="abcdefghight";byte[] b = abc.getBytes();

转载地址:http://nuexb.baihongyu.com/

你可能感兴趣的文章
哪些领域的应用因为大数据变得更聪明?
查看>>
大数据驱动电信运营商转型
查看>>
玩转大数据 运动员如何用科技提升成绩
查看>>
广发银行试水大数据 “精细服务”现雏形
查看>>
大数据让社区生活更方便
查看>>
借助互联网大数据打假
查看>>
东信北邮大数据项目获2014中国通信学会科学技术一等奖
查看>>
大数据塑造新时代公共外交
查看>>
海-两篇
查看>>
整理硬盘
查看>>
ERP&SCM&MES發展歷程
查看>>
风-----
查看>>
系统Server架构图
查看>>
我的简历
查看>>
一种自适应的柔性制造系统优化调度算法
查看>>
现代管理思想与总图设计
查看>>
原创BPR之企业流程分析模型图 FDD
查看>>
PLM技术促进现代模具企业精益化和规模化
查看>>
独一无二的IFS CAD与PDM集成工具发布
查看>>
BPR-FDD 模型图原始档
查看>>