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

时间:2017-08-14 23:24:53

标签: windows batch-file character-encoding windows-console

所以我前段时间正在处理批处理文件游戏,并且由于某种原因它会打印一个问号符号而不是笑脸符号(在命令提示符下按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。我试图找到解决方案来解决这个问题,但没有成功。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

OEM Alt codes的行为会对代码1-31和127产生误导。系统的OEM代码页是ASCII的超集。但是,此范围的Alt代码不会输入ASCII控制字符,而是输入旧IBM PC系统有时会显示和打印ASCII控制字符的字符。例如,Alt + 1输入“☺”(U + 263A,白色笑脸)而不是ASCII SOH(U + 0001,标题开始)。为了使这一点更加复杂,WideCharToMultiByte将这些字符编码到OEM代码页时的默认行为是使用相应的ASCII控制字符作为最适合编码。因此,文本编辑器可能不会警告您所看到的不是您将获得的内容。

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

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

CMD使用旧版控制台代码页逐行解码批处理脚本,您可以使用命令chcp.com 65001将其更改为UTF-8。我建议不要永久更改为UTF-8,因为根据Windows版本,控制台有各种错误代码页65001.您可以临时切换到UTF-8以加载非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%

答案 1 :(得分:-1)

嗯,这取决于您的代码页。您可以通过在命令提示符下输入chcp来检查它,它会很好地显示您的代码页。搜索它,你会找到一个你可以写的字符列表。基本上,它会告诉你哪个&#34;特别&#34;您可以输入的字符将被显示。例如,地雷是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

我认为这就是全部。希望我帮忙!