How can I stop my Windows console application triggers beeps when displaying binary data
-
05-02-2021 - |
Frage
I wrote a windows console application that receives binary data from a third party provider. For Debug and logging purposes, I display the binary data on the output (the console).
Unfortunately when the character 7 is displayed it triggers a beep. Here is a code that can trigger it:
int main(int argc, char** argv)
{
char c = 7;
std::cout << c;
}
My question is simple, is there a way to disable the beeps ?
Thanks
Lösung
You could unplug the beeper in your computer.
If that is not an option: If you have a method debug(String s) that outputs the string s, you could replace character 7 by something else to avoid the beep.
Another way would be to output the text in hexadecimal form.
Andere Tipps
There is a lot of other values that will trigger weird things (depending on what terminal you use). You should check each char with isprint
before outputting it. Better yet is a function like:
void memdump( std::ostream& o, const void* data, size_t len )
{
const unsigned char* ptr = static_cast<const unsigned char*>(data);
for( size_t i = 0; i < len; i += 16 )
{
o << std::setw(8);
o << std::setfill('0');
o << std::hex << i << ' ';
size_t to = std::min(len,i+16);
for( size_t j = i; j < to; ++j )
{
o << ' ';
o << std::setw(2);
o << std::setfill('0');
o << std::hex;
o << (unsigned)ptr[j];
if( (j+1) % 8 == 0 )
{
o << ' ';
}
}
o << " ";
for( size_t j = to; j < i+16; ++j )
{
o << " ";
if( (j+1) % 8 == 0 )
{
o << ' ';
}
}
for( size_t j = i; j < to; ++j )
{
if(isprint(ptr[j]))
{
o << ptr[j];
}
else
{
o << '.';
}
if( (j+1) % 8 == 0 )
{
o << ' ';
}
}
o << '\n';
}
o << std::dec;
}
A way to disable the beeper (at a Windows command line):
net stop beep
It should disable beeps temporarily. If you want the full effect (re-applies on reboot) type this:
sc config beep start= disabled
char c = 7;
is a BELL according to ASCII
if you want to display the 7 then you need to wrap it in single quotations marks (or how they are called:
char c = '7';
If you don't want to display a 7 then you need to remove the character 0x07 or replace it with (for example with '') (http://www.asciitable.com/)
Select a code page that interprets the character 7 as a glyph instead of a control character. See my other answer for details.