Skip to content

Instantly share code, notes, and snippets.

@dsaiztc
Created June 7, 2014 10:25
Show Gist options
  • Save dsaiztc/e722bbf2fc6136b03097 to your computer and use it in GitHub Desktop.
Save dsaiztc/e722bbf2fc6136b03097 to your computer and use it in GitHub Desktop.
Convert an unsigned integer to signed (keeping binary value).
/**
* Convert an unsigned integer to signed (keeping binary value)
*
* @param binary Integer whose binary value need to convert
* @return A byte with same binary than income integer
*/
private static byte fromBinaryToByte(int binary)
{
byte result = -1;
String s = Integer.toBinaryString(binary);
result = (byte) Integer.parseInt(s, 2);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment