6 Replies - 866 Views - Last Post: 28 July 2013 - 12:49 AM Rate Topic: -----

#1 EnvelopeSpoon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-July 13

Text to Binary to Array

Posted 27 July 2013 - 01:14 AM

Hey guys, so i'm trying to convert an array into a series of base 10 digits then store each of those digits into its binary equivalent. The ultimate goal is to xor this for a crypto program. For example I would start with a message "Hello World" then I need that to become "1101000 1100101 1101100 1101100 1101111 100000 1110111 1101111 1110010 1101100 1100100"

What I'm currently doing is...
i = 0
x = Array.new(Array.length)
text = "hello world"
text.each_byte {|c|
x[i] = c.to_s(2)
i+=1}



Which would be fine except I would like to populate the array with only one element instead of several for memory reasons. For example he would be [11010001100101], but under the above method I get [1101000, 1100101]. In addition to that I need to pad each character's binary representation to 8 digits which can be easily done by adding an if statement in that code snippet, but the above snippet doesn't give me what I want. Any ideas about where I could look. For reference I'm using JRuby 1.7.4.

Is This A Good Question/Topic? 0
  • +

Replies To: Text to Binary to Array

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2090
  • View blog
  • Posts: 3,185
  • Joined: 21-June 11

Re: Text to Binary to Array

Posted 27 July 2013 - 05:05 AM

If you only want one string, then just create one and append to that in the loop. To pad the strings with 0s, you can use rjust:

bit_string = ""
text.each_byte do |c|
  bit_string << c.to_s(2).rjust(8, "0")
end



And if you need that one string to be inside an array (though I can't imagine why you'd need that), you can just return [ bit_string ] from that.

That said, I don't really understand why you're trying to create that string at all. If you want to xor the bytes of the string with some other bytes, why not just do that? Why convert them to strings before you do that? There isn't even a built-in xor operator for strings.
Was This Post Helpful? 2
  • +
  • -

#3 EnvelopeSpoon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-July 13

Re: Text to Binary to Array

Posted 27 July 2013 - 01:49 PM

I was under the impression that I would have to go through this roundabout process in order to xor the bits. Is it possible to just xor the decimal values, that doesn't make sense to me, or perhaps I'm misunderstanding.

Generally speaking if there is a way to completely avoid strings I would love that, but as far as my question goes your answer has been extremely helpful.
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2090
  • View blog
  • Posts: 3,185
  • Joined: 21-June 11

Re: Text to Binary to Array

Posted 27 July 2013 - 04:07 PM

To xor too numbers, you can just write number1 ^ number2 - ^ being the xor operator. To xor two strings, you'd need to implement the xor logic yourself.
Was This Post Helpful? 1
  • +
  • -

#5 EnvelopeSpoon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-July 13

Re: Text to Binary to Array

Posted 27 July 2013 - 11:20 PM

So I assumed you had to have a number in base 2 to xor it. Meanwhile base 10 is totally xorable. Thank you for your time, I have no idea why I never checked that.
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2090
  • View blog
  • Posts: 3,185
  • Joined: 21-June 11

Re: Text to Binary to Array

Posted 27 July 2013 - 11:28 PM

You shouldn't think of integers as base 10 numbers. They're just numbers. The base only enters into it when you convert your numbers into strings.

Note that when you have a string like "10110101", that's not a sequence of 1s and 0s in memory. I mean it is, but it's not the sequence that you see in the string. Rather for each one or zero in the string, there will be the byte 48 or 49 in memory. And since you don't actually want to xor those, xoring bit strings involves a lot more operations than just xoring numbers, which are already stored as the correct sequence of bits in memory (modulo Fixnum tagging in MRI).

This post has been edited by sepp2k: 27 July 2013 - 11:32 PM

Was This Post Helpful? 2
  • +
  • -

#7 EnvelopeSpoon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-July 13

Re: Text to Binary to Array

Posted 28 July 2013 - 12:49 AM

That's interesting, I always thought that a number would have to be in binary to be xored. But when you bring it up, it makes much more sense for a number's bit encoding be "present" despite using the decimal case. It's actually going to make this project substantially easier, thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1