This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert from integer to array of bytes

 
Rob Chung
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to convert any integer into 4 bytes. The following code continues to give me ffffff80 in byte 3. Please help. Thanks in advance!
byte [] aa = { 0, 0, 0, 0 };
int bb = 128;
aa[0] = (byte) ((bb >>> 24) & 0xFF);
aa[1] = (byte) ((bb >>> 16) & 0xFF);
aa[2] = (byte) ((bb >>> 8) & 0xFF);
aa[3] = (byte) ((bb >>> 0) & 0xFF);
 
Rob Chung
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This actually works.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic