-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[encoding.binary] signed integer functions #23828
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
Comments
Connected to Huly®: V_0.6-22233 |
I have faced this problem but a workaround is not too difficult, after endianess was resolved: write_a := i32(-3)
write_b := u32(write_a)
println('${write_a:x} ${write_b:x}') // -3 fffffffd
read_a := u32(0xfffffffd)
read_b := i32(read_a)
println('${read_a:x} ${read_b:x}') // fffffffd -3 In another similar problem, I wanted to store a []u8 of maximum length of 8 in sqlite as a number (which saves space). But sqlite maximum number is i64 (signed 64 bits) not unsigned. So I solved the problem converting from signed to unsigned and worked for inserting and reading and works ok. |
The cast from |
Seems no precision is lost, just minus sign bit changing of meaning: a := i32(-31415)
b := u32(a)
c := i32(b)
d := u32(c)
assert a == c
assert b == d |
I agree with JalonSolov here, there is a chance of precision loss when moving between u32 and i32 because of the different ranges. |
Event though, as mentioned in issue #23782, signed integers are accepting already big positive numbers that are converted into the fly into negatives. Even though next example gives the same negative s := i8(200) // I wonder this should be permitted
println('${s}') // -56
u := u8(200) // valid
v := i8(u) // I think this is Ok which helps to convert
println('${v}') // -56 |
I got confused by that too yesterday |
Well I meant no single bit is destroyed into going signed and unsigned. Yes, precision or resolution is different, for i8 we can have exactly 127 positive numbers, one 0 and 128 negative numbers. For u8 we can have exactly 255 positive numbers and one 0. |
I think you're right. pub fn big_endian_i32(b []u8) i32 {
return i32(big_endian_u32(b))
} |
To complicate things there is no a single way to represent signed numbers: sign-magnitude, one's complement, two's complement and others. A rare protocol could be using other representation than V's cast. |
Problem solved by #24106 |
Describe the feature
Currently, encoding.binary has functions to encode and decode unsigned integers exclusively. I recently needed to encode and decode signed integers and could not use the encoding.binary functions, as they exclusively handle unsigned integers.
Use Case
I needed this feature to implement a binary protocol client that uses integers in the message exchanged over the network.
Proposed Solution
No response
Other Information
No response
Acknowledgements
Version used
V 0.4.9 5f5e48e
Environment details (OS name and version, etc.)
Note
You can use the 👍 reaction to increase the issue's priority for developers.
Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.
The text was updated successfully, but these errors were encountered: