character conversion error......!

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
prash11
Posts: 21
Joined: 27 Apr 2012 01:38

character conversion error......!

#1 Post by prash11 » 27 Apr 2012 01:41

hi
i am using batch file to run one program but while doing that i m using § this character in .bat file.
but the problem is that when i am trying to run that .bat file, the DOS is converting the § character to another character.
why DOS is doing this?
how do i solve this?
any suggestions?
thank you.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: character conversion error......!

#2 Post by aGerman » 27 Apr 2012 04:38

It's because Windows works with ANSI (ACP) while the console window works with ASCII (OEMCP) for compatibility reasons with DOS or whatever.
It's always a good idea to use only the default ASCII characters which are the same in ANSI. Since I don't know your default ANSI code page it's difficult for me to suggest a possible solution.

Regards
aGerman

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: character conversion error......!

#3 Post by Fawers » 27 Apr 2012 06:16

As aGerman said, it depends on your system code page.

Anyway, try using this character and see what it outputs.

Code: Select all

õ

prash11
Posts: 21
Joined: 27 Apr 2012 01:38

Re: character conversion error......!

#4 Post by prash11 » 27 Apr 2012 06:17

i am using command prompt Microsoft Windows XP [Version 5.1.2600].
i am using that character as password in batch file.
that batch file contains
wget ftp://username:password§§@ftp.xxx.xx
wget is the software i am using to to download file using ftp.
but i am not able to login on that machine because of password.
if i type it manually it works but it fails if i use it in batch file

prash11
Posts: 21
Joined: 27 Apr 2012 01:38

Re: character conversion error......!

#5 Post by prash11 » 27 Apr 2012 06:19

as Fawers said i tried õ but this character was converted to ⌡

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: character conversion error......!

#6 Post by Fawers » 27 Apr 2012 06:37

Then you might want to try a plain text editor with OEM code page support.

I use and recommend PSPad.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: character conversion error......!

#7 Post by aGerman » 27 Apr 2012 06:40

Run that batch code to read the ACP in the registry and tell us the 2 numbers it displays.
@echo off
chcp
for /f "tokens=2*" %%i in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Nls\CodePage" /v "ACP"') do set /a acp=%%j 2>nul
echo ACP: %ACP%
pause

Regards
aGerman

prash11
Posts: 21
Joined: 27 Apr 2012 01:38

Re: character conversion error......!

#8 Post by prash11 » 27 Apr 2012 06:45

hi aGerman
it displayed this information.
Active code page: 437
ACP: 1252

prash11
Posts: 21
Joined: 27 Apr 2012 01:38

Re: character conversion error......!

#9 Post by prash11 » 27 Apr 2012 06:51

hi Fawers
i tried with that editor.
i copied that code in new file of the PSpad and saved it as .bat file still it is not working.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: character conversion error......!

#10 Post by aGerman » 27 Apr 2012 07:35

Code: Select all

@echo off
>nul chcp 1252
set "p=§"

:: apply the variable
>"test.txt" echo %p%
start "" notepad "test.txt"


:: display the variable
>nul chcp 437
echo %p%

pause

Regards
aGerman

prash11
Posts: 21
Joined: 27 Apr 2012 01:38

Re: character conversion error......!

#11 Post by prash11 » 27 Apr 2012 07:40

hi aGerman
thnks man....!
error solved...!
it works great...!
Hats off to you...!

prash11
Posts: 21
Joined: 27 Apr 2012 01:38

Re: character conversion error......!

#12 Post by prash11 » 27 Apr 2012 07:55

hi aGerman,
your solution worked great.
can u tell me what did you do by asking me those numbers?
what about first program? what u did there?
what those numbers indicates?
and what about second program?
what you are doing over there?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: character conversion error......!

#13 Post by aGerman » 27 Apr 2012 09:51

I'll try to cut a long story short ...

The most text editors work with the default ANSI codepage (Windows-1252 in your case). Each character is represented as 8 bits (a combination of either 0 or 1). Have a look at the link. In the table you'll find character "§" in row A and column 7. That means A7 is the hexadecimal representation of that character in codepage 1252. Hence the following 8 bits (= 1 byte) are written to the file: 10100111.
The cmd.exe reads that byte and tries to translate it to a character. Cmd.exe works with ASCII though. It's codepage 437 in your case. Have a look at that table and find the character in row A and column 7 :wink:
Now you should understand how it works. The content of the file is exactly the same but the character interpretation is completely different.

What I did in my first code is simple. The CHCP command without an argument displays the current codepage of your batch window (ASCII). Inside of the FOR /F loop I queried the registry to get the default ANSI codepage.

In the second code I applied these codepages. First I changed the codepage of the batch window to ANSI. Now the byte 10100111 is interpreted as § and assigned to variable p.
When I changed back to ASCII an internal process in cmd.exe converts the variable and you can display the § in the batch window.

Regards
aGerman

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: character conversion error......!

#14 Post by Fawers » 27 Apr 2012 10:22

Even though aGerman has already given you a complete, specific answer, I'll reply to this post of yours:
prash11 wrote:hi Fawers
i tried with that editor.
i copied that code in new file of the PSpad and saved it as .bat file still it is not working.

I forgot to say that you have to adjust the formatting.
Image


Image

Post Reply