How to convert a gi-normous integer (in string format) to hex format? (C#)(如何将 gi-normous 整数(字符串格式)转换为十六进制格式?(C#))
问题描述
给定一个潜在的巨大整数值(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#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将 gi-normous 整数(字符串格式)转换为十六进制格式?(C#)


- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- 使用 rss + c# 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01