为什么批处理文件中的特殊字符打印为问号?

Why do special characters in a batch file print as a question mark?

所以我刚才在做一个批处理文件游戏,由于某种原因它打印了一个问号符号而不是笑脸符号(当你在命令提示符下按 Ctrl+A 时得到的)。

My Program ScreenShot

我对这些其他符号也有同样的问题:

ctrl+a Smiley face
ctrl+b Dark smiley face
ctrl+d Diamond
ctrl+e Clover
ctrl+f Ace
ctrl+n Music symbol
crtl+o Some random circlish symbol
ctrl+p Left arrow
ctrl+q Right arrow
ctrl+r Up and Down arrows
ctrl+t Line wrap symbol
ctrl+u Double s
ctrl+v Thick underscore
ctrl+w Up and down arrow with a line at the bottom
ctrl+x Up arrow
ctrl+y Down arrow

来自 Instructables 文章:如何在 cmd 中使用特殊字符

I tried it with cmd ScreenShot

我目前是 运行 Windows 10 Pro,最新更新。我试图找到解决此问题的解决方案,但没有成功。任何帮助将不胜感激。

好吧,这取决于您的代码页。您可以通过进入命令提示符并键入 chcp 来检查它,这会很好地显示您的代码页。搜索它,你会找到一个你可以写的字符列表。基本上,它会告诉您可以输入哪些 "special" 个字符。例如,我的是 850(和很多其他的一样),所以不能显示笑脸。您可以通过以下方式更改您的代码页:

Open Windows Control Panel
Select Region and Language
Click on the Administrative tab
Under Language for non-Unicode programs, click on Change System Locale
Choose the locale
Click OK

我想就这些了。希望我有所帮助!

OEM Alt codes 的行为对代码 1-31 和 127 具有误导性。系统的 OEM 代码页是 ASCII 的超集。但是,这一系列的 Alt 代码不输入 ASCII 控制字符,而是输入旧的 IBM PC 系统有时会显示和打印的 ASCII 控制字符。例如,Alt+1 输入“☺”(U+263A,白色笑脸)而不是 ASCII SOH(U+0001,标题开始)。更复杂的是,在将此类字符编码为 OEM 代码页时,WideCharToMultiByte 的默认行为是使用相应的 ASCII 控制字符作为 best-fit 编码。因此,文本编辑器可能不会警告您所见即所得。

控制台不会像旧的 IBM PC 图形字符那样显示控制字符。它解释了其中的一些,例如 U+0009(水平制表符——将光标​​前进 8 个空格)和 U+000D(回车 return——将光标​​前进到下一行的开头),但大多数显示为未映射字符的当前字体字形,例如带框的问号或空框。如果你想将“☺”打印到控制台,那么你应该使用控制台的 wide-character API,它与 Windows 本机 Unicode (UTF-16LE) 字符串一起使用。

好在CMD是Unicode应用程序,所以在控制台打印“☺”是没有问题的。问题是批处理脚本使用什么编码。唯一的通用解决方案是将批处理文件保存为不带 BOM(字节顺序标记)的 UTF-8。您将需要一个 IDE 或文本编辑器,它可以在没有 BOM 的情况下保存 UTF-8,例如 Notepad++。

CMD 使用旧控制台代码页逐行解码批处理脚本,您可以使用命令 chcp.com 65001 将其更改为 UTF-8。我建议不要永久更改为 UTF-8,因为根据 Windows 版本,控制台有代码页 65001 的各种错误。您可以暂时切换到 UTF-8 以加载 non-ASCII 字符串作为环境变量。否则,将批处理文件的其余部分严格保留为 7 位 ASCII,以避免出现本地化问题。

这是一个例子。可能有比创建所有这些局部环境变量更通用的方法来解决这个问题,但这只是为了表明它在原则上是有效的。

graphics_ctl.bat

@echo off
REM ASCII Control-Character Graphics
REM File encoding: UTF-8 (no BOM)

REM Set the console to codepage 65001 (UTF-8).
for /f "tokens=2 delims=:" %%a in ('chcp.com') do set "CONSOLE_CODEPAGE=%%a"
set "CONSOLE_CODEPAGE=%CONSOLE_CODEPAGE: =%"
chcp.com 65001 >NUL

set "GCTL_WHITE_SMILING_FACE=☺"
set "GCTL_BLACK_SMILING_FACE=☻"
set "GCTL_BLACK_HEART_SUIT=♥"
set "GCTL_BLACK_DIAMOND_SUIT=♦"
set "GCTL_BLACK_CLUB_SUIT=♣"
set "GCTL_BLACK_SPADE_SUIT=♠"
set "GCTL_BULLET=•"
set "GCTL_INVERSE_BULLET=◘"
set "GCTL_WHITE_CIRCLE=○"
set "GCTL_INVERSE_WHITE_CIRCLE=◙"
set "GCTL_MALE_SIGN=♂"
set "GCTL_FEMALE_SIGN=♀"
set "GCTL_EIGHTH_NOTE=♪"
set "GCTL_BEAMED_EIGHTH_NOTES=♫"
set "GCTL_WHITE_SUN_WITH_RAYS=☼"
set "GCTL_BLACK_RIGHT_POINTER=►"
set "GCTL_BLACK_LEFT_POINTER=◄"
set "GCTL_UP_DOWN_ARROW=↕"
set "GCTL_DOUBLE_EXCLAMATION_MARK=‼"
set "GCTL_PILCROW_SIGN=¶"
set "GCTL_SECTION_SIGN=§"
set "GCTL_BLACK_RECTANGLE=▬"
set "GCTL_UP_DOWN_ARROW_WITH_BASE=↨"
set "GCTL_UP_ARROW=↑"
set "GCTL_DOWN_ARROW=↓"
set "GCTL_RIGHT_ARROW=→"
set "GCTL_LEFT_ARROW=←"
set "GCTL_RIGHT_ANGLE=∟"
set "GCTL_LEFT_RIGHT_ARROW=↔"
set "GCTL_BLACK_UP_TRIANGLE=▲"
set "GCTL_BLACK_DOWN_TRIANGLE=▼"
set "GCTL_HOUSE=⌂"

REM Restore the previous console codepage.
chcp.com %CONSOLE_CODEPAGE% >NUL

graphics_box.bat

@echo off
REM Box-Drawing Graphics
REM File encoding: UTF-8 (no BOM)

REM Set the console to codepage 65001 (UTF-8).
for /f "tokens=2 delims=:" %%a in ('chcp.com') do set "CONSOLE_CODEPAGE=%%a"
set "CONSOLE_CODEPAGE=%CONSOLE_CODEPAGE: =%"
chcp.com 65001 >NUL

set "GBOX_LIGHT_SHADE=░"
set "GBOX_MEDIUM_SHADE=▒"
set "GBOX_DARK_SHADE=▓"
set "GBOX_LIGHT_VERTICAL=│"
set "GBOX_LIGHT_VERTICAL_AND_LEFT=┤"
set "GBOX_VERTICAL_SINGLE_AND_LEFT_DOUBLE=╡"
set "GBOX_VERTICAL_DOUBLE_AND_LEFT_SINGLE=╢"
set "GBOX_DOWN_DOUBLE_AND_LEFT_SINGLE=╖"
set "GBOX_DOWN_SINGLE_AND_LEFT_DOUBLE=╕"
set "GBOX_DOUBLE_VERTICAL_AND_LEFT=╣"
set "GBOX_DOUBLE_VERTICAL=║"
set "GBOX_DOUBLE_DOWN_AND_LEFT=╗"
set "GBOX_DOUBLE_UP_AND_LEFT=╝"
set "GBOX_UP_DOUBLE_AND_LEFT_SINGLE=╜"
set "GBOX_UP_SINGLE_AND_LEFT_DOUBLE=╛"
set "GBOX_LIGHT_DOWN_AND_LEFT=┐"
set "GBOX_LIGHT_UP_AND_RIGHT=└"
set "GBOX_LIGHT_UP_AND_HORIZONTAL=┴"
set "GBOX_LIGHT_DOWN_AND_HORIZONTAL=┬"
set "GBOX_LIGHT_VERTICAL_AND_RIGHT=├"
set "GBOX_LIGHT_HORIZONTAL=─"
set "GBOX_LIGHT_VERTICAL_AND_HORIZONTAL=┼"
set "GBOX_VERTICAL_SINGLE_AND_RIGHT_DOUBLE=╞"
set "GBOX_VERTICAL_DOUBLE_AND_RIGHT_SINGLE=╟"
set "GBOX_DOUBLE_UP_AND_RIGHT=╚"
set "GBOX_DOUBLE_DOWN_AND_RIGHT=╔"
set "GBOX_DOUBLE_UP_AND_HORIZONTAL=╩"
set "GBOX_DOUBLE_DOWN_AND_HORIZONTAL=╦"
set "GBOX_DOUBLE_VERTICAL_AND_RIGHT=╠"
set "GBOX_DOUBLE_HORIZONTAL=═"
set "GBOX_DOUBLE_VERTICAL_AND_HORIZONTAL=╬"
set "GBOX_UP_SINGLE_AND_HORIZONTAL_DOUBLE=╧"
set "GBOX_UP_DOUBLE_AND_HORIZONTAL_SINGLE=╨"
set "GBOX_DOWN_SINGLE_AND_HORIZONTAL_DOUBLE=╤"
set "GBOX_DOWN_DOUBLE_AND_HORIZONTAL_SINGLE=╥"
set "GBOX_UP_DOUBLE_AND_RIGHT_SINGLE=╙"
set "GBOX_UP_SINGLE_AND_RIGHT_DOUBLE=╘"
set "GBOX_DOWN_SINGLE_AND_RIGHT_DOUBLE=╒"
set "GBOX_DOWN_DOUBLE_AND_RIGHT_SINGLE=╓"
set "GBOX_VERTICAL_DOUBLE_AND_HORIZONTAL_SINGLE=╫"
set "GBOX_VERTICAL_SINGLE_AND_HORIZONTAL_DOUBLE=╪"
set "GBOX_LIGHT_UP_AND_LEFT=┘"
set "GBOX_LIGHT_DOWN_AND_RIGHT=┌"
set "GBOX_FULL_BLOCK=█"
set "GBOX_LOWER_HALF_BLOCK=▄"
set "GBOX_LEFT_HALF_BLOCK=▌"
set "GBOX_RIGHT_HALF_BLOCK=▐"
set "GBOX_UPPER_HALF_BLOCK=▀"

REM Restore the previous console codepage.
chcp.com %CONSOLE_CODEPAGE% >NUL

graphics_test.bat

@echo off
setlocal

set "scriptdir=%~dp0"
call "%scriptdir%\graphics_ctl.bat"
call "%scriptdir%\graphics_box.bat"

echo.
echo ASCII Control-Character Graphics
echo ----------------------------------------------
echo GCTL_WHITE_SMILING_FACE      = %GCTL_WHITE_SMILING_FACE%
echo GCTL_BLACK_SMILING_FACE      = %GCTL_BLACK_SMILING_FACE%
echo GCTL_BLACK_HEART_SUIT        = %GCTL_BLACK_HEART_SUIT%
echo GCTL_BLACK_DIAMOND_SUIT      = %GCTL_BLACK_DIAMOND_SUIT%
echo GCTL_BLACK_CLUB_SUIT         = %GCTL_BLACK_CLUB_SUIT%
echo GCTL_BLACK_SPADE_SUIT        = %GCTL_BLACK_SPADE_SUIT%
echo GCTL_BULLET                  = %GCTL_BULLET%
echo GCTL_INVERSE_BULLET          = %GCTL_INVERSE_BULLET%
echo GCTL_WHITE_CIRCLE            = %GCTL_WHITE_CIRCLE%
echo GCTL_INVERSE_WHITE_CIRCLE    = %GCTL_INVERSE_WHITE_CIRCLE%
echo GCTL_MALE_SIGN               = %GCTL_MALE_SIGN%
echo GCTL_FEMALE_SIGN             = %GCTL_FEMALE_SIGN%
echo GCTL_EIGHTH_NOTE             = %GCTL_EIGHTH_NOTE%
echo GCTL_BEAMED_EIGHTH_NOTES     = %GCTL_BEAMED_EIGHTH_NOTES%
echo GCTL_WHITE_SUN_WITH_RAYS     = %GCTL_WHITE_SUN_WITH_RAYS%
echo GCTL_BLACK_RIGHT_POINTER     = %GCTL_BLACK_RIGHT_POINTER%
echo GCTL_BLACK_LEFT_POINTER      = %GCTL_BLACK_LEFT_POINTER%
echo GCTL_UP_DOWN_ARROW           = %GCTL_UP_DOWN_ARROW%
echo GCTL_DOUBLE_EXCLAMATION_MARK = %GCTL_DOUBLE_EXCLAMATION_MARK%
echo GCTL_PILCROW_SIGN            = %GCTL_PILCROW_SIGN%
echo GCTL_SECTION_SIGN            = %GCTL_SECTION_SIGN%
echo GCTL_BLACK_RECTANGLE         = %GCTL_BLACK_RECTANGLE%
echo GCTL_UP_DOWN_ARROW_WITH_BASE = %GCTL_UP_DOWN_ARROW_WITH_BASE%
echo GCTL_UP_ARROW                = %GCTL_UP_ARROW%
echo GCTL_DOWN_ARROW              = %GCTL_DOWN_ARROW%
echo GCTL_RIGHT_ARROW             = %GCTL_RIGHT_ARROW%
echo GCTL_LEFT_ARROW              = %GCTL_LEFT_ARROW%
echo GCTL_RIGHT_ANGLE             = %GCTL_RIGHT_ANGLE%
echo GCTL_LEFT_RIGHT_ARROW        = %GCTL_LEFT_RIGHT_ARROW%
echo GCTL_BLACK_UP_TRIANGLE       = %GCTL_BLACK_UP_TRIANGLE%
echo GCTL_BLACK_DOWN_TRIANGLE     = %GCTL_BLACK_DOWN_TRIANGLE%
echo GCTL_HOUSE                   = %GCTL_HOUSE%

echo.
echo Box-Drawing Graphics
echo ----------------------------------------------
echo GBOX_LIGHT_SHADE                           = %GBOX_LIGHT_SHADE%
echo GBOX_MEDIUM_SHADE                          = %GBOX_MEDIUM_SHADE%
echo GBOX_DARK_SHADE                            = %GBOX_DARK_SHADE%
echo GBOX_LIGHT_VERTICAL                        = %GBOX_LIGHT_VERTICAL%
echo GBOX_LIGHT_VERTICAL_AND_LEFT               = %GBOX_LIGHT_VERTICAL_AND_LEFT%
echo GBOX_VERTICAL_SINGLE_AND_LEFT_DOUBLE       = %GBOX_VERTICAL_SINGLE_AND_LEFT_DOUBLE%
echo GBOX_VERTICAL_DOUBLE_AND_LEFT_SINGLE       = %GBOX_VERTICAL_DOUBLE_AND_LEFT_SINGLE%
echo GBOX_DOWN_DOUBLE_AND_LEFT_SINGLE           = %GBOX_DOWN_DOUBLE_AND_LEFT_SINGLE%
echo GBOX_DOWN_SINGLE_AND_LEFT_DOUBLE           = %GBOX_DOWN_SINGLE_AND_LEFT_DOUBLE%
echo GBOX_DOUBLE_VERTICAL_AND_LEFT              = %GBOX_DOUBLE_VERTICAL_AND_LEFT%
echo GBOX_DOUBLE_VERTICAL                       = %GBOX_DOUBLE_VERTICAL%
echo GBOX_DOUBLE_DOWN_AND_LEFT                  = %GBOX_DOUBLE_DOWN_AND_LEFT%
echo GBOX_DOUBLE_UP_AND_LEFT                    = %GBOX_DOUBLE_UP_AND_LEFT%
echo GBOX_UP_DOUBLE_AND_LEFT_SINGLE             = %GBOX_UP_DOUBLE_AND_LEFT_SINGLE%
echo GBOX_UP_SINGLE_AND_LEFT_DOUBLE             = %GBOX_UP_SINGLE_AND_LEFT_DOUBLE%
echo GBOX_LIGHT_DOWN_AND_LEFT                   = %GBOX_LIGHT_DOWN_AND_LEFT%
echo GBOX_LIGHT_UP_AND_RIGHT                    = %GBOX_LIGHT_UP_AND_RIGHT%
echo GBOX_LIGHT_UP_AND_HORIZONTAL               = %GBOX_LIGHT_UP_AND_HORIZONTAL%
echo GBOX_LIGHT_DOWN_AND_HORIZONTAL             = %GBOX_LIGHT_DOWN_AND_HORIZONTAL%
echo GBOX_LIGHT_VERTICAL_AND_RIGHT              = %GBOX_LIGHT_VERTICAL_AND_RIGHT%
echo GBOX_LIGHT_HORIZONTAL                      = %GBOX_LIGHT_HORIZONTAL%
echo GBOX_LIGHT_VERTICAL_AND_HORIZONTAL         = %GBOX_LIGHT_VERTICAL_AND_HORIZONTAL%
echo GBOX_VERTICAL_SINGLE_AND_RIGHT_DOUBLE      = %GBOX_VERTICAL_SINGLE_AND_RIGHT_DOUBLE%
echo GBOX_VERTICAL_DOUBLE_AND_RIGHT_SINGLE      = %GBOX_VERTICAL_DOUBLE_AND_RIGHT_SINGLE%
echo GBOX_DOUBLE_UP_AND_RIGHT                   = %GBOX_DOUBLE_UP_AND_RIGHT%
echo GBOX_DOUBLE_DOWN_AND_RIGHT                 = %GBOX_DOUBLE_DOWN_AND_RIGHT%
echo GBOX_DOUBLE_UP_AND_HORIZONTAL              = %GBOX_DOUBLE_UP_AND_HORIZONTAL%
echo GBOX_DOUBLE_DOWN_AND_HORIZONTAL            = %GBOX_DOUBLE_DOWN_AND_HORIZONTAL%
echo GBOX_DOUBLE_VERTICAL_AND_RIGHT             = %GBOX_DOUBLE_VERTICAL_AND_RIGHT%
echo GBOX_DOUBLE_HORIZONTAL                     = %GBOX_DOUBLE_HORIZONTAL%
echo GBOX_DOUBLE_VERTICAL_AND_HORIZONTAL        = %GBOX_DOUBLE_VERTICAL_AND_HORIZONTAL%
echo GBOX_UP_SINGLE_AND_HORIZONTAL_DOUBLE       = %GBOX_UP_SINGLE_AND_HORIZONTAL_DOUBLE%
echo GBOX_UP_DOUBLE_AND_HORIZONTAL_SINGLE       = %GBOX_UP_DOUBLE_AND_HORIZONTAL_SINGLE%
echo GBOX_DOWN_SINGLE_AND_HORIZONTAL_DOUBLE     = %GBOX_DOWN_SINGLE_AND_HORIZONTAL_DOUBLE%
echo GBOX_DOWN_DOUBLE_AND_HORIZONTAL_SINGLE     = %GBOX_DOWN_DOUBLE_AND_HORIZONTAL_SINGLE%
echo GBOX_UP_DOUBLE_AND_RIGHT_SINGLE            = %GBOX_UP_DOUBLE_AND_RIGHT_SINGLE%
echo GBOX_UP_SINGLE_AND_RIGHT_DOUBLE            = %GBOX_UP_SINGLE_AND_RIGHT_DOUBLE%
echo GBOX_DOWN_SINGLE_AND_RIGHT_DOUBLE          = %GBOX_DOWN_SINGLE_AND_RIGHT_DOUBLE%
echo GBOX_DOWN_DOUBLE_AND_RIGHT_SINGLE          = %GBOX_DOWN_DOUBLE_AND_RIGHT_SINGLE%
echo GBOX_VERTICAL_DOUBLE_AND_HORIZONTAL_SINGLE = %GBOX_VERTICAL_DOUBLE_AND_HORIZONTAL_SINGLE%
echo GBOX_VERTICAL_SINGLE_AND_HORIZONTAL_DOUBLE = %GBOX_VERTICAL_SINGLE_AND_HORIZONTAL_DOUBLE%
echo GBOX_LIGHT_UP_AND_LEFT                     = %GBOX_LIGHT_UP_AND_LEFT%
echo GBOX_LIGHT_DOWN_AND_RIGHT                  = %GBOX_LIGHT_DOWN_AND_RIGHT%
echo GBOX_FULL_BLOCK                            = %GBOX_FULL_BLOCK%
echo GBOX_LOWER_HALF_BLOCK                      = %GBOX_LOWER_HALF_BLOCK%
echo GBOX_LEFT_HALF_BLOCK                       = %GBOX_LEFT_HALF_BLOCK%
echo GBOX_RIGHT_HALF_BLOCK                      = %GBOX_RIGHT_HALF_BLOCK%
echo GBOX_UPPER_HALF_BLOCK                      = %GBOX_UPPER_HALF_BLOCK%