java 字符串 字节数组

Today we will learn how to convert String to byte array in java. We will also learn how to convert byte array to String in Java.

今天,我们将学习如何在Java中将String转换为字节数组。 我们还将学习如何在Java中将字节数组转换为String。

字符串到字节数组 (String to byte array)

We can use String class getBytes() method to encode the string into a sequence of bytes using the platform’s default charset. This method is overloaded and we can also pass Charset as argument.

我们可以使用String类的getBytes()方法使用平台的默认字符集将字符串编码为字节序列。 此方法已重载,我们也可以将Charset作为参数传递。

Here is a simple program showing how to convert String to byte array in java.

这是一个简单的程序,显示了如何在Java中将String转换为字节数组。

package com.journaldev.util;import java.util.Arrays;public class StringToByteArray {public static void main(String[] args) {String str = "PANKAJ";byte[] byteArr = str.getBytes();// print the byte[] elementsSystem.out.println("String to byte array: " + Arrays.toString(byteArr));}
}

Below image shows the output when we run the above program.

下图显示了运行上述程序时的输出。

We can also get the byte array using the below code.

我们还可以使用以下代码获取字节数组。

byte[] byteArr = str.getBytes("UTF-8");

However if we provide Charset name, then we will have to either catch UnsupportedEncodingException exception or throw it. Better approach is to use StandardCharsets class introduced in Java 1.7 as shown below.

但是,如果提供了字符集名称,则必须捕获UnsupportedEncodingException 异常或将其抛出。 更好的方法是使用Java 1.7中引入的StandardCharsets类,如下所示。

byte[] byteArr = str.getBytes(StandardCharsets.UTF_8);

That’s all the different ways to convert String to byte array in java.

在Java中,这是将String转换为字节数组的所有不同方法。

Java字节数组到字符串 (Java byte array to String)

Let’s look at a simple program showing how to convert byte array to String in Java.

让我们看一个简单的程序,该程序显示如何在Java中将字节数组转换为String。

package com.journaldev.util;public class ByteArrayToString {public static void main(String[] args) {byte[] byteArray = { 'P', 'A', 'N', 'K', 'A', 'J' };byte[] byteArray1 = { 80, 65, 78, 75, 65, 74 };String str = new String(byteArray);String str1 = new String(byteArray1);System.out.println(str);System.out.println(str1);}
}

Below image shows the output produced by the above program.

下图显示了以上程序产生的输出。

Did you notice that I am providing char while creating the byte array?

您是否注意到创建字节数组时提供了char?

It works because of autoboxing and char ‘P’ is being converted to 80 in the byte array. That’s why the output is the same for both the byte array to string conversion.

由于自动装箱而起作用,并且char'P'在字节数组中被转换为80。 这就是为什么字节数组到字符串转换的输出相同的原因。

String also has a constructor where we can provide byte array and Charset as an argument. So below code can also be used to convert byte array to String in Java.

字符串还有一个构造函数,我们可以在其中提供字节数组和Charset作为参数。 因此,以下代码也可以用于在Java中将字节数组转换为String。

String str = new String(byteArray, StandardCharsets.UTF_8);

String class also has a method to convert a subset of the byte array to String.

String类还具有一种将字节数组的子集转换为String的方法。

byte[] byteArray1 = { 80, 65, 78, 75, 65, 74 };
String str = new String(byteArray1, 0, 3, StandardCharsets.UTF_8);

Above code is perfectly fine and ‘str’ value will be ‘PAN’. That’s all about converting byte array to String in Java.

上面的代码非常好,“ str”值为“ PAN”。 这就是在Java中将字节数组转换为String的全部内容。

GitHub Repository.GitHub存储库中签出更多数组示例。

Reference: getBytes API Doc

参考: getBytes API文档

翻译自: https://www.journaldev.com/770/string-byte-array-java

java 字符串 字节数组

java 字符串 字节数组_字符串到字节数组,字节数组到Java中的字符串相关推荐

  1. C# 传递数组参数_一维数组_二维数组

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. java声明不可变数组_如何使数组元素在Java中不可变?

    不,您不能使数组的元素不变. 但是java.util.Collections类的unmodifiableList()方法接受List接口的一个对象(实现其类的对象),并返回给定对象的不可修改形式.用户 ...

  3. java 最大子数组_求一个数组中子数组的最大和算法(Java实现)

    前几天在微信订阅号"待字闺中"中看到的一篇文章<小技巧求一个数组中子数组的最大和>,提供下Java的实现,并且在对题目做下小修改,本来打算直接在微信里直接回复,但是发现 ...

  4. arrays合并两个数组_九章算法 | 字节跳动面试题:合并k个排序数组

    将 k 个有序数组合并为一个大的有序数组. 在线评测地址:LintCode 领扣 样例 1: Input: [[1, 3, 5, 7],[2, 4, 6],[0, 8, 9, 10, 11]] Out ...

  5. java异常断点数组_使用IDEA异常断点来定位java.lang.ArrayStoreException的问题

    前言 最近对 base-spring-boot项目进行了升级.在将其用于应用开发中时遇到java.lang.ArrayStoreException的异常导致程序无法启动.平常开发过程中面对这种描述不够 ...

  6. 创建数组_如何创建数组

    js数组 js的数组不是典型的数组 典型的数组 元素的数据类型相同 使用连续的内存储存 通过数字下标获取元素 但是js的数组不这样 元素的数据类型可以不同 内存不一定连续的(对象是随机储存的) 不可以 ...

  7. c++ 二维数组_二维数组的声明2019_04_18

    -------------[感谢小郡提供的图片] [广告位招租] ---------------------------------------------------------------- -- ...

  8. c++ 构造函数数组_从 JS 数组操作到 V8 array.js

    前言 最近在写面试编程题,经常用到数组,经常想偷个懒,用它提供的方法,奈何还是对数组方法使用不熟练,导致写了很多的垃圾代码,很多地方稍加修改的话肯定变得简洁高效优雅? 所以✍这篇文章本着了解一下Jav ...

  9. @value 数组_深入PHP数组

    1.数组创建 索引数组:数组下标是数字 //自动分配: $cars=array("Volvo","BMW","SAAB"); //手动分配1 ...

最新文章

  1. html列表中float-left,floatleft的作用
  2. 2020年人工神经网络第二次作业-参考答案第八题
  3. 超过efficientnet
  4. HyperLink的使用
  5. 磁盘阵列简介---图片说明
  6. Java线程:新特征-有返回值的线程(转)
  7. Codeforces Round #263 (Div. 2) D. Appleman and Tree 树形dp
  8. 异常的分类以及什么异常触发回滚
  9. P5708 【深基2.习2】三角形面积
  10. 【转】傅里叶分析之掐死教程(完整版)
  11. Maven打包war报错
  12. jsp+java中小学排课系统
  13. 深度学习卷积算法指南
  14. OpenCV联合Image Watch使用中关于RGB和HSV转换相关知识点
  15. 用在线工具processOn画UML的用例图和时序图
  16. CSS3 border-radius 属性
  17. SQL Server 使用数据库发送邮件(sp_send_dbmail)
  18. 学 Rust 要几天?「GitHub 热点速览 v.22.51」
  19. Python3.6安装 pip安装 BeautifulSoup安装
  20. 1.华为设备CPU过高排查过程

热门文章

  1. iOS8 获取通知设置状态
  2. 软件过程与项目管理第二周作业
  3. 【Xamarin挖墙脚系列:Xamarin的核心】
  4. 【java基础】重载与重写
  5. params 有什么用?
  6. [恢]hdu 2138
  7. 绕过SQL注入限制的方法
  8. [转载] Python3 如何检查字符串是否是以指定子字符串开头或结尾
  9. [转载] 用python统计中文字符数_使用Python统计字符串中各种字符的个数
  10. [转载] [Python] np.ones_like(ndarray)和np.zeros_like(ndarray)