-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlexByte.java
110 lines (94 loc) · 4.04 KB
/
FlexByte.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
public class FlexByte {
//changed from short to int because OutputStream.write(), which is
//used by java.net.Socket, can send one int at a time
private int byteData;
private boolean isSigned;
public FlexByte( int data, boolean signed ) {
byteData = data;
isSigned = signed;
}
public int getByteData() {
if( isSigned ) {
if( (byteData & 0x80) == 0x80 ) //first bit is a 1
return (( byteData & 0x7F ) - 0x80);
else //first bit is a 0
return byteData;
}
else //data is unsigned, so interpret "as is"
return byteData;
}
public static FlexByte[] toFlexByteArray( byte[] bytes, boolean isSigned ) {
int length = bytes.length;
FlexByte[] flexBytes = new FlexByte[bytes.length];
for( int i = 0; i < length; i++ )
flexBytes[i] = new FlexByte( (int) (bytes[i]), isSigned );
return flexBytes;
}
//takes a long and breaks it into length number of FlexBytes. For example,
//(0xffffffffffffL, 6, false) gets broken into an array of 6 FlexBytes,
//each of them unsigned.
public static FlexByte[] toFlexByteArray( long longNum, int length,
boolean isSigned ) {
FlexByte[] flexBytes = new FlexByte[length];
long allOnes = 0xFF;
for( int i = 0; i < length; i++ ) {
flexBytes[i] = new FlexByte( (int) ((longNum & allOnes) >> i*8),
isSigned );
allOnes = allOnes << 8;
}
return flexBytes;
}
public static byte[] toByteArray( FlexByte[] flexBytes ) {
int length = flexBytes.length;
byte[] bytes = new byte[length];
for( int i = 0; i < length; i++ )
bytes[i] = (byte) flexBytes[i].byteData;
return bytes;
}
}
//Just for our own reference:
//
//class Driver {
// public static void main(String[] args) {
// int leadingOne = 0xFF, leadingZero = 0x31;
// FlexByte fb1 = new FlexByte( leadingOne, true );
// FlexByte fb2 = new FlexByte( leadingZero, true );
// System.out.println( "Signed FlexBytes: fb1 = " + fb1.getByteData() + " fb2 = " + fb2.getByteData() );
// FlexByte fb3 = new FlexByte( leadingOne, false );
// FlexByte fb4 = new FlexByte( leadingZero, false );
// System.out.println( "Unsigned FlexBytes: fb3 = " + fb3.getByteData() + " fb4 = " + fb4.getByteData() );
// System.out.println();
//
// FlexByte[] flexBytes = { fb1, fb2, fb3, fb4 };
// byte[] bytes = FlexByte.toByteArray(flexBytes);
// byte[] comparisonBits = { (byte) 0x80, (byte) 0x40, (byte) 0x20, (byte) 0x10,
// (byte) 0x08, (byte) 0x04, (byte) 0x02, (byte) 0x01 };
// for( byte b: bytes ) {
// for( int i = 0; i < 8; i++ ) {
// System.out.print( ((b & comparisonBits[i]) == comparisonBits[i]) + ",");
// }
// System.out.println();
// }
// System.out.println();
//
// long longNum = 0xffffffffffffL;
// flexBytes = FlexByte.toFlexByteArray(longNum, 6, false);
// for( FlexByte flex: flexBytes )
// System.out.println( flex.getByteData() );
//
// byte[] bytes = { (byte) 0xF0 };
// flipBits(bytes);
// System.out.println( bytes[0] );
// }
//
// private static void flipBits( byte[] bytes ) {
// for( int i = 0; i < bytes.length; i++ ) {
// byte oldByte = bytes[i];
// bytes[i] = (byte) ( ((oldByte & 0x01)<<7) | ((oldByte & 0x02)<<5) |
// ((oldByte & 0x04)<<3) | ((oldByte & 0x08)<<1) |
// ((oldByte & 0x10)>>1) | ((oldByte & 0x20)>>3) |
// ((oldByte & 0x40)>>5) | ((oldByte & 0x80)>>7) );
// }
// }
//
//}