将int转换为char?

er
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中则无效。

德扬·莱基奇(DejanLekic)

您已经在std.conv模块中有了所需的东西-最好是使用std.conv.to

import std.conv;
import std.stdio;

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

输出:

D

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章