$getdec() bin to dec conversion

By Zmodem on Nov 02, 2013

Synopsis: $getdec()

 This is a simple mIRC calculator that will convert the binary input into a decimal value. I wrote this to show how simple a formula for this really is, without using the built-in $base() function of mIRC. Obviously, there is room for improvement, and this may help anyone looking for a loop solution on how to convert these values from one to the other.

Enjoy!

alias getdec {
  ; First we set our variables:
  ; %s = The binary value that the user wishes to convert

  ; %c = We set this at 1 to start. It is the counter to keep track of the looping.
  ;      It will also lend itself to pulling the mid count binary number from the
  ;      string, %s

  ; %l = The length of the binary start value. We will use this to also calculate
  ; our exponent value along the way, while decreasing its value during every loop

  ; %m = This value begins at the length of the input binary number, %s, minus a
  ; single value. The reason for this is that we want to multiply our current looped
  ; value, %cur, by the value 2 with an exponent of %m

  ; %cur = Starts off empty, but eventually this will hold the currently cycled number
  ; from the binary string, %s

  ; %v = Also starts off empty, but this will be our returned value from each loop and
  ; each proper calculation
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  var %s = $1, %c = 1, %l = $len(%s), %m = $calc(%l - 1), %cur, %v

  ; A little error checking. If %s is not a number, it will not continue
    if (%s isnum) {

      ; If %s is 0, return 0 because no calculations need to be made on any combination of
      ; 0's
      if (!%s) { return 0 }

      ; Our loop: As long as %c, our counter, is less than or equal to %l, the length of our
      ; binary string, we continue the loop
      while (%c <= %l) {

        ; We set the currently cycled value, %cur, of our string, %s
        %cur = $mid(%s,%c,1)

        ; If the currently cycled value is not 0, perform our calculation
        if (%cur) { %v = $calc(%v + $calc(%cur * $calc(2 ^ %m))) }

        ; Decrease the mid value, %m, and increase our counter, %c
        dec %m
        inc %c
      }
      ; Return our calculated decimal value
      return %v
   }
}

Comments

Sign in to comment.
Hawkee   -  Nov 05, 2013

Hey Z, nice to see a new post! Still doing mIRC after all these years eh?

Hawkee  -  Nov 05, 2013

Wow, I just noticed this was posted just over 10 years after your first snippet.

Yawhatnever  -  Nov 05, 2013

He made a comment about that too lol

Zmodem  -  Nov 05, 2013

Hey, man! Over the past month and a half I've slowly been getting back into it. I was hanging on IRC and noticed another snippet for doing this via the internet (although mIRC has the base function built-in) and decided to do it all mIRC.

Anyways, how ya doin', man? Haven't talked to you in forever! My fault :-D

Yea, that's CRAZY cool about submitting this like 10 years after my first. I was actually JUST talking about that, like Yawhatnever mentioned.

Hawkee  -  Nov 06, 2013

Been doing great. Keeping busy with a handful of website jobs. I noticed you've still got drinksite on your profile. I sold that site a while back, so you might want to remove it.

Zmodem  -  Nov 06, 2013

You did? haha, hilarious, okay I'll remove it.

As for what you're doing, that's good to hear! Glad that you're keeping busy doing what you love! I've been getting back into the design business as well, but nothing solid yet (still trying to find the best CMS for clients to update their sites; I think Concrete5 is going to be my choice).

Hawkee  -  Nov 06, 2013

I've been working with Drupal quite a bit lately and it's a very solid system. It gives the administrator a lot more control than something like WordPress offers. There's a bit of a learning curve, but it's fairly well supported.

Zmodem  -  Nov 06, 2013

Drupal is really awesome, but I think it's going to be too much for what I have in mind. I'm building mostly local business sites, and I want the owners to have some bare-minimum control over the content (eg: posting news, new products/services, etc). I don't want to pull my email or get cell phone calls/texts of an "Oops, I deleted [xxxxxx]" nature ;)

You know that Drupal was created by the founding member of ProjectX, right? (The old scripting team I was a part of). Dries Buytaert, I think that's how you spell his name. I remember when he first started working on Drop/Drupal, it was a crazy idea at the time.

Sign in to comment

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.