Solved

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

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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 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