Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Encode File Into Binary Ruby
05-15-2010, 10:41 PM
Post: #1
Encode File Into Binary Ruby
This code lets you encode a file into binary format using ones and zeros. You can also convert it back to plain text.

Code:
=begin
    This program encodes a file to binary or decodes it to plain text
    coded by codecaine aka Jerome Scott II
=end

#converts each text character to an 8 bit binary string and returns the string
def text2bin(text)
  pad = '' #pads an binary string with 0's
  size = 0 #stores the size of the binary string
  start_str = 0 #start point of the text string
  start_str.freeze #make start_str constant (This is optional)
  bin_bitsize = 8 #holds the size of the binary string
  bin_bitsize.freeze #mae bin_bitsize constant (This is optional)
  bin_str = '' #holds all the binary values of the text
  tempstr = '' #temporary old an 8 bit binary string to be appended

  start_str.upto(text.length - 1) do |i| #loop through the text giving to the function
    size = text[i].to_s(2).length #get the size of the binary string
    tempstr = text[i].to_i().to_s(2) #convert a character to a binary string

    if size < bin_bitsize #if the binary string is less then 8 bits pad the binary string
      pad = '0' * (bin_bitsize - size) #pad the binary string with 0's to equal 8 characters
      bin_str.concat(pad.concat(tempstr)) #append the 0's at the beginning of the binary string
      end
end

return bin_str #return the text as a binary string
end

def bin2text(bin) #converts 8 bit binary text to plain text
  tempstr = '' #temporary string to hold 8 bit binary string
  plain_text = '' #holds the plain text data converted from 8 bit binary string
  start_str = 0 #holds the index for the beginning of a string
  start_str.freeze #make start_str a constant (This is optional)
  start_str.upto(bin.length - 1) do |i| #loop through the binary string
    tempstr.concat(bin[i].chr) #append binary text to the temporary string
    if (i+1) % 8 == 0 #if the binary string reaches 8 bits convert it to plain text
      plain_text.concat(tempstr.to_i(2).chr) #converts the binary string to an character and append it to plain text
      tempstr = '' #empty text from tempstr
    end
  end
  return plain_text #return the binary string in plain text
end

def usage #Instructions on how to use this program
puts 'Usage:'
puts '    -e encodes to binary file -d decodes to binary file'
puts 'Example:'
puts '    ruby bin_encoder.rb -e plain_textfile.txt encoded_file.txt'
puts '    ruby bin_encoder.rb -d encoded_file.txt plain_textfile.txt'
end

encode_file = '' #hold encode file name
decode_file = '' #holde decode file name
ENCODE = '-e' #encode value constant
DECODE = '-d' #decode value constant
MIN_ARGUMENTS = 3 #holds minute amount of arguments required
begin
if ARGV.length >= MIN_ARGUMENTS #if the arguments equal or greater than minium arguments continue else execute help method
    OPTION = ARGV[0] #constant hold that -e for encode or -d for decoding
    INPUT_FILE = ARGV[1] #constant for input file name
    OUTPUT_FILE = ARGV[2]  #constant for output file name
      if OPTION == ENCODE #if user choose encode option encode the file to binary
          fr = File.open(INPUT_FILE,'r') #open plain text file for reading
          encode_file = text2bin(fr.read) #read plain text file
          fr.close #close the file
          fw = File.open(OUTPUT_FILE,'w') #open file for output
          fw.write(encode_file) #encode the file read into binary
          fw.close #close file
          puts "file encoded into binary format as #{OUTPUT_FILE}" #show that the file has been encoded
    elsif OPTION == DECODE #if user choose decode option decode the file to plain text
        fr = File.open(INPUT_FILE,'r') #open file for reading
        decode_file = bin2text(fr.read) #decode binary file to plain text
          fr.close #close file
          fw = File.open(OUTPUT_FILE,'w') #open file for writing
          fw.write(decode_file) #write decoded text to file
          fw.close #close file
          puts "file encoded into binary format as #{OUTPUT_FILE}" #show that the file has been decoded
      end
else
    usage #usage method
end
rescue Exception => e#if there were any error show the error message and display usage
    puts "Error: #{e}"
    usage
end


Attached File(s)
.zip  file_bin_encoder.rb.zip (Size: 1.88 KB / Downloads: 2)
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: