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

Reading RK Number type is buggy #35

Open
fenisteel opened this issue Aug 27, 2024 · 0 comments
Open

Reading RK Number type is buggy #35

fenisteel opened this issue Aug 27, 2024 · 0 comments

Comments

@fenisteel
Copy link

If you try to read large negative numbers, like -11 678 001, you got 1 062 063 823 instead. To fix this, replace the RKNum struct's number() method to this one:

func (r *RKNum) number() (intNum int64, floatNum float64, isFloat bool) {
	rk := helpers.BytesToUint32(r[:])
	isFloat = rk&0x02 == 0
	isMul := rk&0x01 == 1
	if isFloat {
		floatNum = math.Float64frombits(uint64(rk&0xfffffffc) << 32)
		if isMul {
			floatNum /= 100
		}
	} else {
		intNum32 := uint32(rk >> 2)
		if rk&0x80000000 > 0 {
			intNum32 = intNum32 | 0xC0000000
		}
		if isMul {
			intNum32 /= 100
		}
		intNum = int64(int32(intNum32))
	}
	return
}

The problem here, that there is an undocumented sign handling by Microsoft, that you must take into consideration. After you convert the number to an uint32, you must check the original number's leftmost bit, and if its one, then you must OR your number with 0xC0000000 to make the overflowing 32 bit uint into a signed 32 bit int. Then you can convert this into a 64bit signed int.

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

No branches or pull requests

1 participant