# Binary <-> Decimal Converter

## Recommended Posts

Hiya,

i did not find any Script/Function to convert decimal values to binary strings and vice versa, so i just wrote them by myself. I don't know if anyone except me will ever need this, but maybe someone will find it helpful

```Func Dec2Bin(\$decimal)
\$binary = ''
While \$decimal>0
\$binary = String(Mod(\$decimal, 2)) & \$binary
\$decimal = Int(\$decimal/2)
WEnd
Return \$binary
EndFunc

Func Bin2Dec(\$binary)
\$decimal = 0
For \$i = 0 To StringLen(\$binary) Step 1
\$decimal = \$decimal + Number(StringMid(\$binary, StringLen(\$binary)-\$i, 1))*(2^\$i)
Next
Return \$decimal
EndFunc```

Greetings,

ZeD

##### Share on other sites

Good Job! It appears to work.

*** Matt @ MPCS

##### Share on other sites

Nice! Now if someone just had a hex to decimal to binary converter...

##### Share on other sites

There is a Decimal to Hex and Hex to Decimal Converter implemented in AutoIT, named Hex() and Dec().

So you just can call

`Dec2Bin(Dec("FFFF"))`

or did you intend something else?

##### Share on other sites

Hi again,

i was bored, so i wrote the missing Converters, too.

Now its capable to convert Decimal <-> Binary <-> Hexadecimal <-> Decimal

I wonder if the implemented function "Hex()" looks similar to my "Dec2Hex()"? Anyways, mine doesnt put leading "0" on Hexadecimal Numbers.

```Func Dec2Bin(\$decimal)
\$binary = ''
While \$decimal>0
\$binary = String(Mod(\$decimal, 2)) & \$binary
\$decimal = Int(\$decimal/2)
WEnd
Return \$binary
EndFunc

Func Bin2Dec(\$binary)
\$decimal = 0
For \$i = 0 To StringLen(\$binary) Step 1
\$decimal = \$decimal + Number(StringMid(\$binary, StringLen(\$binary)-\$i, 1))*(2^\$i)
Next
Return \$decimal
EndFunc

Func Dec2Hex(\$decimal)
\$hexa = ''
While \$decimal>0
\$single = String(Mod(\$decimal, 16))
If \$single = '10' Then \$single = 'A'
If \$single = '11' Then \$single = 'B'
If \$single = '12' Then \$single = 'C'
If \$single = '13' Then \$single = 'D'
If \$single = '14' Then \$single = 'E'
If \$single = '15' Then \$single = 'F'
\$hexa = \$single & \$hexa
\$decimal = Int(\$decimal/16)
WEnd
Return \$hexa
EndFunc

Func Hex2Dec(\$hexa)
\$decimal = 0
For \$i = 0 To StringLen(\$hexa) Step 1
\$char = StringMid(\$hexa, StringLen(\$hexa)-\$i, 1)
If \$char='A' Then
\$single = 10
ElseIf \$char='B' Then
\$single = 11
ElseIf \$char='C' Then
\$single = 12
ElseIf \$char='D' Then
\$single = 13
ElseIf \$char='E' Then
\$single = 14
ElseIf \$char='F' Then
\$single = 15
Else
\$single = Number(\$char)
EndIf
\$decimal = \$decimal + \$single*(16^\$i)
Next
Return \$decimal
EndFunc

Func Bin2Hex(\$binary)
Return Dec2Hex(Bin2Dec(\$binary))
EndFunc

Func Hex2Bin(\$hexa)
Return Dec2Bin(Hex2Dec(\$hexa))
EndFunc```

Greetings,

ZeD

##### Share on other sites

If you search the forum, you should find several base 2 - 36 converters several of us wrote using different strategies...

##### Share on other sites

DOH!!!

I'm sorry. I DID search the forum but it seems like i used wrong keywords.

I will never post any functions again, i promise

Greetings,

ZeD

##### Share on other sites

I will never post any functions again, i promise

Nevermind! I think it's just interesting to compare different ways to make the same things Different methods may vary by speed, range of use etc. For example, how many sorting methods we have?..

##### Share on other sites

@Kot

As I have been advised to rethink about my Avatar I think it's fair enough to let you know that your nick could be a challenge if used in Austria/Germany and parts of Switzerland - means in an environment where people are used, or able to speak German .

Click this --> [Kot]

##### Share on other sites

Mmm...

I always know before that this was mistake... but I don't know HOW This is like confusion with the "Matroska" container name.

I would like to change this to more neitral but this is not so easy as changing avatar... or?

##### Share on other sites

There is a good point. I think you would have to ask an administrator of the board like jon or larry to transfer your account in the backend to allow you to keep your status and information.

Who else would I be?
##### Share on other sites

@Kot

As I have been advised to rethink about my Avatar    I think it's fair enough to let you know that your nick could be a challenge if used in Austria/Germany and parts of Switzerland - means in an environment where people are used, or able to speak German  .

Click this --> [Kot]

Someone asked you to change avatar? What just because it's aguy with horns/devil thing? Does that mean I can't use this emoticon anymore

?

Tut.

##### Share on other sites

Jon i know someone asked beastmaster if his avitar was demonic. i dunno about you, but what others think about a name or picture is just subjective. Beastmaster shouldn't take offense to any of it... anyone that uses internet explorer is demonic follower....

##### Share on other sites

Everyone has a chance to choose the right or wrong way. What choice they make is theirs to make, but I am trying to help them out.

@jon, The emoticon is not the best. I am not telling you not to use it, and I didn't say that I requied anyone needed to change their avatar. I simply said that I hated to see it and that it was demonic.

@beerman, No offense was intended to beastmaster. Because he has an avatar that reflects something demonic doesn't mean I want to run him off of this forum. I would simply prefer not to see it. As for your signature, I refer to John 10:1-4 Those who know God hear his voice. Those who don't know Him and still hear voices are those who have problems. Also, It is God, not god. A god is an idol. Jesus is God [capital G].

I also mean no offense to you. I have no problems with you as a person. The problems I may have are related to the things people do or say. No harm is intended towards anyone here.

Who else would I be?
##### Share on other sites

I would advise you very strongly to keep religion off this forum. I can promise you that you won't like what you find.

If you have a problem with that avatar, block it, or if thats not an option and it bugs you bad enough, disable avatars altogether (Its a forum option). And I also don't think its very nice you asked BL@(K-R34P3R to change his signature (Provided the one in question is: -I am the giver of life and the bringer of death.). You can disable signatures as well.

##### Share on other sites

@valik, Thank you for your information. Post edited on BL@(K-R34P3R. How do I disable signatures?

Who else would I be?
##### Share on other sites

My Controls->Board Settings->Board Display Settings (Top item under that)

##### Share on other sites

Yay! no avatars, no sigs. Not even my own. LOL

Who else would I be?
##### Share on other sites

I also usually disable those things in Forums. But in this one I found no need. Avatars have to be small, and I saw only one sig with a big pic.

I saw forums with huge pictures every signature... Horrible.

##### Share on other sites

Jesus is God [capital G].

<{POST_SNAPBACK}>

And now you're offending me, much more than any userpic or emoticon ever could. But I think we should just go with Valik's suggestion and not use this place as a forum for religeous discussion.

I would advise you very strongly to keep religion off this forum.  I can promise you that you won't like what you find.

If you have a problem with that avatar, block it, or if thats not an option and it bugs you bad enough, disable avatars altogether (Its a forum option).  And I also don't think its very nice you asked BL@(K-R34P3R to change his signature (Provided the one in question is: -I am the giver of life and the bringer of death.).  You can disable signatures as well.

<{POST_SNAPBACK}>

No one's perfect, right?

Edited by emmanuel

"I'm not even supposed to be here today!" -Dante (Hicks)

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...