将整数转换为十六进制并再次转换回来
我该如何转换以下内容?
2934(整数)到 B76(十六进制)
让我解释一下我想要做什么。 我的数据库中有以整数形式存储的用户 ID。 我不想让用户引用他们的 ID,而是让他们使用十六进制值。 主要原因是因为它更短。
因此,我不仅需要从整数转换为十六进制,而且还需要从十六进制转换为整数。
在 C# 中是否有一种简单的方法可以做到这一点?
How can I convert the following?
2934 (integer) to B76 (hex)
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.
Is there an easy way to do this in C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
来自 http: //www.geekpedia.com/KB8_How-do-I-convert-from-decimal-to-hex-and-hex-to-decimal.html
提示(来自评论):
使用
.ToString("X4")
获取前导 0 的 4 位数字,或使用.ToString("x4")
获取小写十六进制数字(同样适用于更多数字) 。from http://www.geekpedia.com/KB8_How-do-I-convert-from-decimal-to-hex-and-hex-to-decimal.html
HINT (from the comments):
Use
.ToString("X4")
to get exactly 4 digits with leading 0, or.ToString("x4")
for lowercase hex numbers (likewise for more digits).使用:
请参阅如何:在十六进制字符串和数字类型之间进行转换(C# 编程指南) 了解更多信息和示例。
Use:
See How to: Convert Between Hexadecimal Strings and Numeric Types (C# Programming Guide) for more information and examples.
尝试以下将其转换为十六进制
并再次转换回来
Try the following to convert it to hex
And back again
不过,我真的质疑这个的价值。 您所说的目标是使值更短,它会的,但这本身并不是目标。 你真正的意思是要么让它更容易记住,要么更容易打字。
如果你的意思是更容易记住,那么你就退步了。 我们知道它的大小仍然相同,只是编码不同。 但您的用户不会知道这些字母仅限于“AF”,因此 ID 将为他们占据相同的概念空间,就像允许使用字母“AZ”一样。 因此,它不像记住电话号码,而更像记住 GUID(同等长度)。
如果您指的是打字,那么用户现在必须使用键盘的主要部分,而不是能够使用键盘。 打字可能会更困难,因为他们的手指无法识别这个单词。
更好的选择是让他们选择一个真实的用户名。
I really question the value of this, though. You're stated goal is to make the value shorter, which it will, but that isn't a goal in itself. You really mean either make it easier to remember or easier to type.
If you mean easier to remember, then you're taking a step backwards. We know it's still the same size, just encoded differently. But your users won't know that the letters are restricted to 'A-F', and so the ID will occupy the same conceptual space for them as if the letter 'A-Z' were allowed. So instead of being like memorizing a telephone number, it's more like memorizing a GUID (of equivalent length).
If you mean typing, instead of being able to use the keypad the user now must use the main part of the keyboard. It's likely to be more difficult to type, because it won't be a word their fingers recognize.
A much better option is to actually let them pick a real username.
转为十六进制:
转为整数:
To Hex:
To int:
在找到这个答案之前,我创建了自己的解决方案,用于将 int 转换为十六进制字符串并返回。 毫不奇怪,它比 .net 解决方案快得多,因为代码开销更少。
计时代码:
结果:
I created my own solution for converting int to Hex string and back before I found this answer. Not surprisingly, it's considerably faster than the .net solution since there's less code overhead.
Timing code:
Results:
网络框架
帕斯卡>> C#
将旧派非常古老的 pascal 程序转换为 C# 的东西
NET FRAMEWORK
PASCAL >> C#
Something from the old school very old procedure of pascal converted to C #
用零填充以十六进制值打印整数(如果需要):
https://learn.microsoft.com/en-us/dotnet/standard/base-types/how-to-pad-a-number-with-leading -零
Print integer in hex-value with zero-padding (if needed) :
https://learn.microsoft.com/en-us/dotnet/standard/base-types/how-to-pad-a-number-with-leading-zeros
就像@Joel C,我认为这是一个AB 问题。
我认为有一种现有的算法更适合描述的需要,即 uuencode,我确信它有许多公共域实现,也许经过调整以消除看起来非常相似的字符,例如 0/O。 可能会产生明显更短的字符串。 我认为这就是 URL 缩短器所使用的。
Like @Joel C, I think this is an AB problem.
There’s an existing algorithm that I think suits the need as described better which is uuencode, which I’m sure has many public domain implementations, perhaps tweaked to eliminate characters that looks very similar like 0/O. Likely to produce significantly shorter strings. I think this is what URL shorteners use.
这是很久以前的作业问题的答案……就像当时还只有 3 部星球大战电影一样。 我确信它并不完美。 看来我们对算法更感兴趣。 我向 main 添加了一些内容,看看返回的内容是否与标准函数的功能相同。 需要一个反向字符串函数..简单。 从命令行“./a.out 123”等调用它。当然,要信任但要验证。 最好的
This was an answer to a homework problem from long ago .. like when there were still only 3 Star Wars movies. I am sure it is not perfect. It seems we were more interested in algorithms. I added some stuff to main to see if returns what the standard functions do. Need a reverse string function .. easy peasy. Call it from the command line "./a.out 123" etc. Trust but verify, of course. Best
整数转十六进制:
十六进制到整数:
int to hex:
hex to int: