给定一个潜在的巨大整数值(C# 字符串格式),我希望能够生成它的十六进制等效值.普通方法在这里不适用,因为我们谈论的是任意大的数字,50 位或更多.我见过的技术使用这样的技术:
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
843370923007003347112437570992242323
到它的十六进制等价物.
to its hex equivalent.
这些不起作用:
C# 将整数转换为十六进制并再次返回如何在 C# 中转换十六进制和十进制之间的数字?
哦,很简单:
var s = "843370923007003347112437570992242323";
var result = new List<byte>();
result.Add( 0 );
foreach ( char c in s )
{
int val = (int)( c - '0' );
for ( int i = 0 ; i < result.Count ; i++ )
{
int digit = result[i] * 10 + val;
result[i] = (byte)( digit & 0x0F );
val = digit >> 4;
}
if ( val != 0 )
result.Add( (byte)val );
}
var hex = "";
foreach ( byte b in result )
hex = "0123456789ABCDEF"[ b ] + hex;
这篇关于如何将 gi-normous 整数(字符串格式)转换为十六进制格式?(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!