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

Converting 2 bytes to Short in C#

If you reverse the values in the BitConverter call, you should get the expected result: int actualPort = BitConverter.ToUInt16(new byte[2] {(byte)port2 , (byte)port1 }, 0); On a little-endian architecture, the low order byte needs to be second in the array. And as lasseespeholt points out in the comments, you would need to reverse the order … Read more

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

Overflowing Short in java

What’s happening is that your number is wrapping around. More specifically, you’ve got a number 30,000, which in binary is: 0111 0101 0011 0000 When you add it to itself, and carry the 1’s, you get: 1110 1010 0110 0000 (Note: it’s very easy to multiply a number by 2 in binary — just shift … Read more

Unsigned short in Java

You can’t, really. Java doesn’t have any unsigned data types, except char. Admittedly you could use char – it’s a 16-bit unsigned type – but that would be horrible in my view, as char is clearly meant to be for text: when code uses char, I expect it to be using it for UTF-16 code … 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);