10/27, 2021 https://yaoqs.github.io/20211027/c-16-jin-zhi-yu-zi-fu-chuan-zi-jie-shu-zu-zhi-jian-de-zhuan-huan/ post word count: 916 words. post estimate read time: 4 min
C-16 进制与字符串、字节数组之间的转换
Yao Qingsheng Department of Natural/Social Philosophy & Infomation Sciences, CHINA
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. thrownew ArgumentException("hex is not a valid hex number!", "hex"); } } System.Text.Encoding chs = System.Text.Encoding.GetEncoding(charset); return chs.GetString(bytes); }
生成不重复随机数的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
publicclassRandomHelper { ///<summary> ///生成制定位数的随机码(数字) ///</summary> ///<param name="length"></param> ///<returns></returns> publicstaticstringGenerateRandomCode(int length) { var result = new StringBuilder(); for (var i = 0; i < length; i++) { var r = new Random(Guid.NewGuid().GetHashCode()); result.Append(r.Next(0, 10)); } return result.ToString(); } }
References *Corresponding Author: Yao Qing-sheng <Email: 350788415@qq.com> BY-NC-SA .
Yao Qing-sheng.C-16 进制与字符串、字节数组之间的转换.FUTURE & CIVILIZATION.Natural/Social Philosophy & Infomation Sciences,20211027.https://yaoqs.github.io/20211027/c-16-jin-zhi-yu-zi-fu-chuan-zi-jie-shu-zu-zhi-jian-de-zhuan-huan/
版权声明:本文为「LordYao」的原创文章,遵循 CC 4.0 BY-NC-SA 版权协议,转载请附上原文出处链接及本声明。 原文链接: https://yaoqs.github.io/20211027/c-16-jin-zhi-yu-zi-fu-chuan-zi-jie-shu-zu-zhi-jian-de-zhuan-huan/