This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-02-02 13:51:35

rdococ
Scratcher
Registered: 2009-10-11
Posts: 1000+

How do I use lists to convert decimal to any base?

I want to be able to convert any decimal number into any base (base 10, 16, 32, etc.) specified by a list (if your way uses lists) because I plan on making a converting project.
-------------------------------------------------------------------------------------
I deleted a 5-minute older topic similar to this one, but I decided to make something else instead.

Offline

 

#2 2012-02-02 14:09:50

LS97
Scratcher
Registered: 2009-06-14
Posts: 1000+

Re: How do I use lists to convert decimal to any base?

I'd do it with a formula, rather.

Being ignorant in this subject my way would be count upwards in decimal and add a digit every "base" numbers, but there must be a faster, easier way. Google could help!

Offline

 

#3 2012-02-02 14:24:57

rdococ
Scratcher
Registered: 2009-10-11
Posts: 1000+

Re: How do I use lists to convert decimal to any base?

LS97 wrote:

I'd do it with a formula, rather.

Being ignorant in this subject my way would be count upwards in decimal and add a digit every "base" numbers, but there must be a faster, easier way. Google could help!

Yeah, I just had that thought but can't get it working.

Offline

 

#4 2012-02-02 14:26:54

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: How do I use lists to convert decimal to any base?

Here's a script:

when gf clicked
delete (all v) of [remainders v]
ask [Give a decimal number] and wait
set [number v] to (answer)
ask [Choose a base to convert to] and wait
set [base v] to (answer)
repeat until <(number) = [0]>
	set [rounded down v] to [] //mini script from wiki
	set [count v] to [0]
	repeat until <<(letter (count) of ((number) / (base))) = [.]> or <(count) = (length of ((number) / (base)))>>
		set [rounded down v] to (join (rounded down) (letter (count) of ((number) / (base))))
		change [count v] by (1) //end mini script
	end
	add (item (((number) mod (base)) + (1)) of [in base v]) to [remainders v]
	set [number v] to (rounded down)
end
set [count v] to (length of [remainders v]) //mini script to join digits
set [new num v] to []
repeat (length of [remainders v])
	set [new num v] to (join (new num)(item (count) of [remainders v]))
	change [count v] by (-1)
end
say (join (join([Your number in base](base))[ is ](new num))
Does that work for you?
You'll need a list called "in bases" that has all the characters in the bases. Item 1 of the list should be 0.

BTW, Google helped. XD

Last edited by scimonster (2012-02-02 14:29:01)

Offline

 

#5 2012-02-02 14:28:38

demosthenes
Retired Community Moderator
Registered: 2008-02-19
Posts: 1000+

Re: How do I use lists to convert decimal to any base?

This should work for bases less than 10, for bases greater than ten, you will need to create alphabets to stand in for numbers and modify the script accordingly.
http://i.imgur.com/QUlJy.gif


I've taken a long hiatus, but I still visit sometimes. Give me some time to answer any messages you post on my projects!

Offline

 

#6 2012-02-02 14:42:09

rdococ
Scratcher
Registered: 2009-10-11
Posts: 1000+

Re: How do I use lists to convert decimal to any base?

I made it work with conversion to binary, but it still isn't working with base 3.

Offline

 

#7 2012-02-02 14:49:46

rdococ
Scratcher
Registered: 2009-10-11
Posts: 1000+

Re: How do I use lists to convert decimal to any base?

Sorry everyone, I'm about to explode in confusion.
I'm sure I found a small and simple way, why isn't it working in Scratch?

Offline

 

#8 2012-02-02 14:51:47

JSO
Community Moderator
Registered: 2007-06-23
Posts: 1000+

Re: How do I use lists to convert decimal to any base?

Am I seeing a screenshot there  yikes

when green flag clicked
set [base v] to (answer)
ask [Input a number] and wait
set [number v] to (answer)
delete (all v) of [value v]
repeat (10)
  add [0] to [value v]
end
set [difference v] to (number)
repeat until <(difference) = [0]
  set [index v] to [0]
  set [temp v] to [1]
  repeat until <(temp) > (difference)>
    set [temp v] to ((base) * (temp))
    change [index v] by (1)
  end
  replace item (index) of [value v] with (round (((difference) - ((difference) mod ((temp) / (base)))) / ((temp) / (base))))
  set [difference v] to ((difference) - ((item (index) of [value v])*((temp) / (base))))
end


http://oi48.tinypic.com/2v1q0e9.jpg

Offline

 

#9 2012-02-02 15:01:27

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: How do I use lists to convert decimal to any base?

Did you see my post? It works for anything up to base 36. It would be an even shorter script of Scratch has a (round [up/down v] ()) block.

Last edited by scimonster (2012-02-02 15:01:45)

Offline

 

#10 2012-02-03 02:55:47

rdococ
Scratcher
Registered: 2009-10-11
Posts: 1000+

Re: How do I use lists to convert decimal to any base?

scimonster wrote:

Did you see my post? It works for anything up to base 36. It would be an even shorter script of Scratch has a (round [up/down v] ()) block.

Yes, but it's just too long...

Offline

 

#11 2012-02-03 10:46:51

JSO
Community Moderator
Registered: 2007-06-23
Posts: 1000+

Re: How do I use lists to convert decimal to any base?

scimonster wrote:

Did you see my post? It works for anything up to base 36. It would be an even shorter script of Scratch has a (round [up/down v] ()) block.

Rounding up or down (called 'floor' or 'ceil' in a lot of programming languages) can already be done in Scratch  smile

The solution is actually fairly simple:

say (round ((value) + (0.5))) //round up
say (round ((value) - (0.5))) //round down
smile


http://oi48.tinypic.com/2v1q0e9.jpg

Offline

 

#12 2012-02-03 12:51:20

RedRocker227
Scratcher
Registered: 2011-10-26
Posts: 1000+

Re: How do I use lists to convert decimal to any base?

scimonster wrote:

Did you see my post? It works for anything up to base 36. It would be an even shorter script of Scratch has a (round [up/down v] ()) block.

You can just put:

(round ((value) + (0.5)))
and
(round ((value) - (0.5)))
wink


Why

Offline

 

#13 2012-02-03 12:52:20

RedRocker227
Scratcher
Registered: 2011-10-26
Posts: 1000+

Re: How do I use lists to convert decimal to any base?

scimonster wrote:

Here's a script:

when gf clicked
delete (all v) of [remainders v]
ask [Give a decimal number] and wait
set [number v] to (answer)
ask [Choose a base to convert to] and wait
set [base v] to (answer)
repeat until <(number) = [0]>
	set [rounded down v] to [] //mini script from wiki
	set [count v] to [0]
	repeat until <<(letter (count) of ((number) / (base))) = [.]> or <(count) = (length of ((number) / (base)))>>
		set [rounded down v] to (join (rounded down) (letter (count) of ((number) / (base))))
		change [count v] by (1) //end mini script
	end
	add (item (((number) mod (base)) + (1)) of [in base v]) to [remainders v]
	set [number v] to (rounded down)
end
set [count v] to (length of [remainders v]) //mini script to join digits
set [new num v] to []
repeat (length of [remainders v])
	set [new num v] to (join (new num)(item (count) of [remainders v]))
	change [count v] by (-1)
end
say (join (join([Your number in base](base))[ is ](new num))
Does that work for you?
You'll need a list called "in bases" that has all the characters in the bases. Item 1 of the list should be 0.

BTW, Google helped. XD

How long did it take you to figure that out?! D:


Why

Offline

 

#14 2012-02-04 12:50:16

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: How do I use lists to convert decimal to any base?

JSO wrote:

scimonster wrote:

Did you see my post? It works for anything up to base 36. It would be an even shorter script of Scratch has a (round [up/down v] ()) block.

Rounding up or down (called 'floor' or 'ceil' in a lot of programming languages) can already be done in Scratch  smile

The solution is actually fairly simple:

say (round ((value) + (0.5))) //round up
say (round ((value) - (0.5))) //round down
smile

I know what it is in other languages.  tongue  Cool, thanks for the workaround.  big_smile

RedRocker227 wrote:

scimonster wrote:

Here's a script:

when gf clicked
delete (all v) of [remainders v]
ask [Give a decimal number] and wait
set [number v] to (answer)
ask [Choose a base to convert to] and wait
set [base v] to (answer)
repeat until <(number) = [0]>
	set [rounded down v] to [] //mini script from wiki
	set [count v] to [0]
	repeat until <<(letter (count) of ((number) / (base))) = [.]> or <(count) = (length of ((number) / (base)))>>
		set [rounded down v] to (join (rounded down) (letter (count) of ((number) / (base))))
		change [count v] by (1) //end mini script
	end
	add (item (((number) mod (base)) + (1)) of [in base v]) to [remainders v]
	set [number v] to (rounded down)
end
set [count v] to (length of [remainders v]) //mini script to join digits
set [new num v] to []
repeat (length of [remainders v])
	set [new num v] to (join (new num)(item (count) of [remainders v]))
	change [count v] by (-1)
end
say (join (join([Your number in base](base))[ is ](new num))
Does that work for you?
You'll need a list called "in bases" that has all the characters in the bases. Item 1 of the list should be 0.

BTW, Google helped. XD

How long did it take you to figure that out?! D:

Oh, 5 minutes, once I had the algorithm.  wink

Here's an updated version of the script:

when gf clicked
delete (all v) of [remainders v]
ask [Give a decimal number] and wait
set [number v] to (answer)
ask [Choose a base to convert to] and wait
set [base v] to (answer)
repeat until <(number) = [0]>
	add (item (((number) mod (base)) + (1)) of [in base v]) to [remainders v]
	set [number v] to (round ((number) - (0.5)))
end
set [count v] to (length of [remainders v]) //mini script to join digits
set [new num v] to []
repeat (length of [remainders v])
	set [new num v] to (join (new num)(item (count) of [remainders v]))
	change [count v] by (-1)
end
say (join(join [Your number in base](base))(join [ is ](new num)))

Last edited by scimonster (2012-02-04 12:54:40)

Offline

 

#15 2012-02-04 18:53:47

wilsonbiggs
Scratcher
Registered: 2011-10-08
Posts: 71

Re: How do I use lists to convert decimal to any base?

I'm doing something similar, building a base conversion block in BYOB... it keeps outputting 0.

base (base =13) 2 digit number (orignum =54) to base 10
set [baseout v] to <<letter (1) of (orignum)> + <(base)*<letter (2) of (orignum)>>>
report baseout

Last edited by wilsonbiggs (2012-02-04 18:55:10)


http://i196.photobucket.com/albums/aa122/wilsonbiggs/Screenshot2012-02-04at11552PM.pnghttp://dragcave.net/image/S0uKJ.gifhttp://dragcave.net/image/bQId9.gif

Offline

 

#16 2012-02-05 07:58:58

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: How do I use lists to convert decimal to any base?

wilsonbiggs wrote:

I'm doing something similar, building a base conversion block in BYOB... it keeps outputting 0.

base (base =13) 2 digit number (orignum =54) to base 10
set [baseout v] to <<letter (1) of (orignum)> + <(base)*<letter (2) of (orignum)>>>
report baseout

Try using my script.  wink

Offline

 

#17 2012-02-05 09:14:33

rdococ
Scratcher
Registered: 2009-10-11
Posts: 1000+

Re: How do I use lists to convert decimal to any base?

wilsonbiggs wrote:

I'm doing something similar, building a base conversion block in BYOB... it keeps outputting 0.

base (base =13) 2 digit number (orignum =54) to base 10
set [baseout v] to <<letter (1) of (orignum)> + <(base)*<letter (2) of (orignum)>>>
report baseout

That's funny... I calculated and the answer should be 56. Use the

report [stuff]
in the Control category and place it onto the end of the script.

Offline

 

Board footer