Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new field for HexNumerics #301

Closed
wants to merge 1 commit into from
Closed

Conversation

madhr
Copy link

@madhr madhr commented Nov 29, 2023

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

It's related to this change:
#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.

@madhr madhr mentioned this pull request Nov 29, 2023
@madhr madhr marked this pull request as ready for review November 29, 2023 15:55
@madhr madhr requested a review from alovak as a code owner November 29, 2023 15:55
@alovak
Copy link
Contributor

alovak commented Nov 29, 2023

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
Copy link
Contributor

alovak commented Dec 1, 2023

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

@madhr
Copy link
Author

madhr commented Dec 12, 2023

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

yes thanks!

@madhr madhr closed this Dec 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants