documents:windows:powershell:powersahell-005
ファイル調査2:バイト配列から文字列に変換
2025-09-10
自分用メモ
これは何?
PowerShellでバイト列から文字列を表示させる簡単な例。
ファイル調査1:Format-Hexの使い方メモの続きで、項目NAMEを文字列として表示したい場合に対応している。
項目NAME値の調査をする
項目NAMEは漢字の項目で、H社ホストでは漢字コードにSJISを使っている事も分かっている。項目NAMEをバイト列から文字列に直して表示してみればいい。
項目NAMEの変換
ファイル内容をバイト配列として保存し、配列の範囲を System.Text.Encodingオブジェクトに与えて文字列に戻してあげる。
項目NAMEは
- 1レコード目:配列要素 0番~13番で、 4~11番
- 2レコード目:配列要素 14番~27番で、18~25番
- 3レコード目:配列要素 28番~41番で、32~39番
となる。
文字列に戻した結果、
- 1レコード目は「柴犬 」
- 2レコード目は「柴犬 」
- 3レコード目は「土佐犬 」
となった。
| レコード | バイト範囲 | 値(SJIS時は1文字2バイト) | 文字列 | 備考等 |
|---|---|---|---|---|
| 1 | 4~11 | 8EC4 8CA2 8140 8140 | 柴犬 | 最後2文字は全角空白(8140 8140) |
| 2 | 18~25 | 8EC4 8CA2 8140 8140 | 柴犬 | 最後2文字は全角空白(8140 8140) |
| 3 | 32~39 | 9379 8DB2 8CA2 8140 | 土佐犬 | 最後1文字は全角空白(8140) |
PowerShell 7.5 の場合
Get-Content のオプションが 5.1 のものとは違うので注意。
PS E:\WK> $PSVersionTable
Name Value
---- -----
PSVersion 7.5.2
PSEdition Core
GitCommitId 7.5.2
OS Microsoft Windows 10.0.22631
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
PS E:\WK> $bindata = Get-Content -Path SAMPLEBIN -AsByteStream -Raw ← ファイルをバイト型配列としてメモリに確保する。
PS E:\WK> $enc = [System.Text.Encoding]::GetEncoding(932) ← CP932 ※MS932, Windows-31j
PS E:\WK> $bindata[4..11] | Format-Hex
Label: Byte (System.Byte) <35EF1272>
Offset Bytes Ascii
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
------ ----------------------------------------------- -----
0000000000000000 8E C4 8C A2 81 40 81 40 �Ä�¢�@�@ ← SJISの4文字で、最後2文字は全角空白2文字だと分かる
PS E:\WK> $enc.GetString($bindata[4..11])
柴犬 ← しばいぬ
PS E:\WK> $bindata[18..25] | Format-Hex
Label: Byte (System.Byte) <1A796072>
Offset Bytes Ascii
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
------ ----------------------------------------------- -----
0000000000000000 8E C4 8C A2 81 40 81 40 �Ä�¢�@�@ ← SJISの4文字で、最後2文字は全角空白2文字だと分かる
PS E:\WK> $enc.GetString($bindata[18..25])
柴犬 ← しばいぬ
PS E:\WK> $bindata[32..39] | Format-Hex
Label: Byte (System.Byte) <18C69626>
Offset Bytes Ascii
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
------ ----------------------------------------------- -----
0000000000000000 93 79 8D B2 8C A2 81 40 �y�²�¢�@ ← SJISの4文字で、最後1文字は全角空白1文字だと分かる
PS E:\WK> $enc.GetString($bindata[32..39])
土佐犬 ← とさいぬ?とさけん?
PS E:\WK>
PowerShell 5.1 の場合
Get-Content のオプションが 7.5 のものとは違うので注意。
PS E:\WK> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.22621.5697
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.22621.5697
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
PS E:\WK> $bindata = Get-Content -Path SAMPLEBIN -Raw -Encoding Byte ← ファイルをバイト型配列としてメモリに確保する。
PS E:\WK> $enc = [System.Text.Encoding]::GetEncoding(932) ← CP932 ※MS932, Windows-31j
PS E:\WK> $bindata[4..11] | Format-Hex
パス:
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00000000 8E C4 8C A2 81 40 81 40 Ä¢@@ ← SJISの4文字で、最後2文字は全角空白2文字だと分かる
PS E:\WK> $enc.GetString($bindata[4..11])
柴犬 ← しばいぬ
PS E:\WK> $bindata[18..25] | Format-Hex
パス:
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00000000 8E C4 8C A2 81 40 81 40 Ä¢@@ ← SJISの4文字で、最後2文字は全角空白2文字だと分かる
PS E:\WK> $enc.GetString($bindata[18..25])
柴犬 ← しばいぬ
PS E:\WK> $bindata[32..39] | Format-Hex
パス:
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00000000 93 79 8D B2 8C A2 81 40 y²¢@ ← SJISの4文字で、最後1文字は全角空白1文字だと分かる
PS E:\WK> $enc.GetString($bindata[32..39])
土佐犬 ← とさいぬ?とさけん?
PS E:\WK>
documents/windows/powershell/powersahell-005.txt · 最終更新: by k896951