C 中string與byte 的轉換幫助類

2021-08-29 06:42:47 字數 874 閱讀 2289

在寫c#程式時,string和byte之間的轉換比較煩,在移植一些老程式時感覺很不好。我在c#中使用des和tripledes時移植一塊老**時也遇到了同樣的情況。為了下次不為同樣的事情煩惱,就寫了下面的幫助類。

主要實現了以下的函式

**中出現的sidle是我的網名。

/* * @author wuerping

* @version 1.0

* @date 2004/11/30

* @description:

*/ using system;

using system.text;

namespace sidlehelper

else

c = convert.tobyte(bhex[i]&0x0f);

if(c < 0x0a)

else }

} public static byte hex2hexbin(byte bhex, int nlen)

#endregion

#region 陣列和字串之間的轉化

public static byte str2arr(string s)

public static string arr2str(byte buffer)

public static byte str2ascarr(string s)

public static byte str2hexascarr(string s)

public static string ascarr2str(byte b)

public static string hexascarr2str(byte buffer)

#endregion

} } 

byte與string底層強轉

用關鍵字進行強轉,底層會發生拷貝,兩個變數互不影響。示例 s 012345 b byte s s1 string b fmt.println s1 b 0 9 fmt.println s1 run test 強轉 012345 012345 pass test 強轉 0.00s pass1 byte...

C 中byte 與string的轉換

1 system.text.unicodeencoding converter new system.text.unicodeencoding byte inputbytes converter.getbytes inputstring string inputstring converter.ge...

C 中byte 與string的轉換

1 system.text.unicodeencoding converter new system.text.unicodeencoding byte inputbytes converter.getbytes inputstring string inputstring converter.ge...