Text to Binary

U

Umm Whyshouldisay

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!
 
U

Umm Whyshouldisay

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?
 
U

Umm Whyshouldisay

Ricardo said:
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
 
R

Ricardo Panaggio

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

Ricardo said:
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
 
A

Ammar Ali

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
 
U

Umm Whyshouldisay

Ammar said:
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?
 
A

Ammar Ali

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?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top