Discussion:
console app & printf of 8bit ASCII characters
(too old to reply)
Peter
2007-07-02 16:24:29 UTC
Permalink
Hi,

I created a small console app to output some text based on command line
parameters.
Output is multi-lingual, based on input, but only latin text, single byte
characters, no unicode or MBCS.

I noticed that 8bit ASCII characters are not displayed properly, they are
replaced by other characters.
E.g. "é" becomes "8"

I thought for a minute that the most significant bit was cleared but then it
would have been another character than "8" so I'm not sure what the problem
is ?

Is it possible to output such characters in a console app on a Windows
system with latin code page ?
Should I use another command than printf to work around the issue ?
Ed Mulroy [TeamB]
2007-07-02 19:26:19 UTC
Permalink
Try cprintf and see if it gives you want you want. You will need to include
conio.h to get a function prototype for cprintf. If my memory is correct
cprintf will require \r\n instead of \n at the end of a line.

. Ed
Post by Peter
Hi,
I created a small console app to output some text based on command line
parameters.
Output is multi-lingual, based on input, but only latin text, single byte
characters, no unicode or MBCS.
I noticed that 8bit ASCII characters are not displayed properly, they are
replaced by other characters.
E.g. "é" becomes "8"
I thought for a minute that the most significant bit was cleared but then
it would have been another character than "8" so I'm not sure what the
problem is ?
Is it possible to output such characters in a console app on a Windows
system with latin code page ?
Should I use another command than printf to work around the issue ?
Peter
2007-07-02 22:47:43 UTC
Permalink
Post by Ed Mulroy [TeamB]
Try cprintf and see if it gives you want you want. You will need to
include conio.h to get a function prototype for cprintf. If my memory is
correct cprintf will require \r\n instead of \n at the end of a line.
Thanks but no, ... that doesn't work.
In fact the output is strangely scattered over the screen ?
And the French / German characters (accents and points on characters, yet in
the ASCII table) are still not displayed properly (same as printf)
Ed Mulroy [TeamB]
2007-07-03 00:40:03 UTC
Permalink
Ok, I just tried it with them and confirmed your results. To do what you
want you would probably have to use Windows API calls. Start the Windows
help, win32.hlp, and search for the page Console Functions.

. Ed
Post by Peter
Post by Ed Mulroy [TeamB]
Try cprintf and see if it gives you want you want. You will need to
include conio.h to get a function prototype for cprintf. If my memory is
correct cprintf will require \r\n instead of \n at the end of a line.
Thanks but no, ... that doesn't work.
In fact the output is strangely scattered over the screen ?
And the French / German characters (accents and points on characters, yet
in the ASCII table) are still not displayed properly (same as printf)
Peter
2007-07-03 11:43:50 UTC
Permalink
Post by Ed Mulroy [TeamB]
Ok, I just tried it with them and confirmed your results. To do what you
want you would probably have to use Windows API calls. Start the Windows
help, win32.hlp, and search for the page Console Functions.
You mean the " WriteConsole " function ?

Where do I find the handle to a console screen buffer ?
Rudy Velthuis [TeamB]
2007-07-03 11:58:26 UTC
Permalink
Post by Peter
Post by Ed Mulroy [TeamB]
Ok, I just tried it with them and confirmed your results. To do
what you want you would probably have to use Windows API calls.
Start the Windows help, win32.hlp, and search for the page Console
Functions.
You mean the " WriteConsole " function ?
Where do I find the handle to a console screen buffer ?
Have a look at the "Console functions" topic in the Win32 SDK help that
comes with your C++Builder. Have a look at GetStdHandle too. And look
at the many other functions, like:

GetConsoleScreenBufferInfo
GetConsoleScreenBufferInfoEx
GetConsoleSelectionInfo
SetConsoleCursorPosition
SetConsoleDisplayMode
SetConsoleOutputCP
SetConsoleScreenBufferInfoEx
SetConsoleScreenBufferSize
SetConsoleTextAttribute
SetConsoleTitle Sets the title for the current console window.
SetConsoleWindowInfo
SetCurrentConsoleFontEx
SetStdHandle
WriteConsole
WriteConsoleInput
WriteConsoleOutput
WriteConsoleOutputAttribute
WriteConsoleOutputCharacter

... and many many more.
--
Rudy Velthuis [TeamB] http://rvelthuis.de

"Why do grandparents and grandchildren get along so well? They
have the same enemy -- the mother." -- Claudette Colbert.
Peter
2007-07-03 15:24:30 UTC
Permalink
Post by Rudy Velthuis [TeamB]
Have a look at the "Console functions" topic in the Win32 SDK help that
comes with your C++Builder. Have a look at GetStdHandle too. And look
I created this small function to test:

//--------------------------------------------------------------------------
void myprintf (const char *Text)
{
HANDLE OutPutHandle = GetStdHandle( STD_OUTPUT_HANDLE ) ;

DWORD NumberOfCharsWritten ;

WriteConsole( OutPutHandle, // handle to a console screen
buffer
(void*)Text, // pointer to buffer to write
from
strlen(Text), // number of characters to
write
&NumberOfCharsWritten, // pointer to number of
characters written
NULL // reserved
);
}
//--------------------------------------------------------------------------

The result is still exactly the same. The special characters are wrongly
displayed.
Exactly the same as printf.

Suggestions ?
Peter
2007-07-03 16:12:05 UTC
Permalink
Got it to work by using the WriteConsoleW() variant !!
I convert the characters to Unicode first, then write them with WriteConsole
!
That does the trick
Peter
2007-07-03 16:30:33 UTC
Permalink
Post by Peter
Got it to work by using the WriteConsoleW() variant !!
I convert the characters to Unicode first, then write them with
WriteConsoleW That does the trick
Interesting to see is that the screenoutput is not saved into a file, when
created with this myprintf function.
So test.exe /1 /2/ /3 > test.txt
saves only the screenoutput created with printf, not the one created with
myprintf.
But no biggy, only FYI.
Peter
2007-07-03 16:41:00 UTC
Permalink
Post by Peter
Interesting to see is that the screenoutput is not saved into a file, when
created with this myprintf function.
So test.exe /1 /2/ /3 > test.txt
saves only the screenoutput created with printf, not the one created with
myprintf.
But no biggy, only FYI.
BUT OF COURSE ...
then I can just as easy use wprintf()
No need for the Console functions then and the output is also saved to a
file (> test.text).
PS. the e with accent and other characters are displayed correctly now.
Rudy Velthuis [TeamB]
2007-07-03 17:30:04 UTC
Permalink
Post by Peter
The result is still exactly the same. The special characters are
wrongly displayed. Exactly the same as printf.
Set a different code page and try again. But AFAIK, even the C
environment allows you to set the locale, using setlocale(), e.g.

setlocale(LC_ALL, "de_DE");

Also note that you usually have Ansi encoded strings, which must be
converted to Oem, using AnsiToOem().

setlocale(LC_ALL, "de_DE");
printf("Locale \"de_DE\":\n");

CharToOem("Konigstra?e", s);
printf("s: %s\n", s);
--
Rudy Velthuis [TeamB] http://rvelthuis.de

"God is love, but get it in writing." -- Gypsy Rose Lee.
Loading...