001 package sysModel.classFile; 002 003 /** 004 * Type conversions. 005 * 006 * @author Mathias Ricken 007 */ 008 public class Types { 009 /** 010 * Convert a signed int into an unsigned integer. 011 * 012 * @param i signed int 013 * 014 * @return unsigned integer 015 */ 016 public static final long unsigned(int i) { 017 return i & 0xffffffff; 018 } 019 020 /** 021 * Convert a signed short into an unsigned short. 022 * 023 * @param s signed short 024 * 025 * @return unsigned short 026 */ 027 public static final int unsigned(short s) { 028 return s & 0xffff; 029 } 030 031 /** 032 * Convert a signed byte into an unsigned byte. 033 * 034 * @param b signed byte 035 * 036 * @return unsigned byte 037 */ 038 public static final int unsigned(byte b) { 039 return b & 0xff; 040 } 041 042 /** 043 * Construct a short from two bytes in a byte array. 044 * 045 * @param b byte array 046 * @param start index in byte array 047 * 048 * @return unsigned short 049 */ 050 public static final short ushortFromBytes(byte[] b, int start) { 051 return (short)(((b[start] & 0xff) << 8) | ((b[start + 1] & 0xff) << 0)); 052 } 053 054 /** 055 * Construct a short from two bytes in a byte array. 056 * 057 * @param b byte array 058 * @param start index in byte array 059 * 060 * @return short 061 */ 062 public static final short shortFromBytes(byte[] b, int start) { 063 return (short)((b[start] << 8) | ((b[start + 1] & 0xff) << 0)); 064 } 065 066 /** 067 * Construct an array of two individual bytes from a short. 068 * 069 * @param s short 070 * 071 * @return array of two bytes 072 */ 073 public static final byte[] bytesFromShort(short s) { 074 return new byte[]{(byte)(s >> 8), (byte)(s & 0xff)}; 075 } 076 077 /** 078 * Construct two individual bytes from a short and place them in the byte array at offset start. 079 * 080 * @param s short 081 * @param b byte array 082 * @param start offset 083 */ 084 public static final void bytesFromShort(short s, byte[] b, int start) { 085 b[start] = (byte)(s >> 8); 086 b[start + 1] = (byte)(s & 0xff); 087 } 088 089 /** 090 * Construct an int from four bytes in a byte array. 091 * 092 * @param b byte array 093 * @param start index in byte array 094 * 095 * @return unsigned int 096 */ 097 public static final int uintFromBytes(byte[] b, int start) { 098 return (int)(((b[start] & 0xff) << 24) | ((b[start + 1] & 0xff) << 16) | ((b[start + 2] & 0xff) << 8) | ((b[start + 3] & 0xff) << 0)); 099 } 100 101 /** 102 * Construct an int from four bytes in a byte array. 103 * 104 * @param b byte array 105 * @param start index in byte array 106 * 107 * @return signed int 108 */ 109 public static final int intFromBytes(byte[] b, int start) { 110 return (int)((b[start] << 24) | ((b[start + 1] & 0xff) << 16) | ((b[start + 2] & 0xff) << 8) | ((b[start + 3] & 0xff) << 0)); 111 } 112 113 /** 114 * Construct an array of four individual bytes from an int. 115 * 116 * @param i int 117 * 118 * @return array of four bytes 119 */ 120 public static final byte[] bytesFromInt(int i) { 121 return new byte[]{(byte)(i >> 24), (byte)((i >> 16) & 0xff), (byte)((i >> 8) & 0xff), (byte)(i & 0xff)}; 122 } 123 124 /** 125 * Construct four individual bytes from an int and place them in the byte array starting at offset start. 126 * 127 * @param i int 128 * @param b byte array 129 * @param start offset 130 */ 131 public static final void bytesFromInt(int i, byte[] b, int start) { 132 b[start] = (byte)(i >> 24); 133 b[start + 1] = (byte)((i >> 16) & 0xff); 134 b[start + 2] = (byte)((i >> 8) & 0xff); 135 b[start + 3] = (byte)(i & 0xff); 136 } 137 }