C#實現(xiàn)字符串進制轉換方法匯總
C# 字符串進制轉換
/// <summary> /// 進制轉換 /// </summary> /// <param name="input"></param> /// <param name="fromType">原來的進制格式</param> /// <param name="toType">要轉換成的進制格式</param> /// <returns></returns> public string ConvertGenericBinary(string input, byte fromType, byte toType) { string output = input; switch (fromType) { case 2: output = ConvertGenericBinaryFromBinary(input, toType); break; case 8: output = ConvertGenericBinaryFromOctal(input, toType); break; case 10: output = ConvertGenericBinaryFromDecimal(input, toType); break; case 16: output = ConvertGenericBinaryFromHexadecimal(input, toType); break; default: break; } return output; } /// <summary> /// 從二進制轉換成其他進制 /// </summary> /// <param name="input"></param> /// <param name="toType"></param> /// <returns></returns> private string ConvertGenericBinaryFromBinary(string input, byte toType) { switch (toType) { case 8: //先轉換成十進制然后轉八進制 input = Convert.ToString(Convert.ToInt32(input, 2), 8); break; case 10: input = Convert.ToInt32(input, 2).ToString(); break; case 16: input = Convert.ToString(Convert.ToInt32(input, 2), 16); break; default: break; } return input; } /// <summary> /// 從八進制轉換成其他進制 /// </summary> /// <param name="input"></param> /// <param name="toType"></param> /// <returns></returns> private string ConvertGenericBinaryFromOctal(string input, byte toType) { switch (toType) { case 2: input = Convert.ToString(Convert.ToInt32(input, 8), 2); break; case 10: input = Convert.ToInt32(input, 8).ToString(); break; case 16: input = Convert.ToString(Convert.ToInt32(input, 8), 16); break; default: break; } return input; } /// <summary> /// 從十進制轉換成其他進制 /// </summary> /// <param name="input"></param> /// <param name="toType"></param> /// <returns></returns> private string ConvertGenericBinaryFromDecimal(string input, int toType) { string output = ""; int intInput = Convert.ToInt32(input); switch (toType) { case 2: output = Convert.ToString(intInput, 2); break; case 8: output = Convert.ToString(intInput, 8); break; case 16: output = Convert.ToString(intInput, 16); break; default: output = input; break; } return output; } /// <summary> /// 從十六進制轉換成其他進制 /// </summary> /// <param name="input"></param> /// <param name="toType"></param> /// <returns></returns> private string ConvertGenericBinaryFromHexadecimal(string input, int toType) { switch (toType) { case 2: input = Convert.ToString(Convert.ToInt32(input, 16), 2); break; case 8: input = Convert.ToString(Convert.ToInt32(input, 16), 8); break; case 10: input = Convert.ToInt32(input, 16).ToString(); break; default: break; } return input; }
C#進制轉換方法匯總
進制轉換匯總
1.十進制數(shù)轉二進制數(shù)(結果:11)
string s1 = Convert.ToString(3, 2);
2.十進制數(shù)轉二進制數(shù)(結果:0011) (左側補位方法,轉八進制,轉十六進制……與之類似)
string s2 = Convert.ToString(3, 2).PadLeft(4, '0');
3.十進制數(shù)轉二進制數(shù)(結果:1100) (右側補位方法,轉八進制,轉十六進制……與之類似)
string s3 = Convert.ToString(3, 2).PadRight(4, '0');
4.十六進制轉二進制數(shù)(結果:00001111)
string s4 = Convert.ToString(0xf, 2).PadLeft(8, '0'); ? ? ? ? ? ? string s5 = Convert.ToString(0xF, 2).PadLeft(8, '0'); ?//不區(qū)分大小寫,結果一樣。
5.十六進制轉十進制數(shù)
? string s6 = Convert.ToString(0xf, 10); ? ? ? ? ? ? ? ? ?//結果:15,string類型 ? ? ? ? ? ? string s7 = Convert.ToString(0xf, 10).PadLeft(4, '0'); ?//結果:0015,string類型 ? ? ? ? ? ? int s8 = Convert.ToInt32("f", 16); ? ? ? ? ? ? ? ? ? ? //結果:15,int類型
6.二進制轉十進制(結果:15)
int s9 = Convert.ToInt32("1111", 2);
7.十六進制轉十進制(結果:48)
? int s10 = Convert.ToInt32("30", 16);
8.十進制轉十六進制(結果:f)
string s12 = string.Format("{0:x0}", 15); ? ? ? ? ? ? // 第2種方法: ? ? ? ? ? ? string s12 = convert.ToString(15,16);?
9.二進制轉十六進制(結果:f)
? string s13 = string.Format("{0:x}", Convert.ToInt32("1111", 2));
10.字符轉十進制數(shù)(結果:65)
? int s14 = Convert.ToInt32('A');
11.將字符串中第1個字符轉成十進制數(shù)(結果:65)
? string s15 = "AB"; ? ? ? ? ? ? int s16 = Convert.ToInt32(Convert.ToChar(s15[0])); ? ? ? ? ? ? Console.WriteLine(s16);
12.十六進制轉浮點數(shù)(結果:0.68)
? byte[] bt = new byte[4] { 0x7b, 0x14, 0x2e, 0x3f }; ? ? ? ? ? ? float ff = BitConverter.ToSingle(bt, 0);
13,浮點數(shù)轉十六進制數(shù)
float f = 0.68f; ? ? ? ? ? ? string str1 = BitConverter.ToString(BitConverter.GetBytes(f)).Replace("-", ""); //結果:7B142E3F ? ? ? ? ? ? string str2 = BitConverter.ToString(BitConverter.GetBytes(f)); ? ?//結果:7B-14-2E-3F
14.int 轉字符串
int n = 49; ? ? ? ? ? ? string str1 = n.ToString(); ? ? //結果:49 ? ? ? ? ? ? string str2 = n.ToString("x"); ?//結果:0x31
15.字符串轉int
?string str = "31"; ? ? ? ? ? ? int i = Convert.ToInt32(str); ?//結果:31
16.byte 轉 int
?byte bt = 0x31; ? ? ? ? ? ? int i = Convert.ToInt32(bt); ?//結果;49
17.ushort[]轉換為float類型
ushort[] val ; ? ? ? ? ? ? ? ? ? List<byte> result = new List<byte>(); ? ? ? ? ? ?result.AddRange(BitConverter.GetBytes(val[0])); ? ? ? ? ? ?result.AddRange(BitConverter.GetBytes(val[1])); ? ? ? ? ? ?float JG= BitConverter.ToSingle(result.ToArray(), 0);
18.float轉換為ushort[]類型
?float value; ? ? ? ? ? ?ushort lowOrderValue = BitConverter.ToUInt16(BitConverter.GetBytes(value), 2); ? ? ? ? ? ?ushort highOrderValue = BitConverter.ToUInt16(BitConverter.GetBytes(value), 0);
到此這篇關于C#實現(xiàn)字符串進制轉換的文章就介紹到這了,更多相關C# 字符串進制轉換內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C# StreamReader類實現(xiàn)讀取文件的方法
這篇文章主要介紹了C# StreamReader類實現(xiàn)讀取文件的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01