I'm working on a problem (Huffman Tree encoding/decoding) and I need to covert and Int to a char.

I was trying to use

char temp = static_cast <char> (index);  //index is an integer

it compiles but if I try to print, it crashes so obviously that idea doesn't work (or at least I am doing it wrong lol)

I know about the itoa function but we are not allowed to use it as it will not work with cygwin.


6 Years
Discussion Span
Last Post by jonsca
Featured Replies
  • 1
    jonsca 1,059   6 Years Ago

    Make sure it's between 0 and 9 and then add '0' to it. [icode] char a; a= '0' + 1 [/icode] (a = '1') Read More


Make sure it's between 0 and 9 and then add '0' to it. char a; a= '0' + 1 (a = '1')


Maybe I was being vague, I meant I was trying to do something like saving 100 as a char would in turn be translated to 'd'. But it turns out my idea works I was just being goofy and forgot to place a check to keep it from trying to print a non printable character.


ah ok. Yeah, looking back I see what you mean now, the way I read it was you had a single integer digit and were trying to "cast" it to it's character. Apologies then. At least you figured it out.

Edited by jonsca: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.