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!
···
--
Posted via http://www.ruby-forum.com/.
"some text".unpack('b*')
Alright, awesome. What about ternary (0,1,2)?
···
--
Posted via http://www.ruby-forum.com/\.
See String#unpack class String - RDoc Documentation
It may be of help
···
On Wed, Jul 21, 2010 at 19:57, Umm Whyshouldisay <kipthemudkip@yahoo.com> wrote:
"some text".unpack('b*')
Alright, awesome. What about ternary (0,1,2)?
--
Posted via http://www.ruby-forum.com/\.
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?
···
--
Posted via http://www.ruby-forum.com/.
See String#, String#split, String#match and a much more on String
http://ruby-doc.org/core/classes/String.html
···
On Wed, Jul 21, 2010 at 21:50, Umm Whyshouldisay <kipthemudkip@yahoo.com> wrote:
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?
--
Posted via http://www.ruby-forum.com/\.
Ricardo Panaggio wrote:
See String#, String#split, String#match and a much more on String
class String - RDoc Documentation
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
···
--
Posted via http://www.ruby-forum.com/\.
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 00:03, Umm Whyshouldisay <kipthemudkip@yahoo.com> wrote:
Ricardo Panaggio wrote:
See String#, String#split, String#match and a much more on String
class String - RDoc Documentation
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
--
Posted via http://www.ruby-forum.com/\.
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
···
On Thu, Jul 22, 2010 at 6:03 AM, Umm Whyshouldisay <kipthemudkip@yahoo.com> 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).
Ammar Ali 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 Thu, Jul 22, 2010 at 6:03 AM, Umm Whyshouldisay > <kipthemudkip@yahoo.com> wrote:
--
Posted via http://www.ruby-forum.com/\.
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
···
On Fri, Jul 23, 2010 at 12:49 AM, Umm Whyshouldisay <kipthemudkip@yahoo.com> wrote:
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?
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
···
On Jul 22, 3:49 pm, Umm Whyshouldisay <kipthemud...@yahoo.com> wrote:
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
···
--
Posted via http://www.ruby-forum.com/.