如何(字符串格式)GI-安全​​完成整数转换为十六进制格式? (C#) [英] How to convert a gi-normous integer (in string format) to hex format? (C#)

查看:149
本文介绍了如何(字符串格式)GI-安全​​完成整数转换为十六进制格式? (C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个潜在的巨大的整数值(在C#字符串格式),我希望能够产生它的hex等值。正如我们所说任意大的数字,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

其十六进制等价的。

这些不工作:

C#转换整数十六进制和回一次
<一href=\"http://stackoverflow.com/questions/74148/how-to-convert-numbers-between-hex-and-decimal-in-c\">How以十六进制和十进制之间转换数字在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-安全​​完成整数转换为十六进制格式? (C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆