Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Text to Binary Encoder Ruby
05-15-2010, 10:38 PM (This post was last modified: 08-06-2010 05:11 PM by codecaine.)
Post: #1
Text to Binary Encoder Ruby
This program converts plain text into 8 bit binary
PHP Code:
=begin
    This program converts plain text to binary 
and vice versa
    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 #stores the size of the binary string
  
start_str #start point of the text string
  
start_str.freeze #make start_str constant (This is optional)
  
bin_bitsize #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 += 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 #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) % == #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

user_input 
''
puts 'Enter in a string to convert to binary'
user_input gets #read string from user
puts 'Binary output:'
puts text2bin(user_input#display the string in 8 bit binary format
puts 'Binary string converted back to plain text:'
puts bin2text(text2bin(user_input)) #convert 8 bit binary back into plain text format 


Attached File(s)
.zip  bin_text_encoder.rb.zip (Size: 1.34 KB / Downloads: 0)
Visit this user's website Find all posts by this user
Quote this message in a reply
05-16-2010, 01:14 AM
Post: #2
RE: Text to Binary Encoder Ruby
Great example ! I don't get it till the end. Does it convert ASCII values or ???

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
05-16-2010, 01:33 AM
Post: #3
RE: Text to Binary Encoder Ruby
yea it takes the ordinal values of each chracters and convert it to 8 bit binary string. So each characters has a total of eight 1's and 0's when the number is converted and doesn't have 8 characters long, I pad the beginning of the binary string with 0's
Visit this user's website Find all posts by this user
Quote this message in a reply
05-16-2010, 01:37 AM
Post: #4
RE: Text to Binary Encoder Ruby
(05-16-2010 01:33 AM)codecaine Wrote:  yea it takes the ordinal values of each chracters and convert it to 8 bit binary string. So each characters has a total of eight 1's and 0's when the number is converted and doesn't have 8 characters long, I pad the beginning of the binary string with 0's
Yeah ordinal values like ord() function in Pascal. It returns ASCII value (0..255) of character that you entered.

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
05-16-2010, 01:47 AM
Post: #5
RE: Text to Binary Encoder Ruby
Yea in ruby if you only return 1 character from a string it returns the ord unless you add .chr at the end
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: