Binary Calculator - Sockets

By dronez4 on Oct 31, 2013

Hi this is a simple script that took me no longer than 10 mins, Literally..
This is as simple as putting a few sockets together and letting it read information off a website.

It works like this, it works in a bot:

(19:37:52) <~dronez4> !bin 100100
(19:37:52) <~RCBOT> Binary Calculation: 36

(19:37:57) <~dronez4> !bin d000
(19:37:57) <~RCBOT> Sorry Invalid calculation

Meh have fun.

on *:text:*:#:{
  if ($1 == !bin) {
    set %channel1 $chan
    if ($2 isnum) {
      set %binnum $2
      getbin
    }
    else {
      msg $chan Sorry Invalid calculation
    }
  }
}

;http://www.calculator.net/binary-calculator.html?b2dnumber1=1010101010&calctype=b2d&x=77&y=19

alias getbin {
  sockclose getbin
  sockopen getbin www.calculator.net 80
}

alias NoHTML {
  if (alt=": isin $1) {
    return $regsubex($1,/<[^>]+(?:>|$)|^[^<>]+>/g,) :emoticon:
  }
  else {
    if (&#39 isin $1) {
      set %removed1 l $remove(%read,&#39)
      return $regsubex($1,/<[^>]+(?:>|$)|^[^<>]+>/g,)
    }
    else {
      return $regsubex($1,/<[^>]+(?:>|$)|^[^<>]+>/g,) 
    }
  }
}
alias striphtml {
  return $regsubex($1,/=[^"]+(?:>|$)|^[^<>]+</g,) 
}

on *:sockread:getbin:{
  if ($sockerr > 0) { return }
  else {
    var %read
    sockread %read 
    if (*Decimal value: <font* iswm %read) {
      msg %channel1 Binary Calculation: $remove($nohtml(%read),result:,Decimal value:)

    }
  }
}

On *:sockopen:getbin: {
  if (!$sockerr) {
    sockwrite -nt getbin GET /binary-calculator.html?b2dnumber1= $+ %binnum $+ &calctype=b2d&x=77&y=19 HTTP/1.0
    sockwrite -n getbin Host: www.calculator.net
    sockwrite -n getbin $crlf
  }
  else { echo -st Socket Error $nopath($script) | sockclose $sockname | return }
}

Comments

Sign in to comment.
Yawhatnever   -  Oct 31, 2013

$base() can convert numbers between arbitrary bases.
For example:

on $*:text:/^!base +[01]+\b/Si:#:msg # Decimal: $base($2, 2, 10)
dronez4  -  Nov 01, 2013

Oh wow, As easy as that.. Didn't think that was as simple as that.. nicely done, Thanks..

Sign in to comment

Sorasyn   -  Oct 31, 2013

Would be magnitudes more efficient if you took the time to calculate it locally instead of reaching out to a website to do it. Not to mention doing it locally would require about 50% less code. Doing it this way does not guarantee longevity. In a few months or whenever they decide to update the website, it'll have to be redone to function properly again.

dronez4  -  Oct 31, 2013

It works doesnt it? ^.^

Sorasyn  -  Oct 31, 2013

In time, no it won't. Do you even know how to convert binary to decimal/hexadecimal/octal etc?

dronez4  -  Nov 01, 2013

Nope, If it's alot of work then i'm not going to bother either..

If it doesn't work in time then that's totally fine, i'll hook it up to multiple website if it fails

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.