Search code examples
charintd

Converting int to char?


ref void init_board (ref int side, ref char[][] board)  //make empty symbol chessboard
{
    const char black = ' ';
    const char white = 0xB0;

    board[0][0] = ' ';
    for (int i = 1; i <= side; i++)
    {
        board[i][0] = 0x30 + i;  //Setting nums; "Error: Cannot convert int to char"
        board[0][i] = 0x40 + i;  //Setting letters; same here
        for (int j = 1; j <= side; j++)
            board[i][j] = (i+j)%2 == 0 ? black : white; //making black-white board
    }
}

I'm trying to make a simple symbol chessboard. How do I set nums and letters depending or number of row/column correctly? board[i][0] = 0x30 + i; (or 0x40) works in C++, but not in D.


Solution

  • You already have what you need in the std.conv module. - The best is to use the std.conv.to.

    import std.conv;
    import std.stdio;
    
    void main() {
      int i = 68;
      char a = to!char(i);
      writeln(a);
    }
    

    Output:

    D