java string转byte_java中String类型的如何转为byte[]

展开全部

一、String转byte数组简单32313133353236313431303231363533e78988e69d8331333366306436版:

1、String str = "abcd";

2、byte[] bs = str.getBytes();

二、复杂版

// pros - no need to handle UnsupportedEncodingException // pros - bytes in specified

encoding scheme byte[] utf8 = "abcdefgh".getBytes(StandardCharsets.UTF_8);

System.out.println("length of byte array in UTF-8 : " + utf8.length);

System.out.println("contents of byte array in UTF-8: " + Arrays.toString(utf8));

Output : length of byte array in UTF-8 : 8 contents of byte array in UTF-8: [97, 98, 99, 100, 101, 102, 103, 104]1

b4d1354ce42b71d9d98a61038c2b2e24.png

扩展资料:

反过来,将Byte数组转化为String的方法

using System;

using System.Text;

public static string FromASCIIByteArray(byte[] characters)

{

ASCIIEncoding encoding = new ASCIIEncoding( );

string constructedString = encoding.GetString(characters);

return (constructedString);

}

·

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值