Java byte数组与byte,short,int,long,float,double,string 相互转换——小端法
今天没事儿,我写了一个二进制的转换工具,功能和Java.IO里面带的那个类似,但是那个是大段法,我这个是小端法。并且更加轻量级。适用于TCP通讯,文件写入写出。我这个更好理解。今天先把代码发上来。等有时间我写一个TCP发送协议的DEMO。
1 package com.guolaoshi.util; 2 3 import java.io.UnsupportedEncodingException; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 /** 8 * The utility of the byteArray 9 * 10 * @author guoqiwc 11 * 12 */ 13 public class ByteArray { 14 15 private List<Byte> _byteArray; 16 17 private int potision = 0; 18 19 public ByteArray() { 20 _byteArray = new ArrayList<>(); 21 } 22 23 /** 24 * return the NUMBER of the Byte in this ByteArray 25 * 26 * @return 27 */ 28 public int getLength() { 29 return _byteArray.size(); 30 } 31 32 /** 33 * return the ByteArray 34 * 35 * @return 36 */ 37 public Byte[] getByteArray() { 38 Byte[] tempArray = new Byte[this._byteArray.size()]; 39 for (int i = 0; i < this._byteArray.size(); i++) { 40 tempArray[i] = this._byteArray.get(i); 41 } 42 return tempArray; 43 44 } 45 46 /** 47 * print byteArray to Hex for testing 48 * 49 * @return 50 */ 51 public void toHexForTest() { 52 for (int i = 0; i < this._byteArray.size(); i++) { 53 if ((i) % 8 == 0) { 54 System.out.print(i + " ---> "); 55 } 56 if (this._byteArray.get(i) > 0) { 57 System.out.print(Integer.toHexString((byte) this._byteArray.get(i)).toUpperCase()); 58 System.out.print(" "); 59 } else if (this._byteArray.get(i) < 0) { 60 System.out.print(Integer.toHexString((byte) this._byteArray.get(i) + 256).toUpperCase()); 61 System.out.print(" "); 62 } else if (this._byteArray.get(i) == 0) { 63 System.out.print("00"); 64 System.out.print(" "); 65 } 66 if ((i + 1) % 4 == 0) { 67 System.out.print(" | "); 68 } 69 if ((i + 1) % 8 == 0) { 70 System.out.println(); 71 } 72 73 } 74 System.out.println(); 75 } 76 77 /** 78 * set the position where you want When the position is large than the 79 * number of the byteArray . AUTO SET ZERO AS THE EMPTY AMOUNT THE SIZE AND 80 * THE POSITION WHITCH YOU SET 81 * 82 * @param position 83 */ 84 public void setPosition(int position) { 85 if (position > this._byteArray.size()) { 86 for (int index = this._byteArray.size() - 1; index < position; index++) { 87 this._byteArray.add((byte) 0x00); 88 } 89 } 90 this.potision = position; 91 } 92 93 /** 94 * write byte into byteArray 95 * 96 * @param value 97 */ 98 private void write(Byte value, int position) { 99 if (position < this._byteArray.size()) { 100 this._byteArray.set(position, value); 101 } else { 102 this._byteArray.add(value); 103 } 104 } 105 106 /** 107 * return the next byte in this ByteArray 108 * 109 * @return 110 */ 111 public byte readByte() { 112 byte value = (byte) (this._byteArray.get(potision + 0) & 0xff); 113 this.potision += 1; 114 return value; 115 } 116 117 /** 118 * append the byte into this byteArray 119 * 120 * @return 121 */ 122 public void writeByte(byte value) { 123 this.write((byte) ((value >>> 0) & 0xff), this.potision); 124 this.potision += 1; 125 } 126 127 /** 128 * return the next short in this ByteArray 129 * 130 * @return 131 */ 132 public short readShort() { 133 short value = (short) (this._byteArray.get(potision + 0) & 0xff); 134 value += ((short) (this._byteArray.get(potision + 1) & 0xff) << 8); 135 this.potision += 2; 136 return value; 137 } 138 139 /** 140 * append the short into this byteArray 141 * 142 * @return 143 */ 144 public void writeShort(short value) { 145 this.write((byte) ((value >>> 0) & 0xff), this.potision); 146 this.write((byte) ((value >>> 8) & 0xff), this.potision + 1); 147 this.potision += 2; 148 } 149 150 /** 151 * return the next int in this ByteArray 152 * 153 * @return 154 */ 155 public int readInt() { 156 int value = (int) (this._byteArray.get(potision + 0) & 0xff); 157 value += ((int) (this._byteArray.get(potision + 1) & 0xff) << 8); 158 value += ((int) (this._byteArray.get(potision + 2) & 0xff) << 16); 159 value += ((int) (this._byteArray.get(potision + 3) & 0xff) << 24); 160 this.potision += 4; 161 return value; 162 } 163 164 /** 165 * append the int into this byteArray 166 * 167 * @param value 168 */ 169 public void writeInt(int value) { 170 this.write((byte) ((value >>> 0) & 0xff), this.potision); 171 this.write((byte) ((value >>> 8) & 0xff), this.potision + 1); 172 this.write((byte) ((value >>> 16) & 0xff), this.potision + 2); 173 this.write((byte) ((value >>> 24) & 0xff), this.potision + 3); 174 this.potision += 4; 175 } 176 177 /** 178 * return the next Long in this ByteArray 179 * 180 * @return 181 */ 182 public long readLong() { 183 long value = (long) (this._byteArray.get(potision) & 0xff); 184 value += ((long) (this._byteArray.get(potision + 1) & 0xff) << 8); 185 value += ((long) (this._byteArray.get(potision + 2) & 0xff) << 16); 186 value += ((long) (this._byteArray.get(potision + 3) & 0xff) << 24); 187 value += ((long) (this._byteArray.get(potision + 4) & 0xff) << 32); 188 value += ((long) (this._byteArray.get(potision + 5) & 0xff) << 40); 189 value += ((long) (this._byteArray.get(potision + 6) & 0xff) << 48); 190 value += ((long) (this._byteArray.get(potision + 7) & 0xff) << 56); 191 this.potision += 8; 192 return value; 193 } 194 195 /** 196 * append the long into this byteArray 197 * 198 * @param value 199 */ 200 public void writeLong(Long value) { 201 this.write((byte) ((value >>> 0) & 0xff), this.potision); 202 this.write((byte) ((value >>> 8) & 0xff), this.potision + 1); 203 this.write((byte) ((value >>> 16) & 0xff), this.potision + 2); 204 this.write((byte) ((value >>> 24) & 0xff), this.potision + 3); 205 this.write((byte) ((value >>> 32) & 0xff), this.potision + 4); 206 this.write((byte) ((value >>> 40) & 0xff), this.potision + 5); 207 this.write((byte) ((value >>> 48) & 0xff), this.potision + 6); 208 this.write((byte) ((value >>> 56) & 0xff), this.potision + 7); 209 this.potision += 8; 210 } 211 212 /** 213 * return the next float in this ByteArray 214 * 215 * @return 216 */ 217 public float readFloat() { 218 return Float.intBitsToFloat(this.readInt()); 219 } 220 221 /** 222 * append the float into this byteArray 223 * 224 * @param value 225 */ 226 public void writeFloat(float value) { 227 int temp = Float.floatToIntBits(value); 228 this.writeInt(temp); 229 } 230 231 /** 232 * return the next double in this ByteArray 233 * 234 * @return 235 */ 236 public double readDouble() { 237 return Double.longBitsToDouble(this.readLong()); 238 } 239 240 /** 241 * append the double into this byteArray 242 * 243 * @param value 244 */ 245 public void writeDouble(double value) { 246 long temp = Double.doubleToLongBits(value); 247 this.writeLong(temp); 248 } 249 250 public byte[] readbytes(int length) { 251 byte[] temp = new byte[length]; 252 for (int i = 0; i < length; i++) { 253 temp[i] = this._byteArray.get(this.potision + i); 254 } 255 this.potision += length; 256 return temp; 257 } 258 259 public void writebytes(byte[] value) { 260 for (int i = 0; i < value.length; i++) { 261 this.write(value[i], this.potision + i); 262 } 263 this.potision += value.length; 264 } 265 266 /** 267 * return the next string in this ByteArray 268 * 269 * @throws UnsupportedEncodingException 270 */ 271 public String readString() throws UnsupportedEncodingException { 272 int strlen = this.readShort(); 273 byte[] data = this.readbytes(strlen); 274 String temp = new String(data, "UTF-8"); 275 return temp; 276 } 277 278 /** 279 * append the string into this byteArray 280 * 281 * @throws UnsupportedEncodingException 282 */ 283 public void writeString(String value) throws UnsupportedEncodingException { 284 byte[] data = value.getBytes("UTF-8"); 285 this.writeShort((short) data.length); 286 this.writebytes(data); 287 } 288 289 public short readListNumber() { 290 short number = this.readShort(); 291 return number; 292 } 293 294 public void writeListNumber(short value) { 295 this.writeShort(value); 296 } 297 }