将int转换为char?

时间:2016-04-24 11:19:48

标签: char int d

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
    }
}

我试图制作一个简单的符号棋盘。如何正确设置字母和字母或行数/列数? board[i][0] = 0x30 + i;(或0x40)适用于C ++,但不适用于D。

2 个答案:

答案 0 :(得分:6)

您已在std.conv模块中拥有所需内容。 - 最好是使用std.conv.to

import std.conv;
import std.stdio;

void main() {
  int i = 68;
  char a = to!char(i);
  writeln(a);
}

输出:

D

答案 1 :(得分:1)

board[i][0] = cast(char)(0x30 + i);

请记住,在这样转换时,它可能会溢出。