[Undocumented] CHCP

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

[Undocumented] CHCP

Post by MigrationUser »

18 Dec 2012 02:32
carlos

Hello. I check by self, more info about CHCP command.
Reading the MSDN I found that in CONSOLE there are a two code pages: For the INPUT and for the OUTPUT.

Then, when you call to CHCP and provide a valid CODEPAGE, this utility set the same codepage for both: INPUT and OUTPUT.
But, when you call the CHCP command, chcp print the codepage of the INPUT assuming that it is the same of the OUPUT.

I write a little program in c for change the codepage of both. You can compile with tiny c for use it.

Code: Select all

/*
codepage.c
Author: Carlos.
Date: 18 dec 2012
Examples of usage:
codepage
codepage IN 437 OUT 437
*/

#include <windows.h>
#define str_to_int(str) strtoul(str, (TCHAR **) NULL, 10)
                           
int main(int argc, char * argv[])
{
	UINT in_cp;
	UINT out_cp;
	
	/* Get the data */
	in_cp = GetConsoleCP();
	out_cp = GetConsoleOutputCP();
	
	if (argc == 3) {
		if ( ! _stricmp(argv[1], "IN") ) {
			in_cp = str_to_int(argv[2]);
		}
		else if ( ! _stricmp(argv[1], "OUT") ) {
			out_cp = str_to_int(argv[2]);
		}
	}
	else if (argc == 5) {
		if ( ! _stricmp(argv[1], "IN") ) {
			in_cp = str_to_int(argv[2]);
			if ( ! _stricmp(argv[3], "OUT") ) {
				out_cp = str_to_int(argv[4]);
			}
		}
		else if ( ! _stricmp(argv[1], "OUT") ) {
			out_cp = str_to_int(argv[2]);
			if ( ! _stricmp(argv[3], "IN") ) {
				in_cp = str_to_int(argv[4]);
			}
		}
	}
	SetConsoleCP(in_cp);
	SetConsoleOutputCP(out_cp);
	/* Ensure valid code pages */
	in_cp = GetConsoleCP();
	out_cp = GetConsoleOutputCP();
	
	/* Print the information */
	printf("IN=%u OUT=%u\n", in_cp, out_cp);
	
	return 0;
}
This is a screenshot of the usage and use of file with name ascii 219.txt and get your conclusions:

Image

Last edited by carlos (18 Dec 2012 06:55)

----------------------------

#2 18 Dec 2012 06:55
carlos


Updated the c code.

----------------------------

#3 10 Sep 2013 16:52
carlos


I found that the concept of input codepage and output codepage can be demostrated with the native command: graftabl:

In this example, first we set input and output codepage to 437, and after set the output codepage to 850. As commented in previous post chcp show the input codepage. graftabl show the output codepage. graftabl only set the output codepage, chcp set input and output codepage.

Code: Select all

C:\>chcp 437
Tabla de códigos activa: 437

C:\>graftabl 850
Tabla de códigos anterior: 437
Tabla de códigos activa:   850

C:\>chcp
Tabla de códigos activa: 437

C:\>graftabl /status
Tabla de códigos activa:   850

C:\>
Last edited by carlos (10 Sep 2013 16:53)

----------------------------

#4 10 Sep 2013 21:24
npocmaka


graftabl is not available for system with higher versions than xp...

----------------------------

#5 11 Sep 2013 01:24
carlos
npocmaka wrote:

graftabl is not available for system with higher versions than xp...
It is available in windows 7 pro.

----------------------------

#6 11 Sep 2013 02:18
foxidrive


It's in em all by the look of it

https://docs.microsoft.com/en-us/previo ... dfrom=MSDN
Last edited by foxidrive (11 Sep 2013 02:19)

----------------------------
#7 11 Sep 2013 06:02
npocmaka
foxidrive wrote:

It's in em all by the look of it

https://docs.microsoft.com/en-us/previo ... dfrom=MSDN
I saw that.But it's not available on my Vista Ultimate.Will check it on the 7...

----------------------------

#8 11 Sep 2013 07:16
npocmaka


nope...

----------------------------

#9 11 Sep 2013 11:07
foxidrive


c:\Windows\System32\graftabl.com in Win 8 32 bit. Maybe yours is 64 bit.

----------------------------

#10 11 Sep 2013 12:56
npocmaka
foxidrive wrote:

c:\Windows\System32\graftabl.com in Win 8 32 bit. Maybe yours is 64 bit.
Yes.My both machines (7&Vista) are 64b. None of them have the command.

----------------------------

#11 11 Sep 2013 15:16
foxidrive


You can see it is a com file in 32 bit windows 8, like debug.com which you don't have in your 64 bit machines either (coz 64 bit can't run .com 16 bit files)

That would be the reason...

Last edited by foxidrive (11 Sep 2013 15:17)

----------------------------

#12 13 Sep 2013 06:18
dbenham


Very interesting.

After reading this, I googled a bit, and found that PowerShell provides independent control of both Input and Output code pages: http://stackoverflow.com/a/5808445/1012053. The nice thing is, the console preserves the code page settings even after PowerShell terminates :)

Code: Select all

powershell [Console]::OutputEncoding=[System.Text.Encoding]::GetEncoding(850);[Console]::InputEncoding=[System.Text.Encoding]::GetEncoding(437)
This should help with 64 bit machines.

Dave Benham
Post Reply