位元組陣列和字元之間互換

2021-05-22 13:16:28 字數 906 閱讀 1127

定義string變數為str,記憶體流變數為ms,位元陣列為bt

1.字串轉位元陣列

(1)byte bt=system.text.encoding.default.getbytes("字串");

(2)byte bt=convert.frombase64string("字串");

2.字串轉流

(1)memorystream ms=new memorystream(system.text.encoding.default.getbytes("字串"));

(2)memorystream ms=new memorystream(convert.frombase64string("字串"));

3.流轉位元陣列

(1)byte bt=ms.toarray();

(2)memorystream ms=new memorystream();ms.write(bt,0,ms.length);

4.流轉字串

(1)string str=convert.tobase64string(ms.toarray());

(2)string str=system.text.encoding.default.getstring(ms.toarray());

5.位元陣列轉字串

(1)string str=system.text.encoding.default.getstring(bt);

(2)string str=convert.tobase64string(bt);

6.位元陣列轉流

(1)memorystream ms=new memorystream(bt);

(2)memorystream ms=new memorystream();ms.read(bt,0,bt.lenght);

C 中字元陣列,位元組陣列和string之間的轉化

ndc networkdiskclient 的介面和後台程式之間用socket通訊,傳送命令。環境 介面 c winform 後台 訊息通知 網路通訊,是通過網路位元組流進行傳輸的,傳輸內容是有報文頭的 訊息。報文頭是struct結構體,先轉化成byte,protobuf訊息就先轉換為記憶體流,再s...

字串 位元組陣列 字元陣列之間的轉換

package stringexample 1.字串與基本資料型別 包裝類之間的轉換 字串 基本資料型別 包裝類 呼叫相應的包裝類的pa xx string str 基本資料型別 包裝類 字串 字串過載的valueof 方法 2.字串與位元組陣列 字串 位元組陣列 字串的getbytes 位元組陣列...

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

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