Search code examples
castingtype-conversionddmd

How do I convert int to int8_t?


How do I convert int to int8_t? cast() / to!int8_t() didn't work.

const nblocks = l /4;
const int8_t i = to!int8_t(nblocks) * 4;

compile-error:

Error: cannot implicitly convert expression (cast(int)to(nblocks) * 4) of type int to const(byte)


Solution

  • int8_t is a typedef in C/C++ to the signed char (8 bits, signed integer). This type corresponds to the byte in D, so to!byte(nblocks) should work.