PowerShellで作る文字コード表「Shift_JIS|UTF-8|UTF-16」

powershell7 コンピュータ
powershell7

Shift_JISの文字コードを元にUTF-8やUTF-16に変換

<#
.SYNOPSIS
文字コード表を作る

.DESCRIPTION
Shif_JIS,UTF-8,UTF-16

.EXAMPLE
.\Make-CharCodeTable.ps1

.PARAMETER Help
ヘルプを表示

#>
param(
    [switch]
    $Help
)

[System.Text.Encoding]::RegisterProvider([System.Text.CodePagesEncodingProvider]::Instance)
$utf8enc = [System.Text.Encoding]::UTF8
$sjisenc = [System.Text.Encoding]::GetEncoding("Shift_JIS")
$utf16enc = [System.Text.Encoding]::GetEncoding("Unicode")

function bin2text([byte[]]$bytes)
{
    $bytes | ForEach-Object -begin {
        $s=""
    } -Process{
        $s += "{0:X2}" -f $_
    } -end {
        $s
    }
}
function bin2list([byte[]]$bytes)
{
    $utf8bin = [System.Text.Encoding]::Convert($sjisenc, $utf8enc, $sjisbin)
    $utf8str = $utf8enc.GetString($utf8bin)

    $utf16bin = [System.Text.Encoding]::Convert($utf8enc, $utf16enc, $utf8bin)

    $utf8binstr = (bin2text $utf8bin)
    if ($utf8binstr -ne "E383BB") {
        Write-Output ($utf8str + "`t" + (bin2text $sjisbin) + "`t" + $utf8binstr + "`t" + (bin2text $utf16bin))
    }
}

function Main
{

    [byte[]]$sjisbin = @(0x0, 0x0);

    Write-Output ("字`tSJIS`tUTF8`tUTF16")

    for($b1=0x81; $b1 -le 0x9f; $b1++) {
        for($b2=0x40; $b2 -le 0x7e; $b2++) {
            $sjisbin[0] = $b1
            $sjisbin[1] = $b2

            bin2list $sjisbin
        }
    }
    for($b1=0xe0; $b1 -le 0xef; $b1++) {
        for($b2=0x80; $b2 -le 0xfc; $b2++) {
            $sjisbin[0] = $b1
            $sjisbin[1] = $b2

            bin2list $sjisbin
        }
    }
}


if ($Help) {
    Get-Help $PSCommandPath
    Exit 1
}

Main

コメント