How to use Unsigned 256 bit integers(uint256) in C#

Andreas ss 726 Reputation points
2021-09-17T13:16:58.81+00:00

Hello!

I wonder how it would be possible to calculate using Unsigned 256 bit integers(uint256) in C#, Visual Studio 2019.
(A uint256 variable has a maximum value of Math.Pos(2,256) – 1)

I have looked at BigInteger but are not sure if this can be used somehow or if I need to use another solution or library of some sort?
The image shows code that is written in solidity and in that language it is possible to use uint256.

I have written down an example calculation which is my goal to do in C# somehow. I have just put uint256 as declaration to illustrate what I am trying to do.

How can we calculate this in C#?
It would be needed to show a working example of the below calculation exactly because if I change uint256 to BigInteger I get compile error on line 10 that says:
Operator '/' cannot be applied to operands of type 'BigInteger' and 'double'

I am not sure if this is possible either to do and that bigdouble5 return the correct number?
double bigdouble5 = Math.Pow(2, 128)

Thank you!

                                    uint256 bigint1 = 329974300448100608374211110737048701521;  
                                    uint256 bigint2 = 326010339881230390929338722651861109232;  
                                    uint256 bigint3 = 92097159224555409225;  
                                    int decimals = 18;  
                                    uint256 bigint4 = bigint1 - bigint2;  
  
                                    double bigdouble5 = Math.Pow(2, 128); //returns very large number  
                                    double bigdouble6 = Math.Pow(10, decimals);  
  
                                    double NUM = ((bigint4 - 0) / bigdouble5) * bigint3 / bigdouble6; //Returns a normal double value like: "0.8654" (this it not the correct result though)  

Parts of original code from Solidity that I try to put in C#
133155-image.png

Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 90,686 Reputation points
    2021-09-17T14:16:22.843+00:00

    I did this quick test with BigInteger, ... but not 100% sure with all those big numbers... =>

    System.Numerics.BigInteger bigint1 = System.Numerics.BigInteger.Parse("329974300448100608374211110737048701521");
    System.Numerics.BigInteger bigint2 = System.Numerics.BigInteger.Parse("326010339881230390929338722651861109232");
    System.Numerics.BigInteger bigint3 = System.Numerics.BigInteger.Parse("92097159224555409225");
    int decimals = 18;
    System.Numerics.BigInteger bigint4 = bigint1 - bigint2;
    
    System.Numerics.BigInteger bigint5 = System.Numerics.BigInteger.Pow(2, 128); //returns very large number
    System.Numerics.BigInteger bigint6 = System.Numerics.BigInteger.Pow(10, decimals);
    
    double nDiv = Math.Exp(System.Numerics.BigInteger.Log(bigint4) - System.Numerics.BigInteger.Log(bigint5));
    double nResult = (double)(nDiv * (double)bigint3) / (double)bigint6;
    

2 additional answers

Sort by: Most helpful
  1. Binh Thanh Nguyen 0 Reputation points
    2023-07-23T19:43:09.1533333+00:00

    Hi. I used this code to get the maximum value of Unsigned 256.

    The value max has the correct value of 115792089237316195423570985008687907853269984665640564039457584007913129639935

    byte[] bytes = new byte[] {
    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255
    };
    BigInteger max = new BigInteger(bytes, true);
    
    0 comments No comments

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.