-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitString.java
More file actions
161 lines (147 loc) · 4.66 KB
/
BitString.java
File metadata and controls
161 lines (147 loc) · 4.66 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
public class BitString
{
/*------- Methods to convert to/from bit strings -----*/
// BitString to String
public static String bitStringToString(String bitString)
{
String str = ""; // Empty String
// Extract 8 bits at a time
int ix = 0;
int numChars = bitString.length()/8; // Assume multiple of 8
for(int i = 0 ; i<numChars; i++)
{
String bitCharString = bitString.substring(ix,ix+8);
str = str+bitStringToChar(bitCharString);
ix += 8; // skip 8 bits
}
return(str);
}
// BitString to int
public static int bitStringToInt(String bitString)
{
int intVal = 0;
// Initilaise mask according to number of bits
int mask = (int) Math.pow(2,bitString.length()-1); // set high-order bit to 1
for(int i = 0; i<bitString.length(); i++)
{
if(bitString.charAt(i) == '1')
intVal = intVal | mask; // set the bit
mask = mask>>1; // shift 1 by one position.
}
return (intVal);
}
// 8 bit BitString to Char
public static char bitStringToChar(String bitString)
{
char ch = '\0'; // null char - all zeros
int mask = 128; // set bit 0 to 1;
for(int i = 0; i<8; i++)
{
if(bitString.charAt(i) == '1')
ch = (char) ((int) ch | mask); // set the bit
mask = mask>>1; // shift 1 by one position.
}
return ch;
}
// String to BitString
public static String stringToBitString(String str)
{
String bitString = ""; // Empty String
for(int i = 0 ; i<str.length(); i++) // to process all characters
bitString = bitString+charToBitString(str.charAt(i));
return(bitString);
}
// Char to BitString
public static String charToBitString(char ch)
{
String bitAdr = "";
int mask = 1; // set bit 0 to 1;
for(int i = 0; i<8; i++)
{
if((ch & mask) == 0) // bit in address is sero
bitAdr = "0"+bitAdr;
else
bitAdr = "1"+bitAdr;
mask = mask<<1; // shift 1 by one position.
}
return (bitAdr);
}
// int to BitString
// Its up to the calling method to ensure numBits is
// large enough to represent the value. Can be used
// to convert address and sequence numbers to BitString
public static String intToBitString(int intVal, int numBits)
{
String bitString = "";
int mask = 1; // set bit 0 to 1;
for(int i = 0; i<numBits; i++)
{
if((intVal & mask) == 0) // bit in address is sero
bitString = "0"+bitString;
else
bitString = "1"+bitString;
mask = mask<<1; // shift 1 by one position.
}
return bitString;
}
/*------------------------ Other methods ----------------------------*/
// Splits the string into and array of strings
// Each string is at most size chars long
public static String[] splitString(String str, int size)
{
// Variable declarations
int numSubStrings; // number of substrings
int ix, arIx; // index to index into str String and subString array.
String [] subStrArray; // reference to substring array.
int last; // last index in the substring
// Set up an array with enough entries to references substrings
if(str.length() % size == 0) numSubStrings = str.length()/size;
else numSubStrings = str.length()/size +1;
subStrArray = new String [numSubStrings];
// Fill in the array
for(ix = 0, arIx = 0 ; ix < str.length() ; ix +=size, arIx++)
{
if(ix+size <= str.length()) last = ix+size;
else last = str.length();
subStrArray[arIx] = str.substring(ix,last);
}
return(subStrArray);
}
// For displaying a frame (bitString)
public static String displayFrame(String frame)
{
String display;
int ix, last; // for indexing and substring
// insert space every 8 bits
if(frame.length() > 8)
{
display = frame.substring(0,8);
ix = 8;
}
else
{
display = frame;
ix = frame.length();
}
String eightBits;
int end = frame.length();
for(; ix < end ; ix +=8)
{
// If frame contains more than 7 bytes (i.e. information frame
// only want to display first 4 bytes and last 2 bytes
// Adjust ix to point to the last two bytes after the first
// 4 bytes have been displayed
if(frame.length() > 7 && ix == 4*8)
{
ix = frame.length()-2*8;
display = display+ " ... "; // shows undisplayed data
}
// set the index lf the last bit to extra 8 bits
if(ix+8 <= frame.length()) last = ix+8;
else last = frame.length(); // in case not multiple of 8
eightBits = frame.substring(ix,last);
display = display+" "+eightBits;
}
return(display);
}
}