81
81
82
82
/* array.reduce();
83
83
group 4 bits then translate */
84
- const binaryToHex = ( binaryString ) => {
84
+ const binaryToHex = ( binaryString0 ) => {
85
85
const hexNumbers = new Map ( ) ;
86
86
87
87
hexNumbers . set ( '0000' , '0' ) ;
@@ -107,15 +107,33 @@ <h1>
107
107
// group binary string into 4 bit chunks
108
108
const array = [ ] ;
109
109
110
- for ( let i = 0 ; i < binaryString . length ; i += 4 ) {
111
- let chunk = binaryString . substring ( 0 , 4 ) ;
110
+ console . log ( '*** binaryToHex ***' , 'binaryString0= ' , binaryString0 ) ;
111
+ // important; translate binary string from right to left
112
112
113
- //pad left with 0s is bit string <4
114
- if ( chunk . length < 4 ) {
115
- chunk = chunk . padEnd ( 4 , '0' ) ;
116
- }
113
+ let length = binaryString0 . length ;
114
+
115
+ /*
116
+ - if the string is not a multiple of 4;
117
+ left-pad it.
118
+
119
+ - if binaryString0='' then pad with 4
120
+ */
121
+ const missing = length === 0 ? 4 : ( ( length % 4 === 0 ) ? 0 : ( 4 - length % 4 ) ) ;
122
+ const binaryString = binaryString0 . padStart ( length + missing , '0' ) ;
123
+
124
+ const newLength = binaryString . length ;
125
+
126
+
127
+ for ( let i = 0 ; i < newLength ; i += 4 ) {
128
+ let chunk = binaryString . substring ( i , i + 4 ) ;
129
+
130
+ console . log ( '-- chunk=' , chunk ) ;
117
131
118
- array . push ( hexNumbers . get ( chunk ) ) ;
132
+ const value = hexNumbers . get ( chunk ) ;
133
+
134
+ array . push ( value ) ;
135
+
136
+ console . log ( '--value=' , value ) ;
119
137
}
120
138
121
139
const hexString = array . join ( '' ) ;
@@ -131,13 +149,22 @@ <h1>
131
149
using array.unshift(r)
132
150
*/
133
151
const decimalToBinaryV1 = ( decimalString ) => {
134
- /*division remainers*/
152
+ /*
153
+ - division remainers
154
+ */
135
155
const array = [ ] ;
136
156
137
157
/*quotient mutated*/
138
158
let i = BigInt ( decimalString ) ;
139
-
140
159
160
+ /*
161
+ - if decimalString='0' then
162
+ while loop will NOT run;
163
+ */
164
+ if ( i === BigInt ( 0 ) ) {
165
+ array . unshift ( BigInt ( 0 ) ) ;
166
+ }
167
+
141
168
/*stop when quotient = 0*/
142
169
while ( i != BigInt ( 0 ) ) {
143
170
@@ -495,7 +522,7 @@ <h2>Invalid Input</h2>
495
522
super ( props ) ;
496
523
}
497
524
componentDidMount ( ) {
498
- this . props [ 'do-initial-onInputDecimal' ] ( '1234 ' ) ;
525
+ this . props [ 'do-initial-onInputDecimal' ] ( '256 ' ) ;
499
526
}
500
527
render ( ) {
501
528
const {
0 commit comments