• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can you convert HEX to Binary w.o. lossing leading zeros?  RSS feed

 
Sam Moran
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can convert hex to bianry but I lose the leading zeros. I need the leading zeros to assist me in my calculations. I have a hex value of:
017B7020 coming in and binary out of:
1011110110111000000100000
I've lost the 4 zeros for the frist zero and in addition I have lost the 3 leading zeros for the one.
I have tried a mask and that has not helped. I need the full 32 bits from that and other hex values.
Is this possible? Thank you.
 
Neil Laurance
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure of an existing method that gives you the leading zeros, but something like the following should work:

Cheers, Neil
 
Sam Moran
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neil, thank you very mcuh!!
I don't understand the 0x80000000, I assume that this is for formatting? Is it the zeros after the 8 that tell it to print all zeros?
 
Neil Laurance
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
0x80000000 is a bit mask, with a single '1' in the most significant bit of the 4 bytes used to represent an integer, that is:
0x80000000 = 10000000 00000000 00000000 00000000 (base2)
The while loop performs a logical AND of the number and the bit mask. A non zero return value indicates there was a bit at this position in the integer number, so append a '1' to the StringBuffer. Otherwise append a '0'. Then, before repeating the loop, we shift the bit 1 place to the right, filling in on the left with zeros (the >>> shift operator). That is:
10000000 00000000 00000000 00000000 >>> 1 =
01000000 00000000 00000000 00000000
etc..
Keep repeating the loop, until the '1' bit is completely shifted off to the right, and the mask then has the value 0.
Hope this helps :roll: :roll:
[ February 14, 2003: Message edited by: Neil Laurance ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you doing calculations with a String object anyway? It seems to me it would be easier to simply convert the hex string to an int and do the calculations with the built-in operators. Usually hex and binary representations are only necessary for input and output. Internally, you can just use int or long to do all the calculations and bit manipulations.
Keep coding!
Layne
 
Sam Moran
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neil, thank you for the explanation. I don't use hex or binary every day.
Layne, I am working on an assignment that will be a MIPS disassembler. I need the leading zeros to calculate the first bits and determine the OP code. Crazy RIGHT?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!