Page 1 of 1

character conversion error......!

Posted: 27 Apr 2012 01:41
by prash11
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.

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

Posted: 27 Apr 2012 04:38
by aGerman
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

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

Posted: 27 Apr 2012 06:16
by Fawers
As aGerman said, it depends on your system code page.

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

Code: Select all

õ

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

Posted: 27 Apr 2012 06:17
by prash11
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

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

Posted: 27 Apr 2012 06:19
by prash11
as Fawers said i tried õ but this character was converted to ⌡

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

Posted: 27 Apr 2012 06:37
by Fawers
Then you might want to try a plain text editor with OEM code page support.

I use and recommend PSPad.

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

Posted: 27 Apr 2012 06:40
by aGerman
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

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

Posted: 27 Apr 2012 06:45
by prash11
hi aGerman
it displayed this information.
Active code page: 437
ACP: 1252

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

Posted: 27 Apr 2012 06:51
by prash11
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.

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

Posted: 27 Apr 2012 07:35
by aGerman

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

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

Posted: 27 Apr 2012 07:40
by prash11
hi aGerman
thnks man....!
error solved...!
it works great...!
Hats off to you...!

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

Posted: 27 Apr 2012 07:55
by prash11
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?

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

Posted: 27 Apr 2012 09:51
by aGerman
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

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

Posted: 27 Apr 2012 10:22
by Fawers
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