This is an exact inverse of the question Convert to Spoken Binary. This introduction is copied from there.
Introduction
In the video the best way to count, binary is proposed as the best system of counting numbers. Along with this argument is a proposal on how to say numbers in this system. First, we give names to each "double power of two", \$2^{2^n}\$ for each \$n\$.
number = symbol = spoken ============================ 2^0 = 1 = "one" 2^1 = 2 = "two" 2^2 = 4 = "four" 2^4 = H = "hex" 2^8 = B = "byte" 2^16 = S = "short" 2^32 = I = "int" 2^64 = L = "long" 2^128 = O = "overlong" 2^256 = P = "byteplex"
Then, to get from a number to its spoken binary, we
Take its (big-endian) bit string and break off bits from the end equal to the number of zeros in the largest double power of two less than or equal to the number.
Use the name for the corresponding double power of two in the middle, and recursively name the left and right parts through the same procedure. If the left part is one, it is not spoken, and if the right part is zero, it is not spoken.
This system is similar to how we normally read numbers: 2004 -> 2 "thousand" 004 -> "two thousand four".
You can find examples of this procedure in the linked question.
To parse a number in spoken binary, do the opposite of the above, i.e.
Find the largest double power of two in the spoken binary (it is unique).
Recursively parse the sections to the left and the right through the same procedure (assuming one on left and zero on right if either are empty), and compute left * middle + right.
This system is similar to how we normally parse numbers: "two thousand four" -> 2 * 1000 + 4 -> 2004.
As an example,
"2H214"
"2" * 16 + "214"
2 * 16 + "21" * 4 + 0
2 * 16 + (2 + 1) * 4 + 0
44
Challenge
Your program must take a string of symbols for the spoken binary of a positive integer \$n\$ as input and output the integer it represents in decimal.
While numbers under \$2^{512}\$ are expressible in this system, you only need to handle integers up to and including \$2^{32}\$ = I
, and as such, do not need to consider L
, O
, or P
.
You only need to consider valid spoken binary strings; you do not need to handle cases like HH
, 1H
, H44
, 4121S
etc.
Standard loopholes are forbidden. As this is code-golf, shortest program wins.
Example Input and Output
1 -> 1
2 -> 2
21 -> 3
4 -> 4
41 -> 5
42 -> 6
421 -> 7
24 -> 8
241 -> 9
242 -> 10
2421 -> 11
214 -> 12
H241 -> 25
2H214 -> 44
42H4 -> 100
21B2142H24 -> 1000
21421H21421B21421H21421S21421H21421B21421H21421 -> 4294967295
I -> 4294967296
If you need any more test cases, the answers to the linked question can help. Here is one TIO (Credit to @Nick Kennedy).