由于历史原因Windows平台上各个地区的txt、csv等默认编码不一,比如中国区为gb2312,
可以用powershell获取系统编码详细信息
> powershell [System.Text.Encoding]::Default
IsSingleByte : True
BodyName : iso-8859-1
EncodingName : Western European (Windows)
HeaderName : Windows-1252
WebName : Windows-1252
WindowsCodePage : 1252
IsBrowserDisplay : True
IsBrowserSave : True
IsMailNewsDisplay : True
IsMailNewsSave : True
EncoderFallback : System.Text.InternalEncoderBestFitFallback
DecoderFallback : System.Text.InternalDecoderBestFitFallback
IsReadOnly : True
CodePage : 1252
> powershell [System.Text.Encoding]::Default
BodyName : gb2312
EncodingName : Chinese Simplified (GB2312)
HeaderName : gb2312
WebName : gb2312
WindowsCodePage : 936
IsBrowserDisplay : True
IsBrowserSave : True
IsMailNewsDisplay : True
IsMailNewsSave : True
IsSingleByte : False
EncoderFallback : System.Text.InternalEncoderBestFitFallback
DecoderFallback : System.Text.InternalDecoderBestFitFallback
IsReadOnly : True
CodePage : 936
node.js中可通过execSync获取,Typescript 代码如下:
import { execSync } from "child_process"
import { SystemTextEncoding } from "./encoding.interface"
export const getEncoding = () => {
const stdout = execSync('powershell [System.Text.Encoding]::Default')
const strEncoding = stdout.toString()
const encoding: any = {}
for (let line of strEncoding.split(/\r\n/g)) {
if (line) {
let [ key, value ] = line.split(':')
encoding[key.trim()] = value.trim()
}
}
return encoding as SystemTextEncoding
}
回复 (0)
微信扫码 立即评论