Binary<->Text Converter — File Browser

Binary-TextConverterv1.0 / Binary-TextConverterv1.0 / BinText.mrc

BinText.mrc — 12.94 KB — Download this file

;## This i sthe main dialog
dialog BinText {
  title "Text<->Binary Converter"
  size -1 -1 172 251
  option dbu
  list 1, 3 87 165 95, size
  box "Text<->Binary", 2, 1 0 170 65
  box "Convertion Explained", 3, 1 80 170 105
  button "Convert", 4, 1 66 169 15
  box "Convertion", 5, 1 185 170 65
  edit "", 6, 3 8 165 55, multi return autovs
  edit "", 7, 3 193 165 55, multi autovs
}

;## This is the event triggered when the script is loaded.
on *:LOAD: {
  echo 3 -a Binary<->Text converter by funman loaded!
}

;## This is the menu trigger, the menu is the popup you get when rightclicking
menu * {
  Binary<->Text Converter:/dialog -md BinText BinText
}

;## The on dialog event, this one is for sclick (single click) on the dialog with the ID 4 (in this case the "convert" button)
on *:DIALOG:BinText:sclick:4: {
  
  ;## Here we check if the textbox actually contains any text or binary, if not, stop everything
  if (!$did($dname,6).text) { halt }
  
  ;## This checks if the first 8 characters are digits, if they are it is treated as a binary value
  if ($mid($did($dname,6).text,1,8) !isnum) {
    
    ;## These two functions remove any text in the list and output box
    .did -r $dname 1
    .did -r $dname 7
    
    ;## This prepares a variable named "convert" that will be removed after the event is finished
    var %convert
    
    ;## a variable to use with out whie loop, as we're gonna be counting trough lines
    var %a = 1
    
    ;## Ahhh, a while loop, this on etells mIRC to continue doing the stuff inside it until the variable %a is higher then the amount of lines in the textbox
    while (%a <= $did($dname,6).lines) {
      
      ;## Another variable, this time named "b" as %a is taken and we're gonna be using another while loop (You can us eany names really, I just prefer sticking to the alphabet)
      var %b = 1
      
      ;## The second while loop, this will tell mIRC to do the content until %b is higher then the amount of characters in the text
      while (%b <= $len($did($dname,6,%a).text)) {
        
        ;## Here we set the %convert variable to use, making it into the binary value of a character in the text.
        %convert = $t2b($mid($did($dname,6,%a).text,%b,1))
        
        ;## Now that we have a binary value, we add it to the list showing where the different numbers belong etc.
        .did -a $dname 1 $mid($did($dname,6,%a).text,%b,1) $chr(9) $mid(%convert,1,1) $chr(9) $mid(%convert,2,1) $chr(9) $mid(%convert,3,1) $chr(9) $mid(%convert,4,1) $chr(9) $mid(%convert,5,1) $chr(9) $mid(%convert,6,1) $chr(9) $mid(%convert,7,1) $chr(9) $mid(%convert,8,1)
        
        ;## We now add the binary to the convert window, so we in the end will have the entire txt in a long binary line
        .did -a $dname 7 %convert
        
        ;## Increasing the while loop variable is important, or else we will be stuck with an infinitive loop, reezing up your mIRC unless you use Ctrl+Break
        inc %b
        
      ;## This marks the end of this while loop
      }
      
      ;## Same as the "inc %b" but this is for the while loop counting lines of text
      inc %a
      
    ;## Finish the last while loop
    }
    
  ;## Finish the part of the script triggering if the text is not binary
  }
  
  ;## else, this is triggered when the previouse "if" statement is not triggered, in this case, if the text is binary.
  else {
    
    ;## Once again, we clear the list and output fields.
    .did -r $dname 1
    .did -r $dname 7
    
    ;## And once more we prepare the variable "convert"
    var %convert
    
    ;## sets the variable %a for the while loop
    var %a = 1
    
    ;## Like the last time, this loops trough every line of the input field
    while (%a <= $did($dname,6).lines) {
      ;## We set the %convert variable to be it's old value, and the new line put together, we do this to get one long line of binary values
      %convert = %convert $+ $did($dname,6,%a).text
      
      ;## Increase %a to keep the loop running
      inc %a
      
    ;## End the loop
    }
    
    ;## Remove any possible spaces from the binary line
    %convert = $remove(%convert,$chr(32))
    
    ;## Set these two variables, this is to loop trough the value, and seperate them by every 8th digit (every character value)
    var %b = 1
    var %c = 1
    
    ;## Start a loop that will run until %b is a higher value thn the amount of digits in the line
    while (%b <= $len(%convert)) {
      
      ;## if %c is higher then 8, we won't be combining the old value with the new one, as it marks a new character
      if (%c > 8) { %text = %text $mid(%convert,%b,1) | var %c = 2 | inc %b }
      
      ;## If %c is lover then or equals 8, it's still the same characters value, so we keep addign the numbers in one group
      if (%c <= 8) { %text = %text $+ $mid(%convert,%b,1) | inc %c | inc %b }
      
    ;## There is no "inc %b" here, because we do that in both the if statements instead
    ;## End the loop to seperate the digits into groups of 8
    }
    
    ;## Set a variable for yet another loop
    var %d = 1
    
    ;## Run this loop until %d has a higher value then the amount of spaces in the variable %text (our list of binary groups)
    while (%d <= $numtok(%text,32)) {
      
      ;## Set our ever returning variable to the character that the %d'th group of binary values stands for
      %convert = $b2t($gettok(%text,%d,32))
      
      ;## Add the ASCII character, adn the digits to our list
      .did -a $dname 1 %convert $chr(9) $mid($gettok(%text,%d,32),1,1) $chr(9) $mid($gettok(%text,%d,32),2,1) $chr(9) $mid($gettok(%text,%d,32),3,1) $chr(9) $mid($gettok(%text,%d,32),4,1) $chr(9) $mid($gettok(%text,%d,32),5,1) $chr(9) $mid($gettok(%text,%d,32),6,1) $chr(9) $mid($gettok(%text,%d,32),7,1) $chr(9) $mid($gettok(%text,%d,32),8,1)
      
      ;## Increase %d to prevent infinitive loops
      inc %d
      
    ;## End the loop
    }
    
  ;## End the else statement
  }
  
;## End the on dialog event
}

;## When the dialog BinText is initiated, do the following
on *:DIALOG:BinText:init:0: {
  
  ;## This loads up the DLL mdx.dll (used for cool dialog features) and tell it what version of mIRC we're using
  .dll $shortfn($scriptdirmdx.dll) SetMircVersion $version
  
  ;## Tell MDX which dialog we want it to be used with
  .dll $shortfn($scriptdirmdx.dll) MarkDialog $dname
  
  ;## Here we tell it what we want to do, in our case, we want it to make a listview with a grid, and for that we need the Views.mdx file aswell
  .dll $shortfn($scriptdirmdx.dll) SetControlMDX $dname 1 ListView report single grid > $shortfn($scriptdirViews.mdx)
  
  ;## Now we tell the dialog how big we want each gridnet to be in width
  .did -i $dname 1 1 headerdims 30 40 35 35 35 35 35 35 27
  
  ;## Set a text into each of the gridnet sections, notice how a $chr(9) divides them all (this is  a tab)
  .did -i $dname 1 1 headertext ASCII $chr(9) 128 $chr(9) 64 $chr(9) 32 $chr(9) 16 $chr(9) 8 $chr(9) 4 $chr(9) 2 $chr(9) 1
  
;## Ends the on dialog event
}

;## Marks the start of the alis "b2t" (Binary To Text)
alias b2t {
  
  ;## If the length of the text is less then 8, we know it's not binary, so we halt everything
  if ($len($1-) < 8) { halt }
  
  ;## Prepare the variable %text for use, so it will be removed when we're done with it
  var %text
  
  ;## Set %text2 to be the binary value, but remove possible spaces
  var %text2 = $remove($1-,$chr(32))
  
  ;## If the length of the binary value divided by 8 returns decimals, we know it's not a proper binary value and halt it
  ;## (every binary value needs 8 digits, and dividing by 8 will tehrefore never return decimals)
  if ($numtok($calc($len(%text2) /8),46) > 1) { halt }
  
  ;## Start same function as earlier by dividing digits into groups of 8
  var %a = 1
  var %b = 1
  while (%a <= $len(%text2)) {
    if (%b > 8) { %text = %text $mid(%text2,%a,1) | var %b = 2 | inc %a }
    if (%b <= 8) { %text = %text $+ $mid(%text2,%a,1) | inc %b | inc %a }
  }
  ;## Finish dividing ito groups of 8
  
  ;## Prepare a variable for a while loop
  var %a = 1
  
  ;## Prepare a variable for the text/character the binary represents
  var %return
  
  ;## While loop set to run until %a has a higher walue then the amount of spaces in the variable containing our binary groups
  while (%a <= $numtok(%text,32)) {
    
    ;## Set %z to be a group of binary
    var %z = $gettok(%text,%a,32)
    
    ;## Make %c and set its value to 0
    var %c = 0
    
    ;## Make variable %d, it will be used for another loop to go trough all the digits in the binary group
    var %d = 1
    
    ;## The %power variable contains the binary parts we will be adding together if they're associated with the digit "1"
    var %power = 1 2 4 8 16 32 64 128
    
    ;## While %d is lower then or equals the amount of digits in %z:
    while (%d <= $len(%z)) {
      
      ;## Set %ef to be the value of digit%z from the right (binary works its way form the right towards the left) adn then multiply it by the associated value from the %power variable
      .set %ef $calc($left($right(%z,%d),1) * ($gettok(%power,%d,32)))
      
      ;## Increase %c by the value %ef returned
      inc %c %ef
      
      ;## Increase %d by 1 to keep the while loop running
      inc %d 1
      
    ;## End of the while loop
    }
    
    ;## If the value of %c is 32 (a space) don'¨t make the characters stand together, instead allow a space
    %return = $iif(%c == 32,%return $chr(%c),%return $+ $chr(%c))
    
    ;## Increase %a by 1 to go to the next group of binary if any
    inc %a 1
  }
  
  ;## Return the text/characters the binary stands for
  return %return
  
  ;## End of the Binary To Text alias
}

;## Start of the "t2b" alias (Text To Binary)
alias t2b {
  
  ;## If there is no text, stop everything
  if (!$1) { halt }
  
  ;## Prepare a variable for the information we're to return
  var %return
  
  ;## A variable ready for a while loop
  var %a = 1
  
  ;## Again, the %power variable contains the bianry varlues (notice they're the other way around this time)
  var %power = 128 64 32 16 8 4 2 1
  
  ;## While %a is lower then or equals the length of the text supplied:
  while (%a <= $len($1-)) {
    
    ;## Set %x to be character %a in the text
    var %x = $mid($1-,%a,1)
    
    ;## Set %y to be the ASCII value of the characters returned
    var %y = $asc(%x)
    
    ;## Now that we have the value it stands for, it's time to maek a nwe loop
    var %b = 1
    
    ;##
    var %num
    
    ;## while %b is lower then or equals 8, AND %y (the ascii value, you remember?) is higher then or equals 1 (in other words: not 0)
    while (%b <= 8) && (%y >= 1) {
      
      ;## if amount of commas found in "%y - (ascii value - the %b'th digit in %power)" is lower then or equals 1 (this means "there are none") AND %y is still over 0
      ;## AND the calculation leaves %y higher then or equal 0 (no negative numbers), you can add a 1 for that binary value, and decrease %y with the value you got form the calculation
      ;## also increase %b by 1 so we don't make an infinitive loop
      if ($numtok($calc(%y - $gettok(%power,%b,32)),46) <= 1) && (%y >= 1) && ($calc(%y - $gettok(%power,%b,32)) >= 0) { %num = %num $+ 1 | .dec %y $gettok(%power,%b,32) | inc %b }
      
      ;## If that is not the case, we add a "0" for that binary value, as well as increasing %b
      else { %num = %num $+ 0 | inc %b }
      
    ;## End of the loop
    }
    
    ;## If the length of the returned ASCII is lower then 8, we know the binary is wrong, so do the following:
    if ($len(%num) < 8) {
      
      ;## Set %c as the length of the binary value
      var %c = $len(%num)
      
      ;## While %c is lower then 8, loop trough this:
      while (%c < 8) {
        
        ;## Add a 0 to the end of the value
        %num = %num $+ 0
        
        ;## Increase %c
        inc %c
        
      ;## End while loop
      }
      ;## (This loop ensures no matter how many digits are missing, it will always be 8 when we return it)
      
    ;## End if statement for to few digits
    }
    
    ;## Make %return include its old value, and add the new 8digit binary value to it with a space seperating them
    %return = %return %num
    
    ;## Increase %a to go to the next character to return a binary for
    inc %a
    
  ;## End of the while loop
  }
  
  ;## Output the binarys found
  return %return
  
;## End of Text To Binary
}


;## !! Side Note !!
;## You may have noticed I removed spaces and made groups of 8 twice, this is because I wanted the script to be usable with the dialog, but you can also do stuff like
;##
;## //say $t2b(Hello)
;## That will make the binary come out instead of the text
;##
;## OR
;##
;## //say $b2t(0100100001100101011011000110110001101111)
;## That will return "Hello" instead fo the binary