java下字串和位元組陣列如何轉換?

2021-06-05 22:25:51 字數 479 閱讀 6741

1、位元組陣列轉換為字串

byte bybuffer = new byte[20];

... ...

string strread = new string(bybuffer);

strread = string.copyvalueof(strread.tochararray(), 0, bybuffer.length]);

2、字串轉換成位元組陣列

byte bybuffer = new byte[200];

string strinput="abcdefg";

bybuffer= strinput.getbytes();

byte bybuffer = new byte[200];

string strinput="我是字串";

bybuffer= strinput.getbytes("gb2312");

字串 位元組陣列互轉

將byte轉換為string 或者將string轉換為byte author administrator public class byteorstringhelper param str 源字串轉換成位元組陣列的字串 return public static byte stringtobyte s...

C 字串到位元組陣列,位元組陣列轉整型

int num 12345 string num1 convert.tostring 12345,16 byte bytes bitconverter.getbytes num 將int32轉換為位元組陣列 num bitconverter.toint32 bytes,0 將位元組陣列內容再轉成in...

C 字串和位元組陣列轉換

定義string變數為str,記憶體流變數為ms,位元陣列為bt 1.字串轉位元陣列 1 byte bt system.text.encoding.default.getbytes 字串 2 byte bt convert.frombase64string 字串 2.字串轉流 1 memorystr...