BitcoinUtil { public static long getVarInt(byte[] varInt) { long result=0; if (varInt.length==0) { return result; } int unsignedByte=varInt[0] & 0xFF; if (unsignedByte<0xFD) { return unsignedByte; } int intSize=0; if (unsignedByte==0xFD) { intSize=3; } else if (unsignedByte==0xFE) { intSize=5; } else if (unsignedByte==0XFF) { intSize=9; } byte[] rawDataInt=reverseByteArray(Arrays.copyOfRange(varInt, 1, intSize)); ByteBuffer byteBuffer = ByteBuffer.wrap(rawDataInt); if (intSize==3) { result=byteBuffer.getShort(); } else if (intSize==5) { result=byteBuffer.getInt(); } else if (intSize==9) { result=byteBuffer.getLong(); } return result; } private  BitcoinUtil(); static long convertSignedIntToUnsigned(int signedInt); static byte[] convertIntToByteArray(int intToConvert); static byte[] convertLongToByteArray(long longToConvert); static byte[] convertBigIntegerToByteArray(BigInteger bigIntegerToConvert,int exactArraySize); static byte[] convertVarIntByteBufferToByteArray(ByteBuffer byteBuffer); static long convertVarIntByteBufferToLong(ByteBuffer byteBuffer); static BigInteger getVarIntBI(byte[] varInt); static long getVarInt(byte[] varInt); static byte getVarIntSize(byte firstByteVarInt); static long getSize(byte[] byteSize); static byte[] reverseByteArray(byte[] inputByteArray); static byte[] convertHexStringToByteArray(String hexString); static String convertByteArrayToHexString(byte[] byteArray); static Date convertIntToDate(int dateInt); static boolean compareMagics(byte[] magic1,byte[] magic2); static byte[] getTransactionHash(BitcoinTransaction transaction); static byte[] getTransactionHashSegwit(BitcoinTransaction transaction);  }
@Test public void getVarIntByte() { byte[] originalVarInt = new byte[] {0x01}; long convertedVarInt = BitcoinUtil.getVarInt(originalVarInt); assertEquals( 1L,convertedVarInt,"varInt {0x01} must be 1 as long"); }
@Test public void getVarIntWord() { byte[] originalVarInt = new byte[] {(byte)0xFD,0x01,0x00}; long convertedVarInt = BitcoinUtil.getVarInt(originalVarInt); assertEquals( 1L, convertedVarInt,"varInt {0xFD,0x01,0x00} must be 1 as long"); }
@Test public void getVarIntDWord() { byte[] originalVarInt = new byte[] {(byte)0xFE,0x01,0x00,0x00,0x00}; long convertedVarInt = BitcoinUtil.getVarInt(originalVarInt); assertEquals( 1L, convertedVarInt,"varInt {0xFE,0x01,0x00,0x00,0x00} must be 1 as long"); }
@Test public void getVarIntQWord() { byte[] originalVarInt = new byte[] {(byte)0xFF,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; long convertedVarInt = BitcoinUtil.getVarInt(originalVarInt); assertEquals( 1L, convertedVarInt,"varInt {0xFF,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00} must be 1 as long"); }