moov-io / iso8583

A golang implementation to marshal and unmarshal iso8583 message.
https://moov.io
Apache License 2.0
352 stars 105 forks source link

Add new field for HexNumerics #301

Closed madhr closed 9 months ago

madhr commented 10 months ago

Add new field type HexNumeric that allows to store binary encoded fields as hex numeric fields.

It's related to this change: https://github.com/moov-io/iso8583/pull/227 where it's now possible to treat binary encoded fields as Hex. Since Hex is a string field, it's a bit inconvenient for fields that store numeric values like amounts (for instance DE55.9F02). Introducing the HexNumeric allows to convert the binary encoded fields straight into a hex numeric field.

alovak commented 10 months ago

Hey, @madhr! Thanks for the PR. I don't think we need the suggested changes as you can get the same results using BCD encoding:

func TestHexNumeric(t *testing.T) {
    numeric := NewNumeric(&Spec{
        Length:      4,
        Description: "Field",
        Enc:         encoding.BCD,
        Pref:        prefix.BCD.Fixed,
    })

    numeric.SetValue(1234)

    packed, err := numeric.Pack()
    require.NoError(t, err)

    require.Equal(t, []byte{0x12, 0x34}, packed)

    numeric2 := NewNumeric(&Spec{
        Length:      4,
        Description: "Field",
        Enc:         encoding.BCD,
        Pref:        prefix.BCD.Fixed,
    })

    _, err = numeric2.Unpack(packed)
    require.NoError(t, err)

    require.Equal(t, int64(1234), numeric2.Value())
}
alovak commented 10 months ago

@madhr does the snippet I shared above work for you?

madhr commented 9 months ago

@madhr does the snippet I shared above work for you?

yes thanks!