Solved

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

Posted on 2013-11-06
4
963 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

810 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