G grafsoft Well-Known Member Licensed User Longtime User Jul 31, 2017 #1 Hi, I am reading an int with the readint method of RandomAccessFile. Now I have a negative integer, but what I need is an unsignedd int. I have trie3d to add different powers of 2, but to no avail. Thanks for your help! Peter
Hi, I am reading an int with the readint method of RandomAccessFile. Now I have a negative integer, but what I need is an unsignedd int. I have trie3d to add different powers of 2, but to no avail. Thanks for your help! Peter
DonManfred Expert Licensed User Longtime User Jul 31, 2017 #2 Using the forumsearch COULD HAVE HELPED. It is working! B4X: Sub ToUnsigned(b As Byte) As Int Return Bit.And(0xFF, b) End Sub 'check: Log(Bit.ToHexString(ToUnsigned(0xAA))) Upvote 0
Using the forumsearch COULD HAVE HELPED. It is working! B4X: Sub ToUnsigned(b As Byte) As Int Return Bit.And(0xFF, b) End Sub 'check: Log(Bit.ToHexString(ToUnsigned(0xAA)))
Erel B4X founder Staff member Licensed User Longtime User Jul 31, 2017 #3 It is actually a bit more complicated to convert an int to unsigned int. 1. Add AndLong from here: https://www.b4x.com/android/forum/threads/bit-and-and-bit-or-for-longs.53755/#post-336996 2. B4X: Sub ToUnsigned(i As Int) As Long Return AndLong(i, 0xFFFFFFFF) End Sub Upvote 0
It is actually a bit more complicated to convert an int to unsigned int. 1. Add AndLong from here: https://www.b4x.com/android/forum/threads/bit-and-and-bit-or-for-longs.53755/#post-336996 2. B4X: Sub ToUnsigned(i As Int) As Long Return AndLong(i, 0xFFFFFFFF) End Sub
G grafsoft Well-Known Member Licensed User Longtime User Aug 1, 2017 #4 Thank you, but it does not work. Start, choose the number 1 and press OK. Line 80 in main.b4a. Attachments bintest.zip 60.3 KB · Views: 378 Upvote 0
Erel B4X founder Staff member Licensed User Longtime User Aug 1, 2017 #5 Do you get any error? Upvote 0
G grafsoft Well-Known Member Licensed User Longtime User Aug 1, 2017 #6 No. But I have a value of -400818176, which is converted into -4.00818176E8 Unsigned should be positive, I think a value of about 7000 should come out. Upvote 0
No. But I have a value of -400818176, which is converted into -4.00818176E8 Unsigned should be positive, I think a value of about 7000 should come out.
Erel B4X founder Staff member Licensed User Longtime User Aug 1, 2017 #7 Use this code: B4X: Sub ToUnsigned(i As Int) As Long Return AndLong(i, 4294967295) End Sub Upvote 0
G grafsoft Well-Known Member Licensed User Longtime User Aug 2, 2017 #8 Still does not work, produces -400818176. I made a little project that does only use this function and added it to this post. Attachments test2.zip 7.1 KB · Views: 348 Upvote 0
Still does not work, produces -400818176. I made a little project that does only use this function and added it to this post.
Erel B4X founder Staff member Licensed User Longtime User Aug 2, 2017 #9 Your code is wrong. The return type is Long. You cannot store the possible range of unsigned int values in an Int. Upvote 0
Your code is wrong. The return type is Long. You cannot store the possible range of unsigned int values in an Int.