GithubHelp home page GithubHelp logo

number-of-any-base-csharp's Introduction

Number Of Any Base

This is a struct that can represent an integer value of any base (i.e. Binary, Octal, Decimal, Hexadecimal).

Known Limitations

  1. It only supports up to Base-62 out of the box. This support is limited by the OptionAllNumberValues string property’s length. (Adding more possible values increases the number of supported bases, but 62 seemed like plenty for most use-cases.)
  2. Does not support any decimal places. It’s essentially an integer for other bases.
  3. Does not support math results that are less than Int32.MinInt, or greater than Int32.MaxInt, since mathematical operators (+,-,*,/,%) use Int32 methods. Operations that exceed these limits will throw an OverflowException.

Declaration

  • Number n = "10|3";
  • var n = new Number("10|3");
  • Number n = 3; (uses OptionDefaultBaseForStringParsing)
  • Number n = "3"; (uses OptionDefaultBaseForStringParsing)
  • var n = new Number(3); (uses OptionDefaultBaseForStringParsing)
  • var n = new Number("3"); (uses OptionDefaultBaseForStringParsing)

Configuration

  • OptionAllNumberValues : string
    • Default: A string of integers, uppercase, and lowercase letters.
  • OptionDelimiter : char
    • Default: |
  • OptionDefaultBaseForStringParsing : int
    • Default: 10
  • OptionUseDecimalBaseForIntLiterals : bool
    • Default: true

Usage Examples

Math

Number a = "2|1";
Number b = "2|1";
Console.WriteLine(a + b); // Output (in Base-2): 10
Number a = "2|100";
Number b = "2|1";
Console.WriteLine(a - b); // Output (in Base-2): 11
Number a = "2|11";
Number b = "2|11";
Console.WriteLine(a * b); // Output (in Base-2): 1001
Number a = "2|110";
Number b = "2|11";
Console.WriteLine(a / b); // Output (in Base-2): 10
Number a = "2|111";
Number b = "2|110";
Console.WriteLine(a % b); // Output (in Base-2): 1

Literal Assignment

Binary

Number.OptionDefaultBaseForStringParsing = 2;
Number.OptionUseDecimalBaseForIntLiterals = false;
Number n = 01101101;
Console.WriteLine(n.BaseValue); // Output (the Number's base): 2

Octal

Number.OptionDefaultBaseForStringParsing = 8;
Number.OptionUseDecimalBaseForIntLiterals = false;
Number n = 01234567;
Console.WriteLine(n.BaseValue); // Output (the Number's base): 8

Hexadecimal

Number.OptionDefaultBaseForStringParsing = 16;
Number n = "0123456789ABCDE";
Console.WriteLine(n.BaseValue); // Output (the Number's base): 16

number-of-any-base-csharp's People

Contributors

zacharysnewman avatar

Watchers

 avatar

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.