pf » Converting an unsigned byte array to an integer
Converting an unsigned byte array to an integer
I found myself today needing to deal with unsigned integers, and shorts in java. In Java there is no unsigned keyword like in C, or other languages. All primitives are signed (meaning they can hold negative values).
So I came up with two functions for converting the unsigned byte arrays into numbers I can use (thanks to Matt Finn, here at ActivSoftware for some pointers). I am posting these to help others, and to see if anyone knows of a more efficient or easier way to do this:
/**
* Converts a 4 byte array of unsigned bytes to an long
* @param b an array of 4 unsigned bytes
* @return a long representing the unsigned int
*/
public static final long unsignedIntToLong(byte[] b)
{
long l = 0;
l |= b[0] & 0xFF;
l <<= 8;
l |= b[1] & 0xFF;
l <<= 8;
l |= b[2] & 0xFF;
l <<= 8;
l |= b[3] & 0xFF;
return l;
}
/**
* Converts a two byte array to an integer
* @param b a byte array of length 2
* @return an int representing the unsigned short
*/
public static final int unsignedShortToInt(byte[] b)
{
int i = 0;
i |= b[0] & 0xFF;
i <<= 8;
i |= b[1] & 0xFF;
return i;
}
You will notice that the functions actually return a long for unsinged integers, and an integer for shorts, this is because we can't store a large unsigned int in a java int. You may also be interested to know that byte's, short's, and int's are all actually stored using 4 bytes by the jvm (so they can perform 32 bit operations).
Related Entries
- Bea JRockit 5 JVM Released - February 22, 2005
- Java 1.5 for FreeBSD Patchset Released - February 18, 2005
- Robi Sen on Hung Servers, java.lang.OutOfMemory errors and Tuning CF JVM - November 19, 2004
- Visualize Garbage Collection - June 8, 2004
- ColdFusion Garbage - June 3, 2004
It provides methods for dealing with binary data, including two methods very similar to the two you posted. It uses my ByteBlock class, but you could easily change it (using IDEA's structural search & replace for example) to use byte arrays.
public static final int unsignedShortToInt(byte[] b) { return (b[0] << 8) | b[1]; } </pre>
return ((b[0] & 0xFF) << 24) | ((b[1] & 0xFF) << 16) | ((b[2] & 0xFF) << 8) | b[3] & 0xFF;
You can probably can do some bytecode tweaking to make it bit shorter but not necessary faster.
int i = ...; long l = i < 0 ? 0x0000000100000000L + i : i;
return ((long)(b[0] & 0xFF) << 24) | ((long)(b[1] & 0xFF) << 16) | ((long)(b[2] & 0xFF) << 8) | (long)b[3] & 0xFF;
int i = new BigInteger(1,bytes).intValue();
which takes any length array. Especially handy for doing IP filtering for both IPv4 and IPv6
i wasnt '&' it with FF. my code worked because i never tested with values which would give me a -ve int. but looking at this i realized that it had a bug and it sometimes might not work without the '& 0xFF'
int i = ...; long res = (i & 0x7FFFFFFF) + ((long)(i>>>31)<<31);
Hope someone (other than me!) finds that helpful
Good luck !
???????????. ???? ??? ?????????? ??? ?? ????? ????????, ??????? ?????? ?? ??????? ???????? ???????.
??????? ?????!
?? ?????? ?????????? ????????? ????????? ? ?????????? ?? ???????? ? ??????? 30 ??????.
?? ???????? 0.01 $ ?? ?????? ???????? ????????,
? ??? 0.01 $ ??? ?????? ???????? ???????? ????? ?????????.
????? ?? ????? ????? ????? 10$ ?? ?????? ????????? ?? ? Alertpay ? ????? ?? ????????(???????? Visa classic(????? ????? ?????? ? ????? ????? ? ????? ??? "???????")), ??? ????????? ? E-Gold ??????, ? ????? ?? ??????? webmoney
?????? ?????? ??? ?????????????? 20 ?????? ? ???? = 0.20 $ ?10 ????? ????????? ??????? 20 ?????????? ? ???? = 2.00 $ ???? ?????????? ????? = 2.20 $ ???? ???????????? ????? = 15.4 $ ???? ??????????? ??????????? ????? = 66.00 $ ????? ?? 5-10 ????? ? ????!
????????? ?????????? ?? ??????????? ?? ?????? ??????? ?? ???? ?????? http://depositfiles.com/files/4230145
?? ? ???? ? ??? ???????? ????????, ??? ?????? ?? ?? ? ????? ????? ? ????? ???????? ??? ????? ?????? ?? ?????? (???? ??????????)
????????! ?????!
- CFSCRIPT Cheatsheet
- 3 New Image Effects for ColdFusion 8
- Googlebot to Submit Web Forms
- ColdFusion 8 Update 1 Fixes some Image Processing Quirks
- 10 Most Useful Image Functions in ColdFusion 8
- Speaking at NYC CFUG This Week
- Adobe AIR Tutorial for HTML / JavaScript Developers
- INFORMATION_SCHEMA Support in MySQL, PostgreSQL
RSS
add to del.icio.us
Pete Freitag is a software engineer, and web developer located in










