Discuss Scratch

46009361
Scratcher
1000+ posts

How do I convert a number from decimal to hexadecimal?

The Scratch Wiki should be showing the right script.

Anyway, I have a project that looks at the colour of a certain pixel of the screen by using a sprite to compare every colour and see how it matches. However, even though I have the counter (decimal number to be converted) change by 1 every time, the hexadecimal conversion (variable called “Output”) always shows 0, 00, 000, 0000, 00000, or 000000! What is going on? Can someone please help me?
deck26
Scratcher
1000+ posts

How do I convert a number from decimal to hexadecimal?

Do you mean you're using a script from the wiki? Provide a link please and share your project.

Using mod 16, subtracting and dividing by 16 until the value is 0 should be all that is required.

253 mod 16 = 13, subtract to get 240, divide to get 15. 15 mod 16 is 15 leaving 0 when you subtract. so the value is 15-13 which you need to convert to FD.
46009361
Scratcher
1000+ posts

How do I convert a number from decimal to hexadecimal?

deck26 wrote:

Do you mean you're using a script from the wiki? Provide a link please and share your project.

Using mod 16, subtracting and dividing by 16 until the value is 0 should be all that is required.

253 mod 16 = 13, subtract to get 240, divide to get 15. 15 mod 16 is 15 leaving 0 when you subtract. so the value is 15-13 which you need to convert to FD.
I modified the script from the wiki.

The “Counter” variable is the number that's being converted. I put it inside a repeat block in the original project which is unshared.

To prevent the project from freezing, I extracted out the script by creating a copy. Here it is: https://scratch.mit.edu/projects/347540267

Last edited by 46009361 (Nov. 22, 2019 13:02:04)

deck26
Scratcher
1000+ posts

How do I convert a number from decimal to hexadecimal?

You should have Temp mod 16 rather than Output mod 16.
46009361
Scratcher
1000+ posts

How do I convert a number from decimal to hexadecimal?

deck26 wrote:

You should have Temp mod 16 rather than Output mod 16.
Thanks
46009361
Scratcher
1000+ posts

How do I convert a number from decimal to hexadecimal?

kChiaEC19 wrote:

deck26 wrote:

You should have Temp mod 16 rather than Output mod 16.
Thanks
Oh wait, now I have to backpack a larger script. The color is random and should be the same as the Counter variable! What is happening now?
deck26
Scratcher
1000+ posts

How do I convert a number from decimal to hexadecimal?

kChiaEC19 wrote:

kChiaEC19 wrote:

deck26 wrote:

You should have Temp mod 16 rather than Output mod 16.
Thanks
Oh wait, now I have to backpack a larger script. The color is random and should be the same as the Counter variable! What is happening now?
No idea what you mean. Changing one variable doesn't change the size of a script.

Deal with one problem at a time please. Have you now resolved changing one decimal value to hex?

Last edited by deck26 (Nov. 22, 2019 15:59:28)

46009361
Scratcher
1000+ posts

How do I convert a number from decimal to hexadecimal?

deck26 wrote:

kChiaEC19 wrote:

kChiaEC19 wrote:

deck26 wrote:

You should have Temp mod 16 rather than Output mod 16.
Thanks
Oh wait, now I have to backpack a larger script. The color is random and should be the same as the Counter variable! What is happening now?
No idea what you mean. Changing one variable doesn't change the size of a script.

Deal with one problem at a time please. Hvae you now resolved changing one decimal value to hex?
Yes, but now, the hex conversion is random. I'll share with you the new version. It's going to be a copy. (Sorry for not getting the message)
46009361
Scratcher
1000+ posts

How do I convert a number from decimal to hexadecimal?

The digits don't look like they are in the order on this copy with the variable showing: https://scratch.mit.edu/projects/347595204

(Other project now unshared in earlier post)

Last edited by 46009361 (Nov. 22, 2019 15:31:43)

deck26
Scratcher
1000+ posts

How do I convert a number from decimal to hexadecimal?

You don't need to scan that number of colours. Scratch can't distinguish the lower bits of the colour ranges. See the Notes on this project https://scratch.mit.edu/projects/10941245/

You should be joining the new value in front of Output, not at the end.
46009361
Scratcher
1000+ posts

How do I convert a number from decimal to hexadecimal?

deck26 wrote:

You don't need to scan that number of colours. Scratch can't distinguish the lower bits of the colour ranges. See the Notes on this project https://scratch.mit.edu/projects/10941245/

You should be joining the new value in front of Output, not at the end.
I know, I'm just getting the highest color anyway.

Last edited by 46009361 (Nov. 22, 2019 20:33:13)

deck26
Scratcher
1000+ posts

How do I convert a number from decimal to hexadecimal?

kChiaEC19 wrote:

deck26 wrote:

You don't need to scan that number of colours. Scratch can't distinguish the lower bits of the colour ranges. See the Notes on this project https://scratch.mit.edu/projects/10941245/

You should be joining the new value in front of Output, not at the end.
I know, I'm just getting the highest color anyway.
But it means your code is many times slower than it could be. You'd actually be a lot better with 3 nested loops going through the RGB codes.
Agastya467
Scratcher
8 posts

How do I convert a number from decimal to hexadecimal?

PLS GIVE ME THE CODE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
BigNate469
Scratcher
1000+ posts

How do I convert a number from decimal to hexadecimal?

Agastya467 wrote:

PLS GIVE ME THE CODE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Please don't necropost. You can visit the link above (https://scratch.mit.edu/projects/10941245/) and remix the project if you are looking for the color detection script, and if you're looking for a hexadecimal conversion algorithm, look it up or create your own topic

Last edited by BigNate469 (March 27, 2024 16:11:32)

jwfshf
Scratcher
41 posts

How do I convert a number from decimal to hexadecimal?

I think I have the code it works (sometimes)

this is the project I used it in:
https://scratch.mit.edu/projects/1122374528/


it requires a list 1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,0
but it works most of the time, also I would love for someone to help me improve it.
Oisthebestletter
Scratcher
100+ posts

How do I convert a number from decimal to hexadecimal?

jwfshf wrote:

I think I have the code it works (sometimes)

this is the project I used it in:
https://scratch.mit.edu/projects/1122374528/


it requires a list 1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,0
but it works most of the time, also I would love for someone to help me improve it.
Don't necropost.

Powered by DjangoBB