• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java convert a String to binary, question  RSS feed

 
Kevin Olome
Ranch Hand
Posts: 44
1
Android Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I have found the following algorithm to convert a String into binary numbers.
I need your help to understand it.



Why is this working? Why do I need to check val which is a byte number for example C for 64 against 128?

I got this:
64 is:
0 1 0 0 0 0 0 0
128 is:
1 0 0 0 0 0 0 0

We check always the first position if its 0. If it is then it will print 0. If its not then it is 1.
Then we shift 1 bit.
Why does this convert a number to a binary sequence?
Somehow I don't get this part at all.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got this:

Your print out doesn't match what the code shows. For example the code prints:
byte[] bytes =

Can you copy the output from the code and paste it here so we can see what it does?

Note: The code does not do what you'd expect because the char values are not converted to their int values.
 
Kevin Olome
Ranch Hand
Posts: 44
1
Android Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, thanks for your answer:

Input is : "C"
Output is: 01000011
which is 67 ? Which is in Ascii "C" Or am I wrong?

For the sake of completeness (complete code):

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is the output from this statement?
System.err.println("byte[] bytes = ");
It doesn't look like the method is being executed.

How is the code that you posted related to the output that is shown?


 
Kevin Olome
Ranch Hand
Posts: 44
1
Android Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Console output

Standard Output Stream:

Message: C

answer

byte[] bytes =
67
BinaryMessage: 01000011
Failure
Found: a
Expected: 0
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I was confused about what the code's input would be. I was expecting the numeric input: "67" would be converted to binary: 1000011 vs the character "C".

After all this, do you still have questions?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Olome wrote:We check always the first position if its 0. If it is then it will print 0. If its not then it is 1.
Then we shift 1 bit.
Why does this convert a number to a binary sequence?

Because it always checks the same bit position.

Take 'C' (67), which is
01000011
so the first time you '&' it with 128, which is (as you already worked out)
10000000
you get
00000000
which equals 0, so you add '0' to the string.

Then you shift left, giving you (effectively)
10000110
and you '&' that with 128, which gives you
10000000
which is NOT 0, so you add '1' to the string.

Then you shift left again, giving you (again effectively)
00001100
and you '&' that with 128, which gives you
00000000
which equals 0, so you add '0' to the string.
....
usw, usw.

Each time you are only checking the state of ONE bit - the one that you've moved into the first position.

BTW, when you're trying to figure out stuff like this, it's usually good to have LOTS of paper available

HIH

Winston
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the code would be more readable if the AND mask was coded to show where the bits are that it is testing:
0x80 or 0b10000000 instead of 128
Then the basic AND logic:
1 AND 1 is 1
1 AND 0 is 0
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:I think the code would be more readable if the AND mask was coded to show where the bits are that it is testing:
0x80 or 0b10000000 instead of 128...

Very good point; and 0b10000000 would be my choice.

Winston
 
Kevin Olome
Ranch Hand
Posts: 44
1
Android Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answers.
I think I got it now.

For my own comprehension I created a PDF for each step.
Maybe it will help other beginners like me

PDF String to Binary String
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Olome wrote:For my own comprehension I created a PDF for each step.
Maybe it will help other beginners like me

Very nice.

One tiny point: You night want to translate "Wird geshiftet um 1 Bit" into English.

But otherwise: Well done! Have a cow.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!