each_byte

A Ruby script to remove binary (garbage) characters from a text file alvin June 9, 2009 - 12:03pm

Problem: You have a file that should be a plain text file, but for some reason it has a bunch of non-printable binary characters (also known as garbage characters) in it, and you'd like a Ruby script that can create a clean version of the file.

Solution: I've demonstrated how to do this in another blog post by using the Unix tr command, but in case you'd like a Ruby script to clean up a file like this, I thought I'd write up a quick program and share it here.

Ruby - How to convert characters to ASCII decimal (byte) values

Problem: You have a character, or a string of characters, and you want to use a Ruby script to convert each character to its ASCII decimal (byte) value.

Solution

I just ran into this problem while working on a script to remove binary/garbage characters from a Unix text file. In short, all you have to do to convert a character in Ruby to its equivalent decimal ASCII code is use the ? operator in front of it, like this:

How to loop through each byte in a Ruby String

Problem: You need to loop through each character in a Ruby String, and get the byte value of the character as you iterate through the characters in the String.

Solution: Just use the each_byte method of the String class to loop through each byte in your String, like this:

a = 'hello, world'
a.each_byte { |c|
  puts c
}

This results in the following output: