java打印byte数组,我怎么打印字节一个字节数组的内容?

本文探讨了在加密过程中将字符串转换为字节,然后存储到数据库中,以及如何从数据库获取加密字符串并进行解密。重点在于如何在不改变原始字符串的情况下,将字符串安全地转换回字节。示例代码展示了使用默认字符集进行转换,并通过Arrays.toString()方法打印字节数组。

I use encrypt(byte) code for input String and then save encrypt(String) in DB.

I get String encrypt from DB to decrypt it, but I need to cast String to byte without changing because decrypt get just byte.

I used s.getBytes(); but it changed it,

I need some code to cast string to byte without changing the String.

Thank you so much.

解决方案

getBytes() doesn't change the string, it encodes string into a sequence of bytes using the platform's default charset.

In order to print the array of bytes as a String value,

String s = new String(bytes);

Edit:

it seems as you want to print the string as bytes, for which you can use

Arrays.toString(bytes)

See this code,

String yourString = "This is an example text";

byte[] bytes = yourString.getBytes();

String decryptedString = new String(bytes);

System.out.println("Original String from bytes: " + decryptedString);

System.out.println("String represented as bytes : " + Arrays.toString(bytes));

Output,

Original String from bytes: This is an example text

String represented as bytes : [84, 104, 105, 115, 32, 105, 115, 32, 97, 110, 32, 101, 120, 97, 109, 112, 108, 101, 32, 116, 101, 120, 116]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值