C# hexadecimal conversion

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

1 ask c# how string decimal number converted to hexadecimal number sixteen.

//Decimal to binary

Console.WriteLine("The binary representation of the decimal 166: "+Convert.ToString(166, 2));

//Decimal to octal

Console.WriteLine("Decimal, octal representation of 166: "+Convert.ToString(166, 8));

//Decimal to hexadecimal sixteen

Console.WriteLine("Decimal 166 sixteen hexadecimal representation: "+Convert.ToString(166, 16));

//Binary to decimal

Console.WriteLine("The binary 111101 decimal representation: "+Convert.ToInt32("111101", 2));

//Octal to decimal

Console.WriteLine("Octal 44 decimal representation: "+Convert.ToInt32("44", 8));

//Sixteen hexadecimal to decimal

Console.WriteLine("Sixteen hexadecimal CC decimal representation: "+Convert.ToInt32("CC", 16));

2 serial communication process, often use the 16 hexadecimal byte array and string, conversion between

//

private string StringToHexString(string s,Encoding encode)

{

byte[] b = encode.GetBytes(s);//According to the specified code string programming byte array

string result = string.Empty;

for (int i = 0; i <b.Length; i++)//The byte by byte to 16 hexadecimal characters, separated in%

{

result += "%"+Convert.ToString(b[i], 16);

}

return result;

}

private string HexStringToString(string hs, Encoding encode)

{

//By division of the string, and remove the null character

string[] chars = hs.Split(new char[]{'%'},StringSplitOptions.RemoveEmptyEntries);

byte[] b = new byte[chars.Length];

//Characters one by one to 16 hexadecimal byte data

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

{

b[i] = Convert.ToByte(chars[i], 16);

}

//According to the specified byte array into a string encoding

return encode.GetString(b);

}


String of 16 hexadecimal byte array

/// <summary>

/// String of 16 hexadecimal byte array

/// </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;

}



An array of bytes to 16 hexadecimal string

/// <summary>

/// An array of bytes to 16 hexadecimal string

/// </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;

}



From Chinese characters conversion to base 16

/// <summary>

/// From Chinese characters conversion to base 16

/// </summary>

/// <param name="s"></param>

/// <param name="charset">Coding, such as"utf-8","gb2312"</param>

/// <param name="fenge">If every character with a comma</param>

/// <returns></returns>

public static string ToHex(string s, 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();

}



16 hexadecimal conversion into Chinese characters

///<summary>

/// Conversion from 16 system into Chinese characters

/// </summary>

/// <param name="hex"></param>

/// <param name="charset">Coding, such as"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";//Space

}

// Need to convert the hex into the byte array.

byte[] bytes = new byte[hex.Length / 2];

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

{

try

{

// Each of the two characters is a 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);

}

 

 

 

 

 /// <summary>

/// String of 16 hexadecimal byte array

/// </summary>

/// <param name="hexString"></param>

/// <returns></returns>

private static byte[] strToToHexByte(string hexString = "00445255303231323334233030303126313231313031313331352631323131303131333235263130233030303226313231313031313331372631323131303131333339263237")

{

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);

Decoder dest = Encoding.GetEncoding("gb2312").GetDecoder();

char[] rst = new char[hexString.Length / 2];

dest.GetChars(returnBytes, 0, returnBytes.Length, rst, 0);

string aa = new string(rst);

return returnBytes;

}

 

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Martha at November 18, 2013 - 1:56 AM