将整数转换为十六进制并再次返回 [英] Convert integer to hexadecimal and back again

查看:72
本文介绍了将整数转换为十六进制并再次返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何转换以下内容?

2934(整数)到B76(十六进制)

2934 (integer) to B76 (hex)

我解释一下我要做什么。我的数据库中有用户ID,它们存储为整数。与其让用户引用其ID,不如让他们使用十六进制值。主要原因是因为它更短。

Let me explain what I am trying to do. I have User IDs in my database that are stored as integers. Rather than having users reference their IDs I want to let them use the hex value. The main reason is because it's shorter.

因此,我不仅需要从整数转换为十六进制,而且还需要从十六进制转换为整数。

So not only do I need to go from integer to hex but I also need to go from hex to integer.

在C#中有一种简便的方法吗?

Is there an easy way to do this in C#?

推荐答案

// Store integer 182
int intValue = 182;
// Convert integer 182 as a hex in a string variable
string hexValue = intValue.ToString("X");
// Convert the hex string back to the number
int intAgain = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);

来自 http://www.geekpedia.com/KB8_How-do-I-convert-from-decimal-to -hex-and-hex-to-decimal.html

这篇关于将整数转换为十六进制并再次返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆