如何使用go在Windows控制台中正确输出字符串?
发布时间:2020-12-14 01:59:17 所属栏目:Windows 来源:网络整理
导读:我有一个exe in go打印 utf-8个编码的字符串,其中包含特殊字符. 由于该exe用于从控制台窗口使用,因此其输出被破坏,因为Windows使用ibm850编码(也称为 code page 850 ). 你如何确保go exe为控制台窗口打印正确编码的字符串,例如打印: éè????ùòèì? 而不
我有一个exe in go打印
utf-8个编码的字符串,其中包含特殊字符.
由于该exe用于从控制台窗口使用,因此其输出被破坏,因为Windows使用ibm850编码(也称为 code page 850 ).
你如何确保go exe为控制台窗口打印正确编码的字符串,例如打印: éè????ùòèì? 而不是(没有任何翻译到右边charset) ├?├?├?├?├?├┤├╣├▓├?├?├? 解决方法// Alert: This is Windows-specific,uses undocumented methods,does not // handle stdout redirection,does not check for errors,etc. // Use at your own risk. // Tested with Go 1.0.2-windows-amd64. package main import "unicode/utf16" import "syscall" import "unsafe" var modkernel32 = syscall.NewLazyDLL("kernel32.dll") var procWriteConsoleW = modkernel32.NewProc("WriteConsoleW") func consolePrintString(strUtf8 string) { var strUtf16 []uint16 var charsWritten *uint32 strUtf16 = utf16.Encode([]rune(strUtf8)) if len(strUtf16) < 1 { return } syscall.Syscall6(procWriteConsoleW.Addr(),5,uintptr(syscall.Stdout),uintptr(unsafe.Pointer(&strUtf16[0])),uintptr(len(strUtf16)),uintptr(unsafe.Pointer(charsWritten)),uintptr(0),0) } func main() { consolePrintString("Hello ?n") consolePrintString("éè????ùòèì?n") } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- windows – git clone挂起“检查连接”
- windows – 将文件包装在一个可执行包中
- office365 – 未捕获TypeError:window.external.GetContex
- windows – 与findstr.exe进行逻辑OR比较
- Rstudio:设置TO DOs
- 安装MPI for Windows
- skype-for-business – UCWA Skype for Business在线API身份
- windows-7 – 在没有用户登录的情况下运行程序
- 使用语义记录Windows Azure配置PartitionKey
- windows-server-2003 – 如何在Windows中监视镜像磁盘的运行