天天看点

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

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

扩展资料:

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

using System;

using System.Text;

public static string FromASCIIByteArray(byte[] characters)

{

ASCIIEncoding encoding = new ASCIIEncoding( );

string constructedString = encoding.GetString(characters);

return (constructedString);

}

·