I`m not absolutely certain about this but think the first 32 ASCII characters are of non-printing type. Your code seems to work ok after 32. Why it prints out the first lot of garbage will have to be answerd by an expert.
This is correct the first 32 characters in ASCII are non printable control codes. The garbage is just the displays way of dealing with these non-printable characters.
Interestingly though your thread title is ambiguous printing the ASCII code of something normally means print the numeric equivalent of a character
e.g. the ASCII code for 'A' is 65 or 0x41 in hex.
You will never be able to print the characters '\r' (code 13 or 0x0D), '\n' (code 10 oe 0x0A) and '\t' (code 9 or 0x09) without altering your display in some way.
Basically it is not possible to write this program as specified.