How to cast int digit to char without loosing digit value in c#

前端 未结 3 1603
一整个雨季
一整个雨季 2020-12-11 12:12

Mabby it\'s a stupid question, but how can I cast digit of type int to digit of type char?

Standard conversion OPs doesn\'t do this:

int x = 5         


        
相关标签:
3条回答
  • 2020-12-11 12:49

    If you know for sure that the values are in the 0 .. 9 range (both inclusive), you can slightly improve efficiency by making use of a bitwise or, instead of addition:

    (char) ('0' | i);

    this works since '0' has as binary codepoint 0011 0000, and the ten digits thus all are between 0011 0000 and 0011 1001.

    You can thus convert an IEnumerable<int> to an IEnumerable<char> with:

    data.Select(i => (char) ('0' | i));
    0 讨论(0)
  • 2020-12-11 13:00
    var yourInts = new int[]{1,2,3};
    var result = yourInts.Select(x=>x.ToString()[0]).ToArray();
    
    0 讨论(0)
  • 2020-12-11 13:04

    Add 48 to your int value before converting to char:

    char c = (char)(i + 48);
    

    Or for int[] -> char[] conversion:

    var source = new int[] { 1, 2, 3 };
    var results = source.Select(i => (char)(i + 48)).ToArray();
    

    It works, because '0' character in ASCII table has 48 value. But it will work only if your int values is between 0 and 9.

    0 讨论(0)
提交回复
热议问题