2 bytes to short

Remember, you don’t have to tie yourself in knots with bit shifting if you’re not too familiar with the details. You can use a ByteBuffer to help you out: ByteBuffer bb = ByteBuffer.allocate(2); bb.order(ByteOrder.LITTLE_ENDIAN); bb.put(firstByte); bb.put(secondByte); short shortVal = bb.getShort(0); And vice versa, you can put a short, then pull out bytes. By the way, … Read more

When to use `short` over `int`?

(See Eric’s answer for more detailed explanation) Notes: Generally, int is set to the ‘natural size’ – the integer form that the hardware handles most efficiently When using short in an array or in arithmetic operations, the short integer is converted into int, and so this can introduce a hit on the speed in processing … Read more

Ternary operator behaviour inconsistency [duplicate]

C# language specification, version 5, section 6.1.9: An implicit constant expression conversion permits the following conversions: A constant-expression (ยง7.19) of type int can be converted to type sbyte, byte, short, ushort, uint, or ulong, provided the value of the constant-expression is within the range of the destination type. Your first example is a constant expression, … Read more

php: number only hash?

An MD5 or SHA1 hash in PHP returns a hexadecimal number, so all you need to do is convert bases. PHP has a function that can do this for you: $bignum = hexdec( md5(“test”) ); or $bignum = hexdec( sha1(“test”) ); PHP Manual for hexdec Since you want a limited size number, you could then … Read more

byte array to short array and back again in java

I also suggest you try ByteBuffer. byte[] bytes = {}; short[] shorts = new short[bytes.length/2]; // to turn bytes to shorts as either big endian or little endian. ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts); // to turn shorts back to bytes. byte[] bytes2 = new byte[shortsA.length * 2]; ByteBuffer.wrap(bytes2).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().put(shortsA);