Giter Site home page Giter Site logo

sundryutilty's People

Contributors

liangjianyi avatar

sundryutilty's Issues

代码生成的建议

每个 BigIntegerToXxx 方法的逻辑都有高度的模式重合,建议 pass-compile IL rewrite 进行 CodeGen。

转换为有符号整数的函数模版:
public static T BigIntegerToXxx(this BigInteger bi) { byte[] bytes = bi.ToByteArray(); if (bytes.Length == **sizeof(T)**) { return BitConverter.To**Xxx**(bytes); } else if (bytes.Length == **sizeof(T)** + 1) { byte[] res = new byte[**sizeof(T)**]; Array.Copy(bytes, res, bytes.Length - 1); return BitConverter.To**Xxx**(res); } else if (bytes.Length < **sizeof(T)**) { byte[] res = new byte[**sizeof(T)**]; Array.Copy(bytes, res, bytes.Length); if (bi.Sign == -1) { for (int i = 0; i < res.Length; i++) { if (res[i] == 0 && i >= bytes.Length) { res[i] = 255; } } } return BitConverter.To**Xxx**(res); } else { throw new ArgumentOutOfRangeException($"bi={bi}", "The bytes length of BigInteger larger than **T**."); } }

转换为无符号整数的函数模版:
public static UT BigIntegerToUXxx(this BigInteger bi) { byte[] bytes = bi.ToByteArray(); if (bytes.Length == sizeof(UT)) { return BitConverter.ToUXxx(bytes); } else if (bytes.Length == sizeof(UT) + 1) { byte[] res = new byte[sizeof(UT)]; Array.Copy(bytes, res, bytes.Length - 1); return BitConverter.ToUXxx(res); } else if (bytes.Length < sizeof(UT)) { byte[] res = new byte[sizeof(UT)]; Array.Copy(bytes, res, bytes.Length); if (bi.Sign == -1) { for (int i = 0; i < res.Length; i++) { if (res[i] == 0 && i >= bytes.Length) { res[i] = 255; } } } return BitConverter.ToUXxx(res); } else { throw new ArgumentOutOfRangeException($"bi={bi}", "The bytes length of BigInteger larger than Int32."); } }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.