如何将 gi 范数整数(字符串格式)转换为十六进制格式? (C#)
给定一个潜在的巨大整数值(以 C# 字符串格式),我希望能够生成其等效的十六进制值。普通方法在这里不适用,因为我们讨论的是任意大的数字,50 位或更多。我见过的技术使用这样的技术:
// Store integer 182
int decValue = 182;
// Convert integer 182 as a hex in a string variable
string hexValue = decValue.ToString("X");
// Convert the hex string back to the number
int decAgain = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);
不会工作,因为要转换的整数太大。
例如,我需要能够将这样的字符串转换为:
843370923007003347112437570992242323
为其等效的十六进制字符串。
这些不起作用:
Given a potentially huge integer value (in C# string format), I want to be able to generate its hex equivalent. Normal methods don't apply here as we are talking arbitrarily large numbers, 50 digits or more. The techniques I've seen which use a technique like this:
// Store integer 182
int decValue = 182;
// Convert integer 182 as a hex in a string variable
string hexValue = decValue.ToString("X");
// Convert the hex string back to the number
int decAgain = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);
won't work because the integer to convert is too large.
For example I need to be able to convert a string like this:
843370923007003347112437570992242323
to its hex equivalent.
these don't work:
C# convert integer to hex and back again
How to convert numbers between hexadecimal and decimal in C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
哦,这很简单:
Oh, that's easy:
使用 BigInteger 存储整数,然后使用 .ToString("X") 。
示例:
但这仅限于 .NET 4 及更高版本。但是 Jens A. 指出了 codeproject 上的一个 BigInteger 类,该类包含一个名为
ToHexString
因此这适用于ToHexString
.NET 4 场景。Use a BigInteger to store the integer, and than use the .ToString("X") on that object.
Example:
This is however limited to .NET 4 and later. But Jens A. pointed to a BigInteger class on codeproject that class contains a method called
ToHexString
so that would work for a < .NET 4 scenario.正如 Jens 所说,请查看 代码项目。即使它们没有转换为十六进制的函数,您也可以轻松编写函数 自己做,只要这个 BigInt 有除法和模运算(我不认为它有模函数,所以你还需要编写 取模 自己)
As Jens said, take a look at the BigInt implementation on Code Project. Even if they don't have a function to convert to hex, you could easily write a function to do it yourself as long as this BigInt has a divide and modulo operation (I don't think it has a modulo function, so you would also need to write modulo yourself)
嘿到目前为止,在 stackoverflow 上有很好的 dec<->hex 转换解决方案,...但我需要(巨大的整数。巨大的分数)几乎没有精度丢失,所以我用我已经完成的代码修改了我找到的所有代码,这里是我可以分享一些(没有大的 int/real lib 使用)
PS 如果您需要截断小数位(以格式化数字),那么您必须按截断部分的最高有效位进行舍入。
如果您想知道此行的含义:
而不是计算与输入字符串精度匹配的小数位数(每个十六进制小数位有 1.205 个十进制小数位)。这个比率是通过对每小数部分高达 1280 位的精度进行经验测量而获得的。为了简单起见
1e-l 可以存储,最大误差可达 1e-(l+1)。该比率几乎是恒定的(除了低小数数字值(<16 位数字)之外,因此该公式可以安全地用于任何更大的数字位数。在低输入数字值中,输出错误最大为 1(>8 位数字)或最多 2 位(<=8 位)数字
heh nice solutions for dec<->hex conversions here on stackoverflow so far ,... but i needed (gigantic int . gigantic fraction) with almost none precision lost so i modded all codes i found with my already done codes and here is some i can share (without big int/real lib usage)
P.S. if you need to cut off fractional digits (to format numbers) than you have to round by most significant digit of the cutted part.
if you wonder what means this line:
than it compute the number of fractional digits that match input string precision (1.205 decimal fractional digits per hexadecimal fractional digit). This ratio i get by empirical measurement of accuracy up to 1280 bits per fractional part of number. for simplicity
1e-l can be stored with max error up to 1e-(l+1). This ratio is almost constant (except for low fractional digit values (<16 digits) so this formula can be used for any larger num of digits safely. In low input digit values is output wrong max by 1 (>8 digits) or max 2 (<=8 digits) digits