Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / [SOLVED] Having trouble converting a binary string to decimals .... leading zeroes in a string 'disappear' and break the conversion?

Author
Message
PHeMoX
6
Years of Service
Recently Online
Joined: 9th Jan 2018
Location:
Posted: 25th Oct 2018 14:26 Edited at: 25th Oct 2018 14:27
As the title says, I need some help with converting a binary string to decimals. My code works, but only when the binary string starts with a 1. If it starts with a 0, it gets confused lol.
I suspect this is due to how a string doesn't properly save 'leading zeroes'.

Any tips on how to fix this?

Bad;


Good;

Attachments

Login to view attachments

The author of this post has marked a post as an answer.

Go to answer

Bengismo
7
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 25th Oct 2018 14:50
lol... we would need to see some code otherwise... its just guessing
PHeMoX
6
Years of Service
Recently Online
Joined: 9th Jan 2018
Location:
Posted: 25th Oct 2018 14:51 Edited at: 25th Oct 2018 14:58
I think I have fixed it in terms of the math, by limiting the base it goes through, however the 'leading' zero in the final string that's printed is missing :



From what I can tell, the issue is in this piece:



valu is just an array converted to string to integer; might also be the issue, but the math seems correct, so I'm confused:


Here's the extremely basic conversion code:

Attachments

Login to view attachments
PHeMoX
6
Years of Service
Recently Online
Joined: 9th Jan 2018
Location:
Posted: 25th Oct 2018 15:03
PHeMoX
6
Years of Service
Recently Online
Joined: 9th Jan 2018
Location:
Posted: 25th Oct 2018 15:09
I guess Bin(number) doesn't output leading zeroes or are they simply ignored by Print() ?
Bengismo
7
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 25th Oct 2018 15:17
This post has been marked by the post author as the answer.
Bin ignores leading zeros.....otherwise you would have 32 digits every single time you used it

Most operations dont print leading zeros....int decimal values are not 0000023 etc....

If you do want a minimum number of digits from bin() then simply use something like

function binl(i as integer, digits as integer)

t$ = bin(i)
while len(t$)<digits
t$ = "0" + t$
endwhile

endfunction t$


That will give you a defined number of digits everytime
PHeMoX
6
Years of Service
Recently Online
Joined: 9th Jan 2018
Location:
Posted: 25th Oct 2018 15:29
Thanks! Yeah, I figured as much that Bin ignores leading zeros. Probably makes sense it does. But your code example fixes the issue, thank you.
Carharttguy
8
Years of Service
User Offline
Joined: 8th Jul 2016
Location: Belgium
Posted: 25th Oct 2018 18:13
Bin() just ignores de leading zeros as there are not significant, there is no way for Bin() to guess how muchs zeros you want.
You seem to expect 01000, some people want 00001000, some want 000000000000000000000000000000000000000000001000.
Just add them yourself if you need them is the best solution I think.
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 25th Oct 2018 18:21
You can use left to truncate the value like so
left("00000000000000"+bin(value), 4)
PHeMoX
6
Years of Service
Recently Online
Joined: 9th Jan 2018
Location:
Posted: 25th Oct 2018 18:53
Cool, thanks guys!!

Login to post a reply

Server time is: 2024-11-23 12:41:26
Your offset time is: 2024-11-23 12:41:26