//int转16进制 (long类型同样) int a=90; string str= convert.tostring(a,16); //5a //int转2进制 int a=90; string str= convert.tostring(a,2); //1011010 //int转byte int a=90; string str= Convert.Tobyte(a); //0x5a //16进制转10 string hex = "5a"; string aa = Convert.ToInt32(hex,16).ToString();// 90 //16进制转2进制 string hex = "5a"; string aa = Convert.ToString(Convert.ToInt32(hex, 16), 2); //1011010 //十六进制字符串转十进制 public string ConvertHexToIntStr(string hexstr) { if (hexstr.StartsWith("0x")) { hexstr = hexstr.Substring(2); } //如果不是有效的16进制字符串或者字符串长度大于16或者是空,均返回NULL if (!IsHexadecimal(hexstr) || hexstr.Length > 16 || string.IsNullOrEmpty(hexstr)) { return null; } if (hexstr.Length > 8) { return Convert.ToInt64(hexstr, 16).ToString(); } else if (hexstr.Length > 4) { return Convert.ToInt32(hexstr, 16).ToString(); } else { return Convert.ToInt16(hexstr, 16).ToString(); } } // 判断是否是十六进制格式字符串 public bool IsHexadecimal(string str) { const string PATTERN = @"[A-Fa-f0-9]+$"; return System.Text.RegularExpressions.Regex.IsMatch(str, PATTERN); }
原文地址:https://www.cnblogs.com/zhengxia/p/11784795.html
时间: 2024-11-09 00:31:47