2
$\begingroup$

This is my first Mathematica post and I would be obliged if you could be patient with me.

I have the following large binary number $1111010010001001100011010101111110000101101110110011100101$ which I have split into 4 smaller parts.

$1111\ 01001000100110001101010\ 111111\ 0000101101110110011100101$

How would I convert each of the 4 smaller parts back into decimal values i.e 1111 is the same as 15 etc.

I used IntegerString to convert the number to binary and StringTake to break the binary number down into the smaller groups and now I am stuck.

$\endgroup$
  • $\begingroup$ related? How can I transform a list of binary digits into integers $\endgroup$ – user1066 Aug 1 '18 at 11:54
  • $\begingroup$ S.Cook, could you clarify if the input is a String ("1111 01001000100110001101010 111111 0000101101110110011100101") or a list of Integers (1111, 01001000100110001101010, 111111, 0000101101110110011100101})? $\endgroup$ – kglr Aug 1 '18 at 12:03
  • 1
    $\begingroup$ It was a string. $\endgroup$ – S. Cook Aug 2 '18 at 17:01
2
$\begingroup$
d = {1111, 01001000100110001101010, 111111, 0000101101110110011100101};
FromDigits[#, 2] & /@ IntegerDigits@d

{15, 2378858, 63, 1502437}

$\endgroup$
2
$\begingroup$
string = "1111 01001000100110001101010 111111 0000101101110110011100101";

FromDigits[#, 2] & /@ StringSplit[string]

{15, 2378858, 63, 1502437}

Update: To work directly with with original string:

sPartition = FromDigits[#, 2] & /@ StringJoin /@ TakeList[Characters @ #, #2]&;

string0 = "1111010010001001100011010101111110000101101110110011100101"; 
sPartition[string0, {4, 23, 6, All}] 

{15, 2378858, 63, 1502437}

Update 2: The answer above assumes the input is a String. If, instead, it is a list of Integers, then, in addition to the methods in other answers you can also do

#. (2)^(# - Range @ # & @ Length @ #) & /@ 
 IntegerDigits[{1111, 01001000100110001101010, 111111, 0000101101110110011100101}]

{15, 2378858, 63, 1502437}

$\endgroup$
  • $\begingroup$ your last update does not work as expected. $\endgroup$ – OkkesDulgerci Aug 1 '18 at 13:09
  • 1
    $\begingroup$ @OkkesDulgerci, make sure you have space between Dot (.) and 2 so that you are not multiplying # with .2. $\endgroup$ – kglr Aug 1 '18 at 13:13
  • $\begingroup$ Interesting. When I pasted the space is disparaged $\endgroup$ – OkkesDulgerci Aug 1 '18 at 13:21
1
$\begingroup$
FromDigits[#, 2] & /@ (ToString /@ d)

where d = {1111, 01001000100110001101010, 111111, 0000101101110110011100101}

$\endgroup$
0
$\begingroup$
Fold[2 #1 + #2 &, 0, #] & /@ (IntegerDigits@d)

{15, 2378858, 63, 1502437}

where

d = {1111, 01001000100110001101010, 111111, 0000101101110110011100101};
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.