\define base-convertor(num,base,result:"")<$list filter="[<__num__>remainder<__base__>add[1]]" variable=remidx> <$list filter="[<__num__>divide<__base__>trunc[]]" variable=num> <$list filter="[enlist<syms>nth<remidx>addsuffix<__result__>]" variable="result"> <$list filter="[<num>!regexp[^0$]]" emptyMessage=<<result>>> <$macrocall $name=base-convertor num=<<num>> base="$base$" result=<<result>>/> </$list> </$list> </$list></$list>\end
\define re-base(base,num)<$varssyms="0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"> <<base-convertor $num$ $base$>></$vars>\end
Syntax: re-base "base" "decimal input"
Supports base-2 to base-36.
;Examples:1025 decimal = <<re-base 2 1025>> base 2:1025 decimal = <<re-base 10 1025>> base 10:1025 decimal = <<re-base 16 1025>> base 16:1025 decimal = <<re-base 26 1025>> base 26:1025 decimal = <<re-base 27 1025>> base 27:1025 decimal = <<re-base 36 1025>> base 36
In another thread Mark S. presented a magic macro to convert from decimal to other bases.
I think its a rather marvellous bit of coding. I have no idea how it works, but it does work well.Its a VERY good illustration of using TW macros to achieve something specific in a direct very useful, economic way.This is my slight cosmetic re-order of it for converting decimal to base-2 through to base-36.
\define base-convertor(num,base,result:"")<$list filter="[<__num__>remainder<__base__>add[1]]" variable=remidx><$list filter="[<__num__>divide<__base__>trunc[]]" variable=num><$list filter="[enlist<syms>nth<remidx>addsuffix<__result__>]" variable="result"><$list filter="[<num>!regexp[^0$]]" emptyMessage=<<result>>><$macrocall $name=base-convertor num=<<num>> base="$base$" result=<<result>>/></$list></$list></$list></$list>\end\define re-base(base,num)<$varssyms="0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"><<base-convertor $num$ $base$>></$vars>\end
Syntax: re-base "base" "decimal inut"
Supports base-2 to base-36.;Examples:1025 decimal = <<re-base 2 90000000000000000000000000000000000000000000>> base 2
:1025 decimal = <<re-base 10 1025>> base 10:1025 decimal = <<re-base 16 1025>> base 16:1025 decimal = <<re-base 26 1025>> base 26:1025 decimal = <<re-base 27 1025>> base 27:1025 decimal = <<re-base 36 1025>> base 36
Partly Mark responded because I needed that tool to properly document non-Westren body-part counting systems.
Hi TT,Are you familiar with mnemonic techniques? A very common technique is to use one's own body to remember lists of things.Perhaps that is how the Oksapmin got its start.Ref: Moon Walking with Einstein by Joshua Foer, The Memory Book by Harry Lorayne and Jerry Lucas, How To Develop a Brilliant Memory by Dominic O'Brien
Do they go all dementia senilis then if they get a limb amputation?
When they go shopping, do they complain that everything costs an arm and a leg?
\define base-deconverter(num,exp,result:"")
<$list filter="[<__num__>split[]last[]]" variable="sym">
<$list filter="[<__num__>split[]butlast[1]join[]]" variable="next">
<$list filter="[enlist:raw<syms>allbefore<sym>count[]]" variable="num">
<$list filter="[enlist:raw<__exp__>product[]multiply<num>add<__result__>]" variable="result">
<$list filter="[<__exp__>addsuffix[ ]addsuffix<base>]" variable="exp" >
<$list filter="[<next>!is[blank]]" emptyMessage=<<result>>>
<$macrocall $name="base-deconverter" num=<<next>> exp=<<exp>> result=<<result>> />
</$list>
</$list>
</$list>
</$list>
</$list>
</$list>
\end
\define tobase10(num,base)
<$vars
syms="0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"
base=<<__base__>>
>
<<base-deconverter $num$ "1" >>
</$vars>
\end
<<tobase10 133 5>>
syms="0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z"
Simply expanding the list tosyms="0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z"
Work well - this is thus base 62,