Solved

How to convert a 39-decimal characters into hex in c#

Posted on 2013-11-06
4
937 Views
Last Modified: 2013-11-06
Hi experts
I've a string representation of a 39-characters long decimal integer.
I need to convert this into hex format in C#.
eg. 340282366920938463463374607431768211455 (39 characters)
      -> ffffffffffffffffffffffffffffffff (32 characters)

By the same token, I need to be able to reverse the operation from hex to decimal.

I am using C# .NET 3.5 which hasn't got BigInteger.

Any advice please.  Thanks in advance.
0
Comment
Question by:dominicwong
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
QuinnDex earned 500 total points
ID: 39629440
the code for doing this in strings is below but if you want to work in numbers try intX

https://github.com/devoyster/IntXLib


decimal string to hex string

        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;

Open in new window



hex string to decimal string

static string HexToDecimal(string hex)
{
    List<int> dec = new List<int> { 0 };   // decimal result

    foreach (char c in hex)
    {
        int carry = Convert.ToInt32(c.ToString(), 16);   
            // initially holds decimal value of current hex digit;
            // subsequently holds carry-over for multiplication

        for (int i = 0; i < dec.Count; ++i)
        {
            int val = dec[i] * 16 + carry;
            dec[i] = val % 10;
            carry = val / 10;
        }

        while (carry > 0)
        {
            dec.Add(carry % 10);
            carry /= 10;
        }
    }

    var chars = dec.Select(d => (char)('0' + d));
    var cArr = chars.Reverse().ToArray();
    return new string(cArr);
}

Open in new window

0
 
LVL 40
ID: 39629453
There is a BigInteger. It is "hidden" in the System.Numerics namespace, available after referencing the dll by the same name.

If you can get a string representation of your value, the following will do the job:

BigInteger bigInt = BigInteger.Parse ( "340282366920938463463374607431768211455" );
string bigHex = bigInt.ToString ("X");
bigInt = BigInteger.Parse ( bigHex, System.Globalization.NumberStyles.HexNumber );

Otherwise, give a look at the BigInteger Structure in the documentation, it is quite exhaustive.
0
 
LVL 9

Expert Comment

by:QuinnDex
ID: 39629466
BigInteger is part of .net 4, OP is working with .net 3.5
0
 

Author Closing Comment

by:dominicwong
ID: 39629467
Thanks QuinnDex. That works like a charm, and is exactly what I need.

Thanks also to JamesBurger for your help.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now