1. Will c # how the string is converted into a decimal number hexadecimal number string
// binary decimal turn Console.WriteLine ( "166 decimal binary representation:" + Convert.ToString (166, 2)); // octal, decimal turn Console.WriteLine ( "decimal representation of the octal 166:" + Convert.ToString ( 166, 8)); // turn decimal hex Console.WriteLine ( "decimal hex 166 indicates:" + Convert.ToString (166, 16)); // binary decimal Console.WriteLine ( " 111101 binary decimal representation: "+ Convert.ToInt32 (" 111101 ", 2)); // turn octal decimal Console.WriteLine (" decimal 44 octal representation: "+ Convert.ToInt32 (" 44 " , 8)); // turn hex decimal Console.WriteLine ( "CC hex decimal representation:" + Convert.ToInt32 ( "CC" , 16));
2. In the serial communication process, often use a string to a hexadecimal byte array and convert
string StringToHexString Private (string S, Encoding encode) { byte [] B = encode.GetBytes (S); // string encoding program according to the specified byte array string Result = string.Empty; for (int I = 0; I < b.Length; i ++) // changed for each byte hexadecimal characters, separated in% { Result + = "%" + Convert.ToString (B [I], 16); } return Result; } Private String HexStringToString (HS string, Encoding encode) { // string divided at%, and remove empty characters string [] = hs.Split chars (new new char [] { '%'}, StringSplitOptions.RemoveEmptyEntries); byte [] = new new B byte [chars.Length]; // changed character by character hexadecimal data bytes for (int I = 0; I <chars.Length; I ++) { B [I] = Convert.ToByte (chars [I], 16); } // the specified byte array becomes string encoding return encode.GetString (B); }
Hexadecimal byte array transfer string
/// <summary> /// 字符串转16进制字节数组 /// </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; }
Byte array to a hexadecimal string 16
/// <summary> /// 字节数组转16进制字符串 /// </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; }
Conversion from Chinese characters to hexadecimal
/// <Summary> /// characters converted from hex to 16 /// </ Summary> /// <param name = "S"> </ param> /// <param name = "charset"> coding as "UTF-. 8", "GB2312" </ param> /// <param name = "fenge"> Is each separated by a comma character </ param> /// <Returns> </ Returns> public static String ToHex (S String, String charset, BOOL fenge) { IF ((s.Length% 2) = 0!) { S + = ""; // space // 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(); }
Hex converted into Chinese characters
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); }