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#)
基础教程推荐
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
