麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > C# > 正文

C# 16進(jìn)制與字符串、字節(jié)數(shù)組之間的轉(zhuǎn)換

2019-10-29 21:48:31
字體:
供稿:網(wǎng)友
在串口通訊過程中,經(jīng)常要用到 16進(jìn)制與字符串、字節(jié)數(shù)組之間的轉(zhuǎn)換
 
 

 

復(fù)制代碼代碼如下:

/// <summary> 
/// 字符串轉(zhuǎn)16進(jìn)制字節(jié)數(shù)組 
/// </summary> 
/// <param name="hexString"></param> 
/// <returns></returns> 
private static byte[] strToToHexByte(string hexString) 

hexString = hexString.Replace(" ", ""); 
if ((hexString.Length % 2) != 0) 
hexString += " "; 
byte[] returnBytes = new byte[hexString.Length / 2]; 
for (int i = 0; i < returnBytes.Length; i++) 
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16); 
return returnBytes; 

字節(jié)數(shù)組轉(zhuǎn)16進(jìn)制字符串 
/// <summary> 
/// 字節(jié)數(shù)組轉(zhuǎn)16進(jìn)制字符串 
/// </summary> 
/// <param name="bytes"></param> 
/// <returns></returns> 
public static string byteToHexStr(byte[] bytes) 

string returnStr = ""; 
if (bytes != null) 

for (int i = 0; i < bytes.Length; i++) 

returnStr += bytes[i].ToString("X2"); 


return returnStr; 

從漢字轉(zhuǎn)換到16進(jìn)制 
/// <summary> 
/// 從漢字轉(zhuǎn)換到16進(jìn)制 
/// </summary> 
/// <param name="s"></param> 
/// <param name="charset">編碼,如"utf-8","gb2312"</param> 
/// <param name="fenge">是否每字符用逗號分隔</param> 
/// <returns></returns> 
public static string ToHex(string s, string charset, bool fenge) 

if ((s.Length % 2) != 0) 

s += " ";//空格 
//throw new ArgumentException("s is not valid chinese string!"); 

System.Text.Encoding chs = System.Text.Encoding.GetEncoding(charset); 
byte[] bytes = chs.GetBytes(s); 
string str = ""; 
for (int i = 0; i < bytes.Length; i++) 

str += string.Format("{0:X}", bytes[i]); 
if (fenge && (i != bytes.Length - 1)) 

str += string.Format("{0}", ","); 


return str.ToLower(); 

16進(jìn)制轉(zhuǎn)換成漢字 
///<summary> 
/// 從16進(jìn)制轉(zhuǎn)換成漢字 
/// </summary> 
/// <param name="hex"></param> 
/// <param name="charset">編碼,如"utf-8","gb2312"</param> 
/// <returns></returns> 
public static string UnHex(string hex, string charset) 

if (hex == null) 
throw new ArgumentNullException("hex"); 
hex = hex.Replace(",", ""); 
hex = hex.Replace("/n", ""); 
hex = hex.Replace("//", ""); 
hex = hex.Replace(" ", ""); 
if (hex.Length % 2 != 0) 

hex += "20";//空格 

// 需要將 hex 轉(zhuǎn)換成 byte 數(shù)組。 
byte[] bytes = new byte[hex.Length / 2]; 
for (int i = 0; i < bytes.Length; i++) 

try 

// 每兩個字符是一個 byte。 
bytes[i] = byte.Parse(hex.Substring(i * 2, 2), 
System.Globalization.NumberStyles.HexNumber); 

catch 

// Rethrow an exception with custom message. 
throw new ArgumentException("hex is not a valid hex number!", "hex"); 


System.Text.Encoding chs = System.Text.Encoding.GetEncoding(charset); 
return chs.GetString(bytes); 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 91精品最新国内在线播放 | 国产精品久久久久久久模特 | 九九精品视频观看 | 亚洲国产超高清a毛毛片 | 午夜神马电影网 | 午夜视频国产 | 最新精品在线 | 精品国产观看 | 久久精品亚洲一区二区 | 91精品国 | 看一级毛片| 好骚综合在线 | 亚洲一区二区三区四区精品 | 欧美一级视频免费看 | 特片网久久 | 亚洲不卡 | 日韩黄色免费在线观看 | 精品一区二区在线观看视频 | 免费看性xxx高清视频自由 | 国产精品久久久久久久久久东京 | 曰批全过程120分钟免费69 | 久色成人 | 久久久久久中文字幕 | 青久草视频 | 亚洲网视频 | 久久成人免费观看 | 亚洲一区二区三区高清 | 久久骚| 草久影视| 日本精品久久久久久草草 | 亚洲精品 在线播放 | 黄色成年在线观看 | 国产手机av在线 | 欧美日本日韩 | 日韩av在线网| 日本aaaa片毛片免费观蜜桃 | 成人午夜在线播放 | 91久久国产综合久久91猫猫 | 成人福利在线视频 | 久久精品亚洲国产奇米99 | 亚洲午夜精选 |