日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# 16进制与字符串、字节数组之间的转换

發布時間:2025/5/22 C# 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 16进制与字符串、字节数组之间的转换 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在串口通訊過程中,經常要用到 16進制與字符串、字節數組之間的轉換

?

字符串轉16進制字節數組?1?????????private?static?byte[]?strToToHexByte(string?hexString)
?2?????????{
?3?????????????hexString?=?hexString.Replace("?",?"");
?4?????????????if?((hexString.Length?%?2)?!=?0)
?5?????????????????hexString?+=?"?";
?6?????????????byte[]?returnBytes?=?new?byte[hexString.Length?/?2];
?7?????????????for?(int?i?=?0;?i?<?returnBytes.Length;?i++)
?8?????????????????returnBytes[i]?=?Convert.ToByte(hexString.Substring(i?*?2,?2),?16);
?9?????????????return?returnBytes;
10?????????}

?

字節數組轉16進制字符串?1?????????public?static?string?byteToHexStr(byte[]?bytes)
?2?????????{
?3?????????????string?returnStr?=?"";
?4?????????????if?(bytes?!=?null)
?5?????????????{
?6?????????????????for?(int?i?=?0;?i?<?bytes.Length;?i++)
?7?????????????????{
?8?????????????????????returnStr?+=?bytes[i].ToString("X2");
?9?????????????????}
10?????????????}
11?????????????return?returnStr;
12?????????}

?

從16進制轉換成漢字?1?????????public?static?string?UnHex(string?hex,?string?charset)
?2?????????{
?3?????????????if?(hex?==?null)
?4?????????????????throw?new?ArgumentNullException("hex");
?5?????????????hex?=?hex.Replace(",",?"");
?6?????????????hex?=?hex.Replace("/n",?"");
?7?????????????hex?=?hex.Replace("//",?"");
?8?????????????hex?=?hex.Replace("?",?"");
?9?????????????if?(hex.Length?%?2?!=?0)
10?????????????{
11?????????????????hex?+=?"20";//空格
12?????????????}
13?????????????//?需要將?hex?轉換成?byte?數組。?
14?????????????byte[]?bytes?=?new?byte[hex.Length?/?2];
15?
16?????????????for?(int?i?=?0;?i?<?bytes.Length;?i++)
17?????????????{
18?????????????????try
19?????????????????{
20?????????????????????//?每兩個字符是一個?byte。?
21?????????????????????bytes[i]?=?byte.Parse(hex.Substring(i?*?2,?2),
22?????????????????????System.Globalization.NumberStyles.HexNumber);
23?????????????????}
24?????????????????catch
25?????????????????{
26?????????????????????//?Rethrow?an?exception?with?custom?message.?
27?????????????????????throw?new?ArgumentException("hex?is?not?a?valid?hex?number!",?"hex");
28?????????????????}
29?????????????}
30?????????????System.Text.Encoding?chs?=?System.Text.Encoding.GetEncoding(charset);
31?????????????return?chs.GetString(bytes);
32?????????}

?

從漢字轉換到16進制

?1?public?static?string?ToHex(string?s,?string?charset,?bool?fenge)
?2?????????{
?3?????????????if?((s.Length?%?2)?!=?0)
?4?????????????{
?5?????????????????s?+=?"?";//空格
?6?????????????????//throw?new?ArgumentException("s?is?not?valid?chinese?string!");
?7?????????????}
?8?????????????System.Text.Encoding?chs?=?System.Text.Encoding.GetEncoding(charset);
?9?????????????byte[]?bytes?=?chs.GetBytes(s);
10?????????????string?str?=?"";
11?????????????for?(int?i?=?0;?i?<?bytes.Length;?i++)
12?????????????{
13?????????????????str?+=?string.Format("{0:X}",?bytes[i]);
14?????????????????if?(fenge?&&?(i?!=?bytes.Length?-?1))
15?????????????????{
16?????????????????????str?+=?string.Format("{0}",?",");
17?????????????????}
18?????????????}
19?????????????return?str.ToLower();
20?????????}

?

轉載于:https://www.cnblogs.com/eric_ibm/archive/2012/01/14/2322413.html

總結

以上是生活随笔為你收集整理的C# 16进制与字符串、字节数组之间的转换的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。