0

Suppose i have the number 520 that is mapped in two bytes that gives me the number: 1000001000 and i want to convert this number (520) to two other numbers, these numbers should be: 2 and 8 because 00000010 will give me 2 and 00001000 will give me 8. how can i do this with java?

tiagomac
  • 461
  • 1
  • 7
  • 18
  • 2
    The answers to http://stackoverflow.com/questions/1936857/convert-integer-into-byte-array-java answer your question, and then some. – NPE Jan 02 '15 at 17:03
  • JLRishe's answer is technically correct, but the variable names are awful: Conventional names would be `leastSignificantByte` or `lsb` instead of `oneNumber`, and `mostSignificantByte` or `msb` instead of `otherNumber`. Conventional names, conventional style, conventional design patterns, etc. are like grease on the rails of your career path: Obeying convention makes it much more likely that other developers will enjoy working with you and with your code. – Solomon Slow Jan 02 '15 at 17:24

1 Answers1

4

Like this:

int theNumber = 520;
byte oneNumber   = (byte)theNumber;
byte otherNumber = (byte)(theNumber >> 8);
JLRishe
  • 99,490
  • 19
  • 131
  • 169