Created
November 9, 2017 13:44
-
-
Save ssaurel/f8818aa83af6786b9b6505096b7dce1b to your computer and use it in GitHub Desktop.
Utils method for the NFC Reader tutorial on the SSaurel's Channel
This file contains 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
private String toHex(byte[] bytes) { | |
StringBuilder sb = new StringBuilder(); | |
for (int i = bytes.length - 1; i >= 0; --i) { | |
int b = bytes[i] & 0xff; | |
if (b < 0x10) | |
sb.append('0'); | |
sb.append(Integer.toHexString(b)); | |
if (i > 0) { | |
sb.append(" "); | |
} | |
} | |
return sb.toString(); | |
} | |
private String toReversedHex(byte[] bytes) { | |
StringBuilder sb = new StringBuilder(); | |
for (int i = 0; i < bytes.length; ++i) { | |
if (i > 0) { | |
sb.append(" "); | |
} | |
int b = bytes[i] & 0xff; | |
if (b < 0x10) | |
sb.append('0'); | |
sb.append(Integer.toHexString(b)); | |
} | |
return sb.toString(); | |
} | |
private long toDec(byte[] bytes) { | |
long result = 0; | |
long factor = 1; | |
for (int i = 0; i < bytes.length; ++i) { | |
long value = bytes[i] & 0xffl; | |
result += value * factor; | |
factor *= 256l; | |
} | |
return result; | |
} | |
private long toReversedDec(byte[] bytes) { | |
long result = 0; | |
long factor = 1; | |
for (int i = bytes.length - 1; i >= 0; --i) { | |
long value = bytes[i] & 0xffl; | |
result += value * factor; | |
factor *= 256l; | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment