C# VB 实现10进制 16进制之间互相转换
时间:2023-06-24 14:22:55
方法1:
int d=10;
d.ToString("x") //或把x改为X,,,就变成了16位的字符串了.
int x=Convert.ToInt32(d.ToString("x"),16);//把16进制的字符串变回10进制的.
方法2:
static void Main()
{
int i = 446;
string hex = i.ToString( "X" /* or x * );
Console.WriteLine( hex );
int j = HexToInt( hex );
Console.WriteLine( j );
}
static int HexToInt(string hex)
{
hex = Regex.Replace(hex, "^0x", "", RegexOptions.IgnoreCase);
if (Regex.IsMatch(hex, "[g-z]", RegexOptions.IgnoreCase))
{
throw new Exception("Invalid Hexadecimal Expression.: 0x" + hex);
}
char[] chars = hex.ToUpper().ToCharArray();
Array.Reverse(chars);
int dec = 0;
for (int i = 0; i < chars.Length; i++)
{
dec += HexMapping(chars[i]) * (int)Math.Pow(16, i);
}
return dec;
}
static int HexMapping(char c)
{
switch (c)
{
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
case 'A':
return 10;
case 'B':
return 11;
case 'C':
return 12;
case 'D':
return 13;
case 'E':
return 14;
case 'F':
return 15;
default :
throw new Exception("Invalid Hexadecimal Character :" + c.ToString());
}
}
标签:10进制转,16进制
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解决CollectionUtils.isNotEmpty()不存在的问题
2021-11-15 12:03:17
Android 8.0系统中应用图标的适配微技巧
2022-09-29 00:22:26
![](https://img.aspxhome.com/file/2023/0/138200_0s.jpg)
SpringCloud Alibaba项目实战之nacos-server服务搭建过程
2022-02-28 02:43:26
![](https://img.aspxhome.com/file/2023/2/85542_0s.jpg)
C#判断系统是32位还是64位的方法
2022-04-01 12:44:04
![](https://img.aspxhome.com/file/2023/9/108799_0s.png)
腾讯云部署javaWeb项目的实现步骤
2023-11-05 18:03:29
![](https://img.aspxhome.com/file/2023/1/58911_0s.png)
idea生成类注释和方法注释的正确方法(推荐)
2022-09-11 03:45:14
![](https://img.aspxhome.com/file/2023/4/85554_0s.jpg)
Java后端Tomcat实现WebSocket实例教程
2023-08-22 17:31:09
![](https://img.aspxhome.com/file/2023/0/58340_0s.png)
.NET实现父窗体关闭而不影响子窗体的方法
2022-10-06 20:56:04
![](https://img.aspxhome.com/file/2023/4/104264_0s.jpg)
SpringBoot返回多种格式的数据的实现示例
2023-11-24 14:22:52
![](https://img.aspxhome.com/file/2023/0/59700_0s.png)
Java中List.contains(Object object)方法使用
2022-04-25 20:47:11
开源一个c# 新的雪花算法
2022-01-20 16:08:56
java使double保留两位小数的多方法 java保留两位小数
2023-06-17 14:01:30
C#如何解析http报文
2022-11-22 23:20:56
![](https://img.aspxhome.com/file/2023/6/85696_0s.png)
c# FTP上传文件实例代码(简易版)
2023-09-08 11:43:56
Android实现环形进度条代码
2023-06-08 11:09:55
![](https://img.aspxhome.com/file/2023/9/138129_0s.jpg)
APK程序获取system权限的方法
2023-09-05 06:41:49
Android实现ListView分页加载数据
2023-11-09 10:05:45
Android编程学习之抽象类AbsListView用法实例分析
2023-03-18 12:19:49
Android的HTTP类库Volley入门学习教程
2022-06-08 11:56:51
![](https://img.aspxhome.com/file/2023/3/139483_0s.png)
Java中JSON处理工具类使用详解
2023-09-19 17:59:08