Fixed-length data is hard to understand as text: Use PowerShell to run a hex dump and inspect boundaries

Programming / Web Development

About this article
This article was created using an automated generation workflow leveraging generative AI. It reviews the .NET Encoding API, examines dummy fixed-length records as byte sequences, and organizes them to help investigate item misalignment and character encoding mismatches in business files.

Verification status: 📘 Confirmed with official .NET specifications; actual device testing not yet performed

In fixed-length files, spaces are also data. Looking at byte positions rather than just strings on the screen clarifies "which item starts and ends where."

This concept is useful when investigating why integration files from legacy core systems, Zengin-system-style fixed-width data, or outsourced vendors fail to import despite "looking correct on the surface."

Success criteria for this task

The task is successful if you can convert a single record into an ASCII byte sequence and verify characters, spaces, and item boundaries with their positions.

Try this first

$record = '001PANDA     025'
$bytes = [Text.Encoding]::ASCII.GetBytes($record)
0..($bytes.Length-1) | ForEach-Object {
  '{0:D2}: {1:X2} {2}' -f $_,$bytes[$_],[char]$bytes[$_]
}

Look here

Spaces exist as20. They do not simply disappear visually; they are bytes used to pad the item width.

For example, assume this dummy record follows the specification below.

Byte positionItemWidth
0-2Code3 bytes
3-12Name10 bytes
13-15Quantity3 bytes

PANDAThe spaces following are also data used to pad the 10-byte name field.

Slicing by item

Fixed-length data reveals its structure when sliced by byte positions.

$code = [Text.Encoding]::ASCII.GetString($bytes, 0, 3)
$name = [Text.Encoding]::ASCII.GetString($bytes, 3, 10)
$qty  = [Text.Encoding]::ASCII.GetString($bytes, 13, 3)

[pscustomobject]@{
  Code = $code
  Name = $name.TrimEnd()
  Qty  = $qty
}

Rather than looking at the entire string, you can directly cross-reference it with the specification definitions of "3 bytes, 10 bytes, 3 bytes."

Changing one part

PANDAfromパンダto and setting the encoding to UTF-8.

$record = '001パンダ     025'
$bytes = [Text.Encoding]::UTF8.GetBytes($record)

"文字数 = $($record.Length)"
"byte数 = $($bytes.Length)"

The character count and the byte count will no longer match.

What this shows is that"10 characters" and "10 bytes" in a fixed-length specification are not the same thing..

Why does this happen?

A file is ultimately a sequence of bytes. Character encoding determines which byte sequence characters are converted into.

ASCII alphanumeric characters are basically one character per byte, making it easy for character counts and byte counts to align. On the other hand, using Japanese characters in UTF-8 results in multiple bytes per character, so managing fixed widths solely by character count can cause boundaries to shift.

graph LR
A[文字列] --> B[Encoding]
B --> C[byte列]
C --> D[固定位置で項目分割]

Intentionally breaking it

Suppose the fixed-length specification states a "10-byte name," but you insert 10 characters of UTF-8 Japanese text based solely on character count.

In that case, the name field will exceed 10 bytes and potentially encroach into the starting position of the subsequent quantity field.

In other words, even if the specification is

コード | 名称       | 数量
001    | 10byte固定 | 025

, if the name actually expands to 13 bytes, 15 bytes, and so on, all subsequent item positions will be misaligned.

Errors of this type are difficult to diagnose simply by looking at strings on a screen.

Going a step deeper: Inspecting byte counts instead of character counts

As a business validation check, you can pre-inspect the byte count for each item.

$name = 'パンダ'
$encoding = [Text.Encoding]::UTF8
$byteCount = $encoding.GetByteCount($name)

[pscustomobject]@{
  Value      = $name
  CharCount  = $name.Length
  ByteCount  = $byteCount
  LimitBytes = 10
  Fits       = ($byteCount -le 10)
}

FitsBy checking , you can programmatically confirm whether "this value can be placed into a 10-byte field."

Where this can be applied in clerical work and business operations

1. Investigating upload errors in core systems

In operations where fixed-length files rather than CSVs are uploaded, if you encounter phenomena such as:

  • Only specific rows result in errors

  • Only rows containing Japanese text fail

  • Quantities or amounts shifting into adjacent items

You can anonymize the target row and inspect the byte positions to narrow down the cause.

2. Pre-handover checks for data created in Excel

In operations where clerical staff enter data in Excel and finally convert it into fixed-length text, checking cellLENalone may not be enough to determine byte limits.

You can inspect byte counts on the PowerShell side and develop a simple checking tool to verify whether:

  • The data is within the specified byte limit

  • Full-width characters are mixed in

  • Trailing spaces are required

.

3. Specification confirmation with vendors

Specifications that merely state "10-digit name" are sometimes insufficient.

The things you should verify are:

  • Whether it is 10 characters or 10 bytes

  • What the encoding is

  • Whether padding consists of spaces0or

  • Left-alignment or right-alignment

  • Whether newline codes are included in the record length

.

Being able to ask these five questions alone can reduce troubleshooting before acceptance testing.

4. Confirming invisible spaces

When you encounter issues where "data looks identical in Excel, but only one side errors out," subtle differences such as half-width spaces, full-width spaces, nulls, or line breaks can be the cause.

Hex display is a tool toconvert those into visible values.

When using this in your work

When investigating Zengin-system or legacy integration files, check the offsets, encoding, padding, and presence of line breaks in the specification, and analyze anonymized samples rather than actual production data.

It is safest to first verify the structure with just a single record, and then expand to checking all records. Never overwrite production files immediately; start with read-only inspections.

Microsoft Encoding.GetBytes: https://learn.microsoft.com/dotnet/api/system.text.encoding.getbytes

Microsoft Encoding.GetByteCount: https://learn.microsoft.com/dotnet/api/system.text.encoding.getbytecount

Document information

Article title
Fixed-length data is hard to understand as text: Use PowerShell to run a hex dump and inspect boundaries
Published
Updated
Source
https://papanda925.com/?p=15467&lang=en

License: Text and original figures for which this site holds the relevant rights are available under CC BY 4.0 , unless otherwise noted. This article may include content created or edited with generative AI. If code has a separate license notice or a linked GitHub repository license, that license takes precedence for the code. Quotations, third-party materials, images, and trademarks are excluded from this license. Usage policy

Copied title and URL