博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java 字符串 字节数组_字符串到字节数组,字节数组到Java中的字符串
阅读量:2533 次
发布时间:2019-05-11

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

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[] elements		System.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 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的全部内容。

. 签出更多数组示例。

Reference:

参考:

翻译自:

java 字符串 字节数组

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

你可能感兴趣的文章
一、 kettle开发、上线常见问题以及防错规范步骤
查看>>
eclipse没有server选项
查看>>
CRC码计算及校验原理的最通俗诠释
查看>>
QTcpSocket的连续发送数据和连续接收数据
查看>>
使用Gitbook来编写你的Api文档
查看>>
jquery扩展 $.fn
查看>>
Markdown指南
查看>>
influxDB的安装和简单使用
查看>>
JPA框架学习
查看>>
JPA、JTA、XA相关索引
查看>>
机器分配
查看>>
php opcode缓存
查看>>
springcloud之Feign、ribbon设置超时时间和重试机制的总结
查看>>
Go 结构体
查看>>
LINQ巩固
查看>>
观看杨老师(杨旭)Asp.Net Core MVC入门教程记录
查看>>
UIDynamic(物理仿真)
查看>>
Windows下安装Redis
查看>>
迷宫实现
查看>>
【字符编码】Java字符编码详细解答及问题探讨
查看>>