Search code examples
stringdchar-pointer

Convert into const(char)* from float in D


DrawText's first argument needs to be a const(char)* but i tried using to! for that and have failed :(

yVel = to!(string)(player.vel.y);
DrawText(yVel, player.pos.x, player.pos.y - 40, 20, RAYWHITE);

How to properly convert from float to const(char)* ?


Solution

  • to!string converts from float to string. Then toStringz converts from string over to a const char*. So just combine them.

    Or for more control and efficiency, you could define a little stack buffer and sprintf or something as well.

    Generally some_string.ptr will give something you can use as const char*, just make sure you put the 0 terminator at the end before passing it to most C or Windows functions.