16進製制與字串 位元組陣列之間的轉換

2021-07-31 03:52:16 字數 2806 閱讀 6805

2.在串列埠通訊過程中,經常要用到 16進製制與字串、位元組陣列之間的轉換

//

private string stringtohexstring(string s,encoding encode)

return result;

}private string hexstringtostring(string hs, encoding encode)

,stringsplitoptions.removeemptyentries);

byte b = new byte[chars.length];

//逐個字元變為16進製制位元組資料

for (int i = 0; i < chars.length; i++)

//按照指定編碼將位元組陣列變為字串

return encode.getstring(b);

} ///

///字串轉16進製制位元組陣列

//////

///private

static

byte

strtotohexbyte(

string

hexstring)

//////位元組陣列轉16進製制字串

//////

///public

static

string

bytetohexstr(

byte

bytes)

}return

returnstr;

}///

///從漢字轉換到16進製制

//////

///編碼,如"utf-8","gb2312"

///是否每字元用逗號分隔

///public

static

string

tohex(

string

s, string

charset,

bool

fenge)

system.text.encoding chs

=system.text.encoding.getencoding(charset);

byte

bytes

=chs.getbytes(s);

string

str =""

;for

(inti =

0; i

<

bytes.length; i++)

", bytes[i]);

if(fenge

&&(i

!=bytes.length -1

))", "

,");}

}return

str.tolower();

}///

///從16進製制轉換成漢字

//////

///編碼,如"utf-8","gb2312"

///public

static

string

unhex(

string

hex,

string

charset)

//需要將 hex 轉換成 byte 陣列。

byte

bytes

=new

byte

[hex.length /2

];for

(inti =

0; i

<

bytes.length; i++)

catch

}system.text.encoding chs

=system.text.encoding.getencoding(charset);

return

chs.getstring(bytes);

}#region 字串和byte之間的轉化

//////

數字和位元組之間互轉

//////

///public

static

int inttobitconverter(int

num)

//////

將字串轉為16進製制字元,允許中文

//////

//////

public

static

string stringtohexstring(string s, encoding encode ,string

spanstring)

return

result; }

//////

將16進製制字串轉為字串

//////

//////

public

static

string hexstringtostring(string

hs, encoding encode)

//按照指定編碼將位元組陣列變為字串

return

encode.getstring(b); }

//////

byte轉為16進製制字串

//////

///public

static

string bytetohexstr(byte

bytes) }

return

returnstr; }

//////

將16進製制的字串轉為byte

//////

///public

static

byte strtohexbyte(string

hexstring)

#endregion

16進製制字串轉位元組陣列

16進製制的字串表示轉成位元組陣列 param hexstring 16進製制格式的字串 return 轉換後的位元組陣列 public static byte hexstr2bytearray string hexstring return bytearray 16進製制字串轉換成byte陣列 p...

16進製制與字串 位元組陣列之間的轉換(一)

1 1.請問c 中如何將十進位制數的字串轉化成十六進製制數的字串 23 十進位制轉二進位制 4 console.writeline 十進位制166的二進位制表示 convert.tostring 166,2 5 十進位制轉八進位制 6 console.writeline 十進位制166的八進位制表示...

C 16進製制與字串 位元組陣列之間的轉換

在串列埠通訊過程中,經常要用到 16進製制與字串 位元組陣列之間的轉換 字串轉16進製制位元組陣列 private static byte strtotohexbyte string hexstring 位元組陣列轉16進製制字串 public static string bytetohexstr ...