Last active
August 29, 2015 14:20
-
-
Save brandonto/12c71a6208bfa5273180 to your computer and use it in GitHub Desktop.
Encodes an int as 2 bytes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For Tianming | |
| // Encodes an int as 2 bytes | |
| public class TestByteIntConversion | |
| { | |
| public static void main(String[] args) | |
| { | |
| int blockNumber = 257; | |
| System.out.println("Block number = " + blockNumber); | |
| byte msb_blockNumber = (byte)(blockNumber / 256); | |
| byte lsb_blockNumber = (byte)(blockNumber % 256); | |
| System.out.println("MSB = " + msb_blockNumber); | |
| System.out.println("LSB = " + lsb_blockNumber); | |
| int parsedBlockNumber = (int)(msb_blockNumber & 0xFF)*256 + (int)(lsb_blockNumber & 0xFF); | |
| System.out.println("Parsed block number = " + parsedBlockNumber); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment