• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to concatenate Strings of binary numbers together?  RSS feed

 
Greenhorn
Posts: 17
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so what I a String array of binary numbers



and what I want to do is simply run through a loop (Which I already made ) that will randomly just put this strings together.
So if the loop runs once it will return something similar to



The thing is that I don't want to return such a number as an String, I want it to be returned as a long.

This was one of my recent attempts



But the output is not the desired:




What can I do ? I want them the values returned to be of the long data type, but I don't want the binary numbers to be added together. I just simply want them to be placed one next to each other at random patterns
 
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not concatenate them as Strings and convert them to longs (using base 2) at the end? And remember to print in base 2 as well so you still get the base 2 output.
 
Daniel Vargas
Greenhorn
Posts: 17
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because Long.parseLong() doesn't support parsing String arrays
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You wouldn't have an array. You'd have a single string that you created by looping through the array of strings and concatenating them. Or in Java 8 by optionally calling the join method.
 
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Daniel Vargas
Greenhorn
Posts: 17
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey, could you explain the loop ?
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:

You were asking about how to create a long of random bits. A long is 8 bytes so this loops for 8 times and each time it shifts chrom to the left by one byte and then adds a new random byte on the right. I'm not sure how your going to use it but I was just trying to answer the question you asked.
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You really are doing two things here:

1) contatenating a bunch of strings together
2) Converting a string of binary digits to a long.

These are 100% independent of each other. You can do either one without regard to the other. So, is your concatenation working? Print it out to prove it to yourself.

Regardless of whether or not that works, can you convert a binary string to a long? Hard code some value like "1000" and pass it to your conversion method, and see if it works. Try a few other "known" values and be SURE it works.

Once you have those two separate methods working, the rest is easy. Take the output from one and pass it as the input to the other.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!