Text to Binary

Hi! I’m new to the forums. I’m also a bit new to Ruby. I already know
most of the basics.

At the moment, I’m wondering how to convert text to binary in Ruby. As
in, encoding text into binary as shown here:
http://home3.paulschou.net/tools/xlate/

At one point, I would like to know how to convert it into ternary as
well. That may not be possible, but I would like to try.

Thanks!

On Thu, Jul 22, 2010 at 1:12 AM, Umm W.
[email protected] wrote:

Hi! I’m new to the forums. I’m also a bit new to Ruby. I already know
most of the basics.

At the moment, I’m wondering how to convert text to binary in Ruby. As
in, encoding text into binary as shown here:
http://home3.paulschou.net/tools/xlate/

“some text”.unpack(‘b*’)

“some text”.unpack(‘b*’)
Alright, awesome. What about ternary (0,1,2)?

See String#unpack http://ruby-doc.org/core/classes/String.html#M000760
It may be of help

Alright, new question. How do you take each individual character and use
it in a certain way?

For example, if I were to have the string “The cake is a lie.”, and I
wanted to pick out the first letter “T”, or the period, or any of the
other characters in that string, and identify it as I choose, how would
I do this?

See String#[], String#split, String#match and a much more on String
http://ruby-doc.org/core/classes/String.html

Ricardo Panaggio wrote:

See String#[], String#split, String#match and a much more on String
http://ruby-doc.org/core/classes/String.html

No, what I mean is, I’m trying to translate text to ternary. So, I want
to check EACH AND EVERY individual character and, depending on ASCII
values, turn the characters into the number in ternary that represents
that ASCII value. So, for example, in hex, an ! mark is 21. In decimal,
that’s 33. In ternary, that’s 001020. I would keep going on in this
manner and translate every single character into its respective ASCII
number (in ternary).

You still with me here? XD

each_char, to_i, to_s, [], maybe less, maybe more, should do the job.

You should try to read String, Numeric, Integer, … docs. It would help
a lot

On Thu, Jul 22, 2010 at 6:03 AM, Umm W.
[email protected] wrote:

No, what I mean is, I’m trying to translate text to ternary. So, I want
to check EACH AND EVERY individual character and, depending on ASCII
values, turn the characters into the number in ternary that represents
that ASCII value. So, for example, in hex, an ! mark is 21. In decimal,
that’s 33. In ternary, that’s 001020. I would keep going on in this
manner and translate every single character into its respective ASCII
number (in ternary).

To avoid solving people’s homework, only hints are usually given.
String#unpack is the way. Hint: look at the C and c format specifiers.
Another hint, Array#map.
If you share some code that shows that you’ve tried to solve it, it
makes it easier for others to take the time and help.

HTH,
Ammar

Ammar A. wrote:

On Thu, Jul 22, 2010 at 6:03 AM, Umm W.
[email protected] wrote:

No, what I mean is, I’m trying to translate text to ternary. So, I want
to check EACH AND EVERY individual character and, depending on ASCII
values, turn the characters into the number in ternary that represents
that ASCII value. So, for example, in hex, an ! mark is 21. In decimal,
that’s 33. In ternary, that’s 001020. I would keep going on in this
manner and translate every single character into its respective ASCII
number (in ternary).

To avoid solving people’s homework, only hints are usually given.
String#unpack is the way. Hint: look at the C and c format specifiers.
Another hint, Array#map.
If you share some code that shows that you’ve tried to solve it, it
makes it easier for others to take the time and help.

HTH,
Ammar

Alright, I think I’m almost there. I’ve got this:

test = “Hello World!”
test2 = test.unpack(‘c*’)
test3 = test2.collect {|x| x.to_s(3) + ’ '}
puts test3.to_s

However, I want each number to have 6 digits (6 trits in a tryte). How
would I do that? I want it to, if it didn’t already have 6 digits in it,
add the appropriate number of zeros in the beginning of it. Anyone got
any suggestions here?

On Fri, Jul 23, 2010 at 12:49 AM, Umm W.
[email protected] wrote:

any suggestions here?
Using the % method of the String class you can pad the value with zeros:

‘%06d’ % 123 #=> 000123

Instead of adding a space to each value inside collect, it is cleaner
to do it only on output, using Array#join.

test = “Hello World!”

decimal = test.unpack(‘c*’)
ternary = decimal.collect {|x| x.to_s(3)}

puts ternary.map {|v| ‘%06d’ % v }.join(’ ');

Note that the decimal format specifier ‘d’ is used in ‘%06d’, even
though the value is a String and in ternary. This works because the
string is automatically converted to an Integer, and ternary numbers
only consist of characters that can pass as decimal numbers. This is
OK here since it is for formatting purposes only.

GL,
Ammar

test = “Hello World!”
test2 = test.unpack(‘c*’)
test3 = test2.collect {|x| x.to_s(3).rjust(6,‘0’) + ’ '}
puts test3.to_s

use rjust
http://ruby-doc.org/core/classes/String.html#M000814

On Jul 22, 3:49 pm, Umm W. [email protected]in.invalid wrote:

However, I want each number to have 6 digits (6 trits in a tryte). How
would I do that? I want it to, if it didn’t already have 6 digits in it,
add the appropriate number of zeros in the beginning of it. Anyone got
any suggestions here?

numbers = [12, 120131, 0]
=> [12, 120131, 0]

numbers.map{ |n| “%06d” % n }
=> [“000012”, “120131”, “000000”]

See also http://ruby-doc.org/ruby-1.9/classes/Kernel.html#M002627

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs