{"id":7002,"date":"2020-03-12T00:27:53","date_gmt":"2020-03-12T00:27:53","guid":{"rendered":"http:\/\/www.hexacorn.com\/blog\/?p=7002"},"modified":"2020-03-12T00:27:55","modified_gmt":"2020-03-12T00:27:55","slug":"going-bat-mode-crazy","status":"publish","type":"post","link":"https:\/\/www.hexacorn.com\/blog\/2020\/03\/12\/going-bat-mode-crazy\/","title":{"rendered":"Going BAT&#8230;mode crazy"},"content":{"rendered":"\n<p>What will the following bat file print? Foo, or Bar?<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@echo off\n\n mode con cp select=65000 > nul\n set jump=+ACQ-\n mode con cp select=437 > nul\n goto %jump%\n\n:+ACQ-\n echo Foo\n goto :eof\n\n:$\n echo Bar\n goto :eof<\/code><\/pre>\n\n\n\n<p>Here&#8217;s the answer:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2020\/03\/utf7_utf8.png\"><img decoding=\"async\" loading=\"lazy\" width=\"228\" height=\"130\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2020\/03\/utf7_utf8.png\" alt=\"\" class=\"wp-image-7003\"\/><\/a><\/figure><\/div>\n\n\n\n<p>Batch files can be saved as text files using different encodings, including UTF7, and UTF8 as well as MBCS\/DBCS characters sets.<\/p>\n\n\n\n<p>One can therefore enforce encoding and change it not only outside of a batch file, but also on the fly, as is the case in the example above. As a result, the part of the code that executes after first &#8216;mode&#8217; is encoded in UTF7 (&#8216;+ACQ-&#8216; is an encoded &#8216;$&#8217; sign), and the second is OEM-US English.<\/p>\n\n\n\n<p>The below example replaces UTF7 in the above example with Traditional Chinese:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@echo off\n\n mode con cp select=950 > nul\n set jump=\u00a7A\u00a6n\n mode con cp select=65001 > nul\n goto %jump%\n\n:\u00a7A\u00a6n\n echo Foo\n goto :eof\n\n:\u00e4\u00bd\u00a0\u00e5\u00a5\u00bd\n echo Bar\n goto :eof<\/code><\/pre>\n\n\n\n<p>If you look at this code using 950 character set (big5) you will see this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@echo off\n\n mode con cp select=950 > nul\n set jump=\u4f60\u597d\n mode con cp select=65001 > nul\n goto %jump%\n\n:\u4f60\u597d\n echo Foo\n goto :eof\n\n:\u96ff\uee9e\u672b\n echo Bar\n goto :eof<\/code><\/pre>\n\n\n\n<p>and if you choose to preview as UTF8:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@echo off\n\n mode con cp select=950 > nul\n set jump=\u00a7A\u00a6n\n mode con cp select=65001 > nul\n goto %jump%\n\n:\u00a7A\u00a6n\n echo Foo\n goto :eof\n\n:\u4f60\u597d\n echo Bar\n goto :eof<\/code><\/pre>\n\n\n\n<p>Misleading, isn&#8217;t it?<\/p>\n\n\n\n<p>When you run this version of script you will see an error from the interpreter &#8211; this is  a result of it interpreting superfluous UTF8 prefixes that seem to be appearing out of nowhere within the interpreter. Perhaps further study of cmd.exe internals can help to eliminate this quirk. Still, the jump goes to the proper label &amp; errors can be always hidden with standard error redirection:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2020\/03\/big5_utf8.png\"><img decoding=\"async\" loading=\"lazy\" width=\"478\" height=\"155\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2020\/03\/big5_utf8.png\" alt=\"\" class=\"wp-image-7004\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2020\/03\/big5_utf8.png 478w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2020\/03\/big5_utf8-300x97.png 300w\" sizes=\"(max-width: 478px) 100vw, 478px\" \/><\/a><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>What will the following bat file print? Foo, or Bar? Here&#8217;s the answer: Batch files can be saved as text files using different encodings, including UTF7, and UTF8 as well as MBCS\/DBCS characters sets. One can therefore enforce encoding and &hellip; <a href=\"https:\/\/www.hexacorn.com\/blog\/2020\/03\/12\/going-bat-mode-crazy\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[43,13,28,21,58],"tags":[],"_links":{"self":[{"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts\/7002"}],"collection":[{"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/comments?post=7002"}],"version-history":[{"count":1,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts\/7002\/revisions"}],"predecessor-version":[{"id":7005,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts\/7002\/revisions\/7005"}],"wp:attachment":[{"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/media?parent=7002"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/categories?post=7002"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/tags?post=7002"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}