Discuss Scratch

jTron
Scratcher
100+ posts

Tester for decimal to binary conversion

The one thing I've always wanted a nice easy block for is decimal to binary conversion, for encoding large amounts of on/off information in a single integer. If there's already a block out there, it would be great if someone would let me know. Do note that I have seen many a converter for this sort of thing, but many are clunky and/or need lists, which I am trying to avoid. Otherwise, here's what I've come up with (from some algorithms I've found online):


Decimal [st] > Binary @ [*bint] | WV [wv] [[ // I use the * to remind me that bint is a pointer and as such should never be directly accessed or modified. WV is a working variable and its value is ignored and cleared in the first line.
Set [wv v] to ().
Forever[[
if <({{st}} % 2) = (0)>[[
Set [wv v] to (join (0) and {{wv}}).]]
else[[
Set [wv v] to (join (1) and {{wv}}).
Change [st v] by (-1).]].
Set [st v] to ({{st}} / (2)).
}
if <not <{{st}} > 0>>[[
Set [{{*bint}} v] to ({{wv}}). // this line will require JSON editing
Stop [this script v].]].]].]]. // I'm not sure about the properties of this block. Will it stop the block, or the entire script the block is in? If the entire script, replace the forever with a repeat until, put in the conditions that I've got in the last if, find a way (maybe using the wv) to ensure that the script runs at least once, and end with the "set …" line. Hopefully, that's not the case.

If someone would test this out for me, as I'm currently in a situation where either the Internet is terrible (a.k.a. fluctuates from about 10 bytes/sec to .3 mb/sec) or it's decent (…ish, upwards of .5 mb/sec stably) but there is no flash (a.k.a. an iPad, on one of which I am typing this and autocorrect is ruining my day.), that would be brilliant. (Pant, pant…)

So it's not crucial but would be really helpful if some nice fellow out there would help out a fellow scratcher and just test this out and let me know if it works. It does require some JSON editing, so if someone wants help with that just ask. It's not in scratchblocks because I'm not quite sure if that's there with the new forums (?) or if it would compat with the pointer. Sorry if you don't understand how I've syntaxed that, it's a direct modification of a java snippet I found.

Thanks a lot!
jTron

Woot - first post on 2.0!

Last edited by jTron (July 27, 2013 20:58:46)


clipd • osx clipboard manager and history

;
drmcw
Scratcher
1000+ posts

Tester for decimal to binary conversion

You don't need JSON far that. Just tweak this.

10 !
ScratchVaders or Galaga?
Maybe Eliza can help you decide?
jTron
Scratcher
100+ posts

Tester for decimal to binary conversion

Oh gosh…sorry about half the code being not there, BB screwed it up :(
Just click quote to read it, I guess, there's no ignore bbcode button yet.
- Fixed

Drmcw, the reason I need JSON editing is because of the last line, where I need to set a variable that the script specifies. It's only because I'm making a custom reporter block, which (sadly) doesn't currently exist. Thanks for the link- does that project use the divisible by two, divide by two, tack on a number method or lists?

Last edited by jTron (July 27, 2013 20:59:57)


clipd • osx clipboard manager and history

;
drmcw
Scratcher
1000+ posts

Tester for decimal to binary conversion

It does not use lists but divides by 2 and tacks.

10 !
ScratchVaders or Galaga?
Maybe Eliza can help you decide?
doonkoo
Scratcher
100+ posts

Tester for decimal to binary conversion

wait wut? I LIKE BINARY BUT I HAVE ABSOLUTELY NO IDEA WHAT YOU ARE DOING!

“I am pleased a book on science competes with the memoirs of pop stars. Maybe there is some hope for the human race.”
-Stephen Hawking

“Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world.”
-Albert Einstein

“I have noticed people who claim everything is predestined, and that there is nothing we can do to change it, look before they cross the road.”
-Stephen Hawking
drmcw
Scratcher
1000+ posts

Tester for decimal to binary conversion

doonkoo wrote:

wait wut? I LIKE BINARY BUT I HAVE ABSOLUTELY NO IDEA WHAT YOU ARE DOING!

Decimal to binary conversion.

10 !
ScratchVaders or Galaga?
Maybe Eliza can help you decide?

Powered by DjangoBB