Skip to Content
go-dicom(3)
Packages

util

Debugging helpers: hex dumps, a printed data set, a small summary of common attributes, and a fix for values that use the wrong separator.

import "github.com/amrshadid/go-dicom/util" · pkg.go.dev

When to use it

When you are looking at bytes or a data set by hand: a hex dump of the start of a file, or a quick print of patient, modality and image size.

For a full listing of a file, the command line's show is more complete. For image attributes in code, use dataset.Dataset.GetPixelDataInfo.

Key types and functions

NameWhat it is for
HexDump, HexDumpReaderOffset, hex and ASCII columns, from bytes or a reader.
Bytes2Hex, Hex2BytesBytes as hex text and back.
GetDatasetInfo, DatasetInfoPatient, study date, modality, numbers, rows, columns, bits and UIDs.
PrintDatasetInfoPrint that summary.
DumpDataset, PrettyPrintA data set as text.
FixSeparator, FixSeparatorConfigReplace a wrong separator with a backslash in a copy of a data set.

Example

// Dump the first bytes of a file the way hexdump -C does, and print a summary.
package main

import (
	"bytes"
	"fmt"
	"log"
	"os"

	"github.com/amrshadid/go-dicom/filebase"
	"github.com/amrshadid/go-dicom/filereader"
	"github.com/amrshadid/go-dicom/util"
)

func main() {
	raw, err := os.ReadFile(os.Args[1])
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(util.HexDump(raw[128:176], 128, 0)) // after the preamble

	df, err := filereader.ReadDICOMFile(filebase.NewFileReader(bytes.NewReader(raw)))
	if err != nil {
		log.Fatal(err)
	}
	info, err := util.GetDatasetInfo(df.GetDataset())
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(info.Modality, info.Rows, "x", info.Columns)
}

On CT_small.dcm it prints three lines of hex starting 00000080 44 49 43 4D (DICM) and then CT 128 x 128.

Notes

  • GetDatasetInfo returns values without trimming their padding: a PN may end in a space and a UI in a NUL. Use strings.TrimRight before comparing.
  • No other package in the module uses util.

Note

This is a thin helper package. Prefer GetStringByKeyword and GetPixelDataInfo on the dataset for values you act on.

See also

dataset · filebase · Reading and writing

On this page