Character encoding C and Swedish åöä - c

I have no idea how to solve the character encoding for the swedish letters "å" "ä" "ö".
It works fine in Eclipse where the encoding is set to Cp1252. BUT - when I execute the same program in windows CMD I can not any longer see these characters. I have tried to change the encoding in CMD to 1252 and 65001, that is: chcp 1252 and chcp 65001 ... but NOPE!
Have even tried with a library function in C called setlocale, that is:
setlocale(LC_ALL, "swedish");
MANY THANKS TO SUGGESTIONS THAT SOLVES THIS ISSUE!!!!!!!

Just do
setlocale(LC_ALL, "SE");

Related

En Dash in file path batch job query

Hoping someone can help.
I have a batch job (Windows env) which simply copies a file to another folder.
copy "\\ACP-MS-NAS21\Global\MEC Daily Productivity\Business Analysts\Master_List\HCP_Master_List.xlsx" ^
"\\ACP-MS-NAS21\Global\CSD [?] DWP Medical Services\CSL_CSD_DB\Master_List"
But I get the following error:
The filename, directory name, or volume label syntax is incorrect.
I can see there's an en dash in the file path which I believe is causing the problem.
Is there any way to have a wildcard in the file path or any other way the job can recognise this?
Thanks in advance.
PS. newbie at batch programming, coding etc, so please can explanations be in plain English. Many thanks
The command-line windows (and by extension - BAT files) operate in OEM codepage by default. Which exact codepage is defined by your OS settings (Language for non-Unicode programs or similar). Therefore you cannot normally use anything outside ASCII or whatever codepage you have.
Save script as UTF-8
To use those characters you will need to work in a codepage that actually has them. UTF-8 is the best one for this tasks (and probably the only one that will work in your case).
First, save your script as UTF-8. In notepad you can select codepage from save as menu.
If your editor does not allow you to select UTF-8 (no BOM) leave first line of your BAT file blank as some editors may preface your file a special header called BOM that helps detecting codepage. If it does and you leave 1st line blank you will get a Bad command or file name error as soon as your script starts but this won't prevent it from running properly.
Select UTF-8 codepage
Now, your script is in UTF-8 but windows command processor will still execute it as if it was in ASCII, thus corrupting all special characters. To specify our encoding we need to add following command, preferentially as a first non-blank line of your script, including comments (those may have non-ASCII characters in them - with unpredictable results)
CHCP 65001
CHCP changes current codepage to 65001 which is internal codepage number for UTF-8.
This works because latin letters and numbers in UTF-8 have the same encoding as in ASCII and OEM codepages. Thus your scripts starts executing in OEM codepage, but since CHCP 65001 command itself does not have non-ASCII characters it understood correctly. All following commands will be executed in UTF-8 and may have non-ASCII characters.
You may now insert em-dash into the filename and it won't be replaced with ? upon saving.
Set UTF-8 font
Unfortunately default console window font does not display UTF-8 correctly so you won't be able to see non-ASCII characters correctly.
To solve this you should right-click command-line window title bar, select properties, and change font to UNICODE one. Consolas, Lucida Console and Courier New should work.
Thanks for all the responses.
I've had to make a design change to my DB so have managed to get around the need to do this now (phew :))
Thanks again

How does ¯¯¯¯¯ become ùùùùùùùùù

I have tried: echo ¯¯¯¯¯
but the result becomes
ùùùùùùùùù
this was not the output expected.
the expected output was ¯¯¯¯¯
My previous solution, which saves the batch script in Unicode UTF-8 without BOM and codepage 65001 seems to have issues with both console and C runtime as user #eryksun mentioned.
#eryksun also mentioned in our chat:
Like I said, all of the codepages are supersets of ASCII, so what I mean is to limit the rest of the batch script to just ASCII characters, because they can be decoded properly regardless of the console codepage.
Unicode UTF-8
chcp 65001
echo ¯¯¯¯¯
chcp [Original Codepage]
Explanation by #eryksun:
CMD decodes line by line, i.e. you can change to codepage 65001 just for the non-ASCII lines and then switch back to the original codepage.
If you don't use an editor such as Notepad++ that can save UTF-8 without a BOM (byte order mark), CMD will see the first line as an error since it doesn't know to ignore a BOM.

ASCII Code [☢] in Batch

Ive tried pasting this character: ☢ into a txt file,but when I use TYPE,it doesn't show the symbol? Is there a certain code page?
Tried different encoding options.
I know about the CHCP command and tried different pages.
As Josep indicates, the text file containing the UTF-8 character has to be encoded in UTF-8 with BOM.
Also, your console font has to include that character. The default fonts don't. I just tested DejaVu Sans Mono, and it works. See this page for instructions on how to add a TrueType font to the cmd console.
Finally, you can get around messing with the code page garbage by invoking powershell. That way you don't have to mess with scraping the current code page, changing it to 65001, doing your thing, then restoring back to original. Just do this:
powershell "gc nuke.txt"
... where nuke.txt contains ☢
Edit: Since you're unable to install new fonts on your computer, the only solution is ascii art.
___
\_/
.--,O.--,
\/ \/
You have to make sure your file has the UTF8 BOM (Use Notepad++ or the option suggested in the answers below), then you may need to change the character set of your console with chcp 65001, see this answers for more information:
How to make Unicode charset in cmd.exe by default?
How to convert a batch file stored in utf-8 to something that works via another batch file and run it

Batch File Character Encoding on Windows Seven?

How do I know what character encoding to save a batch file as?
I've heard that windows uses difrent charset for cmd and GUIs.
It depends on the character set implemented by cmd.
C:\Users\>chcp
Active code page: 437
Code page 437 is (I believe) the default for Windows systems. It uses ASCII encoding, and the following character set:
You can use chcp to display and config the active code page.
Show chcp usage:
C:\> chcp /?
Show current code page:
C:\> chcp
Config active code page (example):
C:\> chcp 437
I think the question has to do with localization, e.g., for echo messages in a .bat file. (and not line-endings or anything like that)
The question for me is then how can a .bat file control being treated as UTF-8 and not the local default code page. Is there a cue to cmd.exe that will get that to work?
In scripts built and used in an international setting, this matters.

Batch script Latin characters

I am writing a batch script to go through some directories doing an specific task, something like the following:
set DBCreationScript=//Here I set the full path for the script
echo %DBCreationScript%
Problem is the path has got some latin characters (ç, ã, á) and when I run the script, the output shows strange characters, not the ones I typed in. The batch script is in ANSI encoding.
I already tried to set the script encoding to UTF-8, but apparently the batch interpreter can't handle the control characters that appear on the beggining of the file.
Any thoughts?
Save the batch file in OEM encoding (a decent editor should allow this) or change the code page prior to running it with
chcp 1252
You can also save it as UTF-8 without signature (BOM) and use
chcp 65001
but down that path lies peril and dragons await to eat you (in short: It's usually painful and has a few weird side-effects).

Resources