SoFunction
Updated on 2025-03-03

Solution to the Chinese garbled code in CMD in Windows

Running the go program under cmd or running the go program under GOLAND's Terminal will cause Chinese garbled code.

go run

���� Ping  [127.0.0.1] ���� 32 �ֽڵ�����:
���� 127.0.0.1 �Ļظ�: �ֽ�=32 ʱ��<1ms TTL=128
���� 127.0.0.1 �Ļظ�: �ֽ�=32 ʱ��<1ms TTL=128
���� 127.0.0.1 �Ļظ�: �ֽ�=32 ʱ��<1ms TTL=128
���� 127.0.0.1 �Ļظ�: �ֽ�=32 ʱ��<1ms TTL=128

127.0.0.1 �� Ping ͳ����Ϣ:
    ���ݰ�: �ѷ��� = 4���ѽ��� = 4����ʧ = 0 (0% ��ʧ)��
�����г̵Ĺ���ʱ��(�Ժ���Ϊ��λ):

Because Go's encoding is UTF-8, and CMD's active page is cp936 (GBK), garbled code is generated.

In Chinese Windows systems, if a text file is UTF-8 encoded, the contents in the file cannot be displayed correctly in the command line window (the so-called DOS window). By default, the code page used in the command line window is Chinese or American, that is, the encoding is Chinese character set or English character set.

Run chcp under CMD or Terminal to view the active page code:

chcp
Activity code page: 936

The result is Chinese 936, the code page of UTF-8 is 65001. You can directly use chcp 65001 to change the active code page to 65001, so that the UTF-8 coded displays normally.

chcp 65001
Active code page: 65001

go run 

Pinging  [127.0.0.1] with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

Ping statistics for 127.0.0.1:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

Or convert Chinese to UTF-8 encoding, the complete code is as follows:

package main

import (
 "bufio"
 "fmt"
 "/x/text/encoding/simplifiedchinese"
 "os/exec"
)

type Charset string

const (
 UTF8    = Charset("UTF-8")
 GB18030 = Charset("GB18030")
)

func main() {
 command := "ping"
 params := []string{"127.0.0.1","-t"}
 cmd := (command, params...)
 stdout, err := ()
 if err != nil {
  (err)
  return
 }
 ()
 in := (stdout)
 for () {
  cmdRe:=ConvertByte2String((),"GB18030")
  (cmdRe)
 }
 ()
}

func ConvertByte2String(byte []byte, charset Charset) string {
 var str string
 switch charset {
 case GB18030:
  var decodeBytes,_=simplifiedchinese.().Bytes(byte)
  str= string(decodeBytes)
 case UTF8:
  fallthrough
 default:
  str = string(byte)
 }
 return str
}

Ping 127.0.0.1 has 32 bytes of data:
Reply from 127.0.0.1: Byte=32 Time <1ms TTL=128
Reply from 127.0.0.1: Byte=32 Time <1ms TTL=128
Reply from 127.0.0.1: Byte=32 Time <1ms TTL=128
Reply from 127.0.0.1: Byte=32 Time <1ms TTL=128
Reply from 127.0.0.1: Byte=32 Time <1ms TTL=128
Reply from 127.0.0.1: Byte=32 Time <1ms TTL=128
Reply from 127.0.0.1: Byte=32 Time <1ms TTL=128
Reply from 127.0.0.1: Byte=32 Time <1ms TTL=128

This is the article about the solution to the Chinese garbled code that occurs under CMD under Windows. For more relevant content on executing Go under CMD, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!