Jump to content

Binary <-> Decimal Converter


ZeDMIN
 Share

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

Please leave any comments, what you think about it (or just to let me know someone actually READ this post :ph34r:).

Greetings,

ZeD

Link to comment
Share on other sites

Hi again,

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

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

Link to comment
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 :ph34r: Different methods may vary by speed, range of use etc. For example, how many sorting methods we have?..

Link to comment
Share on other sites

  • Administrators

@Kot

As I have been advised to rethink about my Avatar  :ph34r:  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

:lol: ?

Tut.

Link to comment
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?
Link to comment
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.

Link to comment
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}>

Edit: whoops, I hadn't read the second page, please excuse my picking up a dead string with a message of "drop it"

No one's perfect, right?

Edited by emmanuel

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

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...