Skip to Content
go-dicom(3)
Packages

values

Converts raw element bytes to Go values by VR, and Go values back to bytes: numbers in a byte order, strings split on \, padding.

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

When to use it

When you have the raw value bytes of an element and its VR, outside a data set, and want a number or a string from them; or the reverse. Nothing else in the module uses it: the reader decodes values itself.

For values of a read file, use GetStringByKeyword and the element's GetValue and Bytes in dataset and dataelem.

Key types and functions

NameWhat it is for
ConvertValueBytes of a VR to a Go value: uint64 for US and UL, float64 for FD and a single DS, a string for text.
EncodeValueA Go value to the bytes of a VR.
ConvertTagFour bytes to group and element.
MultiStringFloat, MultiStringInt0.5\0.25 as []float64; IS values as []int64.
MultiStringSplit on \ with your own converter.
PadString, SanitizeStringPad to a length; strip control characters.
ValidateNumericStringCheck DS and IS text.
ParsedValue, RawDataElementA raw element and what it converted to.

Example

// Convert value bytes to Go values by VR, and back.
package main

import (
	"fmt"
	"log"

	"github.com/amrshadid/go-dicom/values"
)

func main() {
	v, err := values.ConvertValue("US", []byte{0x00, 0x02}, true) // little endian
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("US: %v (%T)\n", v, v)

	ds, err := values.ConvertValue("DS", []byte("0.661468 "), true)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("DS: %v (%T)\n", ds, ds)

	fmt.Println("spacing:", values.MultiStringFloat(`0.5\0.25`))

	b, err := values.EncodeValue("IS", int64(789), true)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("IS bytes: %q\n", b)
}

It prints US: 512 (uint64), DS: 0.661468 (float64), spacing: [0.5 0.25] and IS bytes: "789".

Notes

  • ConvertValue returns only the first value of a binary number: four bytes of US give one uint64, not two. A DS or IS with several values comes back as one string; split it with MultiStringFloat or MultiStringInt.
  • Text is not decoded from its Specific Character Set; see charset.

Note

This is a standalone helper with no users inside the module. Prefer the dataset and dataelem accessors for values of a file.

See also

dataelem · valuerep · multival · charset

On this page