5

ASCII character code 0x07 is a beep character.

Opening CMD and clicking ALT + 007 yields :

enter image description here

And when I click ENTER - I hear a beep. This is fine.

I've searched how to add a beep in the end of a batch file, and I found that this is the solution : (I'm pasting it as an image becuase SO doesn't show the round bullet after editing) :

enter image description here

This does work and does make a sound. When examined with a HEX viewer (with beyond compare) on ECHO, the round bullet is:

enter image description here

But if I manually add ALT+7 to the document , I see this:

enter image description here

Which is a 95 as a hex - and it's not a Beep. In addition, I went to the working batch file and added a new line with my ALT+7 :

enter image description here

But looking via HEX viewer :

enter image description here

Question:

I'm a bit confused. Clicking Alt+65 does yields A everywhere.

So why does the beep different and doesn't work when saved in Windows GUI?

In the console, if I click ALT+007 I get ^G (it does beep), but when I click ALT+7 I get the circle, which is not a beep:

Here are both:

enter image description here

Another interesting observation via notepad++ :

enter image description here

I think it's related to encoding, etc, but I don't understand the inconsistency.

Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
Royi Namir
  • 144,742
  • 138
  • 468
  • 792

3 Answers3

1

This is not really an answer, but it would be too large for a comment.

The "manually adding ALT+7 to the document" part produced strange results which it should not have produced. The result should have been a single 0x07 character. It may be that your editor did something funny when you pressed ALT+7.

The problem with this kind of troubleshooting is that the tools you are working with have complex behavior that distorts the experiment. The tools also have strange modes and strange states. For example, what is the encoding of your console subsystem?

I tried this: copy con x.bat typed echo followed by Alt+7 and then Ctrl+Z and got a beeping batch file. The character looked like a bullet.
Then I tried with Alt+007 and I also got a beeping batch file, but the character was ^G now.

At the C:\> prompt, I typed echo followed by Alt+7, a bullet was also produced, but there was no beep. But an Alt+007, looking like a ^G did produce a beep. Go figure.

I would say ignore the inconsistencies that occur when you are trying to input a control character, because there is a lot of software involved in doing so which is beyond your control and apparently works in mysterious ways. (I know, not an answer.)

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • In Windows Alt+7 is the OEM character for your system locale, such as codepage 437 (U.S. English) or 856 (Hebrew). When typed in a window such as notepad, it's sent to the app as the Unicode character, such as U+2022 (bullet). Then if the app saves it as the system ANSI codepage, it gets encoded as the byte 0x95 in codepage 1252 (U.S. English) or 1255 (Hebrew). Then when read from a batch file, CMD decodes it using the OEM codepage (unless changed via chcp.com), which is "ò" in codepage 437 and "ץ" in codepage 856. – Eryk Sun Jul 30 '17 at 20:38
  • I forgot to add that if you precede the number with a 0 on the keypad, Windows uses the ANSI codepage instead of the OEM codepage. In this case, entering Alt+007 gives an ASCII BEL character. When saved as an ANSI file, it's simply the byte 0x07. It's important to note that when decoding this as the default OEM codepage (e.g. 437), CMD uses `MultiByteToWideChar`, which maps the control characters 0-31 and 127 to the *ASCII* control codes and *not* the classic MS-DOS OEM symbols that you get when entering Alt+7, and so on. – Eryk Sun Jul 30 '17 at 22:31
  • @eryksun you seem to know your stuff. I think you should post an answer. – Mike Nakis Jul 30 '17 at 22:35
  • @eryksun Can you please add an answer ? – Royi Namir Aug 25 '17 at 16:35
1

I have a workaround to suggest. Put this in your script:

forfiles /p "%~dp0" /m "%~nx0" /c "cmd /c echo 0x07"

For every file in your script's directory matching your script's filename (e.g. 1 time), it will echo ASCII character 7, and will make noise. From the forfiles /? documentation:

To include special characters in the command line, use the hexadecimal code for the character in 0xHH format (ex. 0x09 for tab). Internal CMD.exe commands should be preceded with "cmd /c".

forfiles is a handy utility to abuse whenever you need a non-printable or extended character.


As for my speculation for why Alt+007 doesn't behave as expected, I believe the console works on a different codepage from windowed applications (console = 437, windowed = 1252 for en-US, IIRC). I've struggled with this issue as well for reasons, ultimately resorting to hard coding the symbols used for console characters 1 through 31 in that JavaScript project.

rojo
  • 24,000
  • 5
  • 55
  • 101
  • The console initially uses the OEM codepage, for both input and output, and CMD decodes a batch script line by line using the current output codepage. You can change to the ANSI codepage in the batch script itself, if you saved the script as ANSI instead of OEM, via chcp.com (e.g. `chcp 1252`). However, this won't use the classic DOS glyphs for codes 1-31. CMD decodes via `MultiByteToWideChar`, which uses the extended ASCII version of the OEM codepage and thus ASCII control codes (e.g. a ^G BEL character instead of a bullet glyph). – Eryk Sun Jul 30 '17 at 22:49
0

To beep in a cmd file :

Solution 1

C:\>rundll32 user32.dll,MessageBeep -1

Solution 2

C:\>echo ^G>beep.txt
C:\>type beep.txt

NB : ^G is obtained by pressing Ctrl+G and you can see the character by editing beep.txt

efdummy
  • 684
  • 1
  • 8
  • 9