常用的一些轉換方法,整理如下,在編程中,用得比較多的是十進制跟十六進制,二進制跟八進制用的很少。
namespace Jerry.Framework.Socket.Common
{
public class Utility
{
/// <summary>
/// 將指定進制的字符串轉換到指定進制的字符串
/// </summary>
/// <param name="value">要轉換的字符串</param>
/// <param name="fromBase">value 中數字的基數,它必須是 2、8、10 或 16。</param>
/// <param name="toBase">value 中數字的基數,它必須是 2、8、10 或 16。</param>
/// <returns></returns>
public static string ToString(string value, int fromBase, int toBase)
{
return Convert.ToString(Convert.ToInt32(value, fromBase), toBase);
}
/// <summary>
/// 將字節數組節轉換成指定進制的字符串
/// </summary>
/// <param name="byteArray">要轉換的字符串</param>
/// <param name="toBase">它必須是 2 或 16</param>
/// <returns></returns>
public static string ToString(byte[] byteArray, int toBase = 16)
{
if (byteArray == null || byteArray.Length == 0)
return string.Empty;
switch (toBase)
{
case 2:
return ByteArrayToBinaryString(byteArray);
case 16:
return ByteArrayToHexStr(byteArray);
default:
return string.Empty;
}
}
/// <summary>
/// 將指定進制的字符串轉換成字節數組
/// </summary>
/// <param name="value"></param>
/// <param name="fromBase"></param>
/// <returns></returns>
public static byte[] ToByteArray(string value, int fromBase = 16)
{
if (string.IsNullOrEmpty(value))
return null;
switch (fromBase)
{
case 2:
return BinaryStringToByteArray(value);
case 16:
return HexStrToByteArray(value);
default:
return null;
}
}
/// <summary>
/// 字節數組節轉換成二進制字符串
/// </summary>
/// <param name="b">要轉換的字節數組</param>
/// <returns></returns>
private static string ByteArrayToBinaryString(byte[] byteArray)
{
int capacity = byteArray.Length * 8;
StringBuilder sb = new StringBuilder(capacity);
for (int i = 0; i < byteArray.Length; i++)
{
sb.Append(byte2BinString(byteArray[i]));
}
return sb.ToString();
}
private static string byte2BinString(byte b)
{
return Convert.ToString(b, 2).PadLeft(8, '0');
}
/// <summary>
/// 二進制字符串轉換成字節數組
/// </summary>
/// <param name="binaryString">要轉換的字符,如"00000000 11111111"</param>
/// <returns></returns>
private static byte[] BinaryStringToByteArray(string binaryString)
{
binaryString = binaryString.Replace(" ", "");
if ((binaryString.Length % 8) != 0)
throw new ArgumentException("二進制字符串長度不對");
byte[] buffer = new byte[binaryString.Length / 8];
for (int i = 0; i < buffer.Length; i++)
{
buffer[i] = Convert.ToByte(binaryString.Substring(i * 8, 8).Trim(), 2);
}
return buffer;
}
/// <summary>
/// 字節數組轉換成十六進制字符串
/// </summary>
/// <param name="bytes">要轉換的字節數組</param>
/// <returns></returns>
private static string ByteArrayToHexStr(byte[] byteArray)
{
int capacity = byteArray.Length * 2;
StringBuilder sb = new StringBuilder(capacity);
if (byteArray != null)
{
for (int i = 0; i < byteArray.Length; i++)
{
sb.Append(byteArray[i].ToString("X2"));
}
}
return sb.ToString();
}
/// <summary>
/// 十六進制字符串轉換成字節數組
/// </summary>
/// <param name="hexString">要轉換的字符串</param>
/// <returns></returns>
private static byte[] HexStrToByteArray(string hexString)
{
hexString = hexString.Replace(" ", "");
if ((hexString.Length % 2) != 0)
throw new ArgumentException("十六進制字符串長度不對");
byte[] buffer = new byte[hexString.Length / 2];
for (int i = 0; i < buffer.Length; i++)
{
buffer[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Trim(), 0x10);
}
return buffer;
}
}
}