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
| Name | What it is for |
|---|---|
ConvertValue | Bytes of a VR to a Go value: uint64 for US and UL, float64 for FD and a single DS, a string for text. |
EncodeValue | A Go value to the bytes of a VR. |
ConvertTag | Four bytes to group and element. |
MultiStringFloat, MultiStringInt | 0.5\0.25 as []float64; IS values as []int64. |
MultiString | Split on \ with your own converter. |
PadString, SanitizeString | Pad to a length; strip control characters. |
ValidateNumericString | Check DS and IS text. |
ParsedValue, RawDataElement | A 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
ConvertValuereturns only the first value of a binary number: four bytes of US give oneuint64, not two. A DS or IS with several values comes back as one string; split it withMultiStringFloatorMultiStringInt.- Text is not decoded from its Specific Character Set; see charset.
See also
valuerep
The value representations of PS3.5 as rules: length, character set and form checks for each VR, UID checks, and parsers for DA, TM, DS and IS.
volume
Assembles the slices of a series into a regular stack: which slice comes next, how far apart they are, and where each lies in the patient. It reads geometry and decides order; it does not load pixels.