Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Help with hexadecimal conversion
- duckboycool
-
1000+ posts
Help with hexadecimal conversion
That is true but you should look at the two above comments. Thank you.
Not you, the one on the previous page.
Not you, the one on the previous page.
Last edited by duckboycool (Feb. 11, 2017 22:24:53)
- BASIC4U
-
54 posts
Help with hexadecimal conversion
Yes you can. This is also case sensitive allowing for 22 different character types, but you can't check or decode for that on the other side.https://wiki.scratch.mit.edu/wiki/Case_Sensing_(2.0)
- duckboycool
-
1000+ posts
Help with hexadecimal conversion
I didn't know of those methods. Learn new stuff.Yes you can. This is also case sensitive allowing for 22 different character types, but you can't check or decode for that on the other side.https://wiki.scratch.mit.edu/wiki/Case_Sensing_(2.0)
- TheLogFather
-
1000+ posts
Help with hexadecimal conversion
Scratch uses double-precision floating-point to store numerical values. That means you have a maximum of 16(ish) digits for an integer.
As deck26 said, you must NOT use mathematical operations on such large strings of digits, but you should use string operators instead (which will also mean you can drop the leading one, as deck26 said).
If you want to convert to hex using mathematical operators, you must only do it on smaller ‘chunks’ of digits, not the whole thing.
For example, don't convert the 18 digit string “102030405060708090” to hex. Instead, split it into, say, two chunks of 9 digits, and convert each of those chunks, padding each conversion with leading zeros if necessary (to get to some fixed length), and then join.
(Also, there's really no difference between converting to hex and converting to case-sensitive hex, except the range of ‘digits’ it has. You just need a case-sensitive digit detector when converting back to decimal.)
Hope that helps!
As deck26 said, you must NOT use mathematical operations on such large strings of digits, but you should use string operators instead (which will also mean you can drop the leading one, as deck26 said).
If you want to convert to hex using mathematical operators, you must only do it on smaller ‘chunks’ of digits, not the whole thing.
For example, don't convert the 18 digit string “102030405060708090” to hex. Instead, split it into, say, two chunks of 9 digits, and convert each of those chunks, padding each conversion with leading zeros if necessary (to get to some fixed length), and then join.
(Also, there's really no difference between converting to hex and converting to case-sensitive hex, except the range of ‘digits’ it has. You just need a case-sensitive digit detector when converting back to decimal.)
Hope that helps!
Last edited by TheLogFather (Feb. 12, 2017 00:29:23)
- GizzB
-
100+ posts
Help with hexadecimal conversion
That isn't reliable though, as 100000000 is 0x5F5E100 so if you'd join it it'd be 0x5F5E1005F5E100 which results in some obsurd number… right? Scratch uses double-precision floating-point to store numerical values. That means you have a maximum of 16(ish) digits for an integer.
As deck26 said, you must NOT use mathematical operations on such large strings of digits, but you should use string operators instead (which will also mean you can drop the leading one, as deck26 said).
If you want to convert to hex using mathematical operators, you must only do it on smaller ‘chunks’ of digits, not the whole thing.
For example, don't convert the 18 digit string “102030405060708090” to hex. Instead, split it into, say, two chunks of 9 digits, and convert each of those chunks, padding each conversion with leading zeros if necessary (to get to some fixed length), and then join.
(Also, there's really no difference between converting to hex and converting to case-sensitive hex, except the range of ‘digits’ it has. You just need a case-sensitive digit detector when converting back to decimal.)
Hope that helps!
- f1lip
-
1000+ posts
Help with hexadecimal conversion
Hexadecimal on Scratch only works on letters a-f. You have to work around that…
- GizzB
-
100+ posts
Help with hexadecimal conversion
I have already, making things hexadecimal was the easy part… the hard part is making it work for large numbers as scratch hates large numbers Hexadecimal on Scratch only works on letters a-f. You have to work around that…
- GizzB
-
100+ posts
Help with hexadecimal conversion
http://thestarman.pcministry.com/asm/6to64bits.htmI took a look at that and it's exactly what I need, arbitrary precision base 16… I found a converter for it here with open source code: http://www.danvk.org/hex2dec.html but I can't figure out how to implement it into scratch…. any tips?

- deck26
-
1000+ posts
Help with hexadecimal conversion
If you're storing more than one number you can either use a fixed number of digits per value or precede each value with its length so you know how many digits it has.That isn't reliable though, as 100000000 is 0x5F5E100 so if you'd join it it'd be 0x5F5E1005F5E100 which results in some obsurd number… right? Scratch uses double-precision floating-point to store numerical values. That means you have a maximum of 16(ish) digits for an integer.
As deck26 said, you must NOT use mathematical operations on such large strings of digits, but you should use string operators instead (which will also mean you can drop the leading one, as deck26 said).
If you want to convert to hex using mathematical operators, you must only do it on smaller ‘chunks’ of digits, not the whole thing.
For example, don't convert the 18 digit string “102030405060708090” to hex. Instead, split it into, say, two chunks of 9 digits, and convert each of those chunks, padding each conversion with leading zeros if necessary (to get to some fixed length), and then join.
(Also, there's really no difference between converting to hex and converting to case-sensitive hex, except the range of ‘digits’ it has. You just need a case-sensitive digit detector when converting back to decimal.)
Hope that helps!
- GizzB
-
100+ posts
Help with hexadecimal conversion
Not accurate for hex though, for example, the last digit of the first block could be 1 and the first digit of the second block could be 5… so it would be stored as xxxxxxxx1 5xxxxxxxx and pieced like that… when actually it could've meant FIf you're storing more than one number you can either use a fixed number of digits per value or precede each value with its length so you know how many digits it has.That isn't reliable though, as 100000000 is 0x5F5E100 so if you'd join it it'd be 0x5F5E1005F5E100 which results in some obsurd number… right? Scratch uses double-precision floating-point to store numerical values. That means you have a maximum of 16(ish) digits for an integer.
As deck26 said, you must NOT use mathematical operations on such large strings of digits, but you should use string operators instead (which will also mean you can drop the leading one, as deck26 said).
If you want to convert to hex using mathematical operators, you must only do it on smaller ‘chunks’ of digits, not the whole thing.
For example, don't convert the 18 digit string “102030405060708090” to hex. Instead, split it into, say, two chunks of 9 digits, and convert each of those chunks, padding each conversion with leading zeros if necessary (to get to some fixed length), and then join.
(Also, there's really no difference between converting to hex and converting to case-sensitive hex, except the range of ‘digits’ it has. You just need a case-sensitive digit detector when converting back to decimal.)
Hope that helps!
- deck26
-
1000+ posts
Help with hexadecimal conversion
Of course it's accurate. Let's assume I want no hex value with more than 20 hex digits but in my string I may have values of different lengths.Not accurate for hex though, for example, the last digit of the first block could be 1 and the first digit of the second block could be 5… so it would be stored as xxxxxxxx1 5xxxxxxxx and pieced like that… when actually it could've meant FIf you're storing more than one number you can either use a fixed number of digits per value or precede each value with its length so you know how many digits it has.That isn't reliable though, as 100000000 is 0x5F5E100 so if you'd join it it'd be 0x5F5E1005F5E100 which results in some obsurd number… right? Scratch uses double-precision floating-point to store numerical values. That means you have a maximum of 16(ish) digits for an integer.
As deck26 said, you must NOT use mathematical operations on such large strings of digits, but you should use string operators instead (which will also mean you can drop the leading one, as deck26 said).
If you want to convert to hex using mathematical operators, you must only do it on smaller ‘chunks’ of digits, not the whole thing.
For example, don't convert the 18 digit string “102030405060708090” to hex. Instead, split it into, say, two chunks of 9 digits, and convert each of those chunks, padding each conversion with leading zeros if necessary (to get to some fixed length), and then join.
(Also, there's really no difference between converting to hex and converting to case-sensitive hex, except the range of ‘digits’ it has. You just need a case-sensitive digit detector when converting back to decimal.)
Hope that helps!
05xxxxx10yyyyyyyyyyyyyyyy07zzzzzzz is clearly specified as a 5 digit value, then a 16 digit value and then a 7 digit value. I can even choose to use decimal for the lengths if I prefer which in this case would just mean changing that hex value 10 to 16. What's the problem?
- GizzB
-
100+ posts
Help with hexadecimal conversion
You just blew my mind xDOf course it's accurate. Let's assume I want no hex value with more than 20 hex digits but in my string I may have values of different lengths.Not accurate for hex though, for example, the last digit of the first block could be 1 and the first digit of the second block could be 5… so it would be stored as xxxxxxxx1 5xxxxxxxx and pieced like that… when actually it could've meant FIf you're storing more than one number you can either use a fixed number of digits per value or precede each value with its length so you know how many digits it has.That isn't reliable though, as 100000000 is 0x5F5E100 so if you'd join it it'd be 0x5F5E1005F5E100 which results in some obsurd number… right? Scratch uses double-precision floating-point to store numerical values. That means you have a maximum of 16(ish) digits for an integer.
As deck26 said, you must NOT use mathematical operations on such large strings of digits, but you should use string operators instead (which will also mean you can drop the leading one, as deck26 said).
If you want to convert to hex using mathematical operators, you must only do it on smaller ‘chunks’ of digits, not the whole thing.
For example, don't convert the 18 digit string “102030405060708090” to hex. Instead, split it into, say, two chunks of 9 digits, and convert each of those chunks, padding each conversion with leading zeros if necessary (to get to some fixed length), and then join.
(Also, there's really no difference between converting to hex and converting to case-sensitive hex, except the range of ‘digits’ it has. You just need a case-sensitive digit detector when converting back to decimal.)
Hope that helps!
05xxxxx10yyyyyyyyyyyyyyyy07zzzzzzz is clearly specified as a 5 digit value, then a 16 digit value and then a 7 digit value. I can even choose to use decimal for the lengths if I prefer which in this case would just mean changing that hex value 10 to 16. What's the problem?
- TheLogFather
-
1000+ posts
Help with hexadecimal conversion
Wow, I missed all of this – didn't follow this topic. (Pity, since I would've answered your question straight away.)
As deck was saying above (and as I was meaning before), you can work in ‘chunks’ of hex digits of known, limited lengths, and you just convert back and forth as much as you want for each chunk.
I do this all the time in my case-sensitive-hex-based cloud encode/decode project. There really is no difficulty here.
As deck was saying above (and as I was meaning before), you can work in ‘chunks’ of hex digits of known, limited lengths, and you just convert back and forth as much as you want for each chunk.
I do this all the time in my case-sensitive-hex-based cloud encode/decode project. There really is no difficulty here.
Last edited by TheLogFather (Feb. 12, 2017 17:08:24)
- GizzB
-
100+ posts
Help with hexadecimal conversion
Your project doesn't encode the data though? Wow, I missed all of this – didn't follow this topic. (Pity, since I would've answered your question straight away.)
As deck was saying above (and as I was meaning before), you can work in ‘chunks’ of hex digits of known, limited lengths, and you just convert back and forth as much as you want for each chunk.
I do this all the time in my case-sensitive-hex-based cloud encode/decode project. There really is no difficulty here.
Encoding “255255” results in encoded data of;
0x2255255F
- TheLogFather
-
1000+ posts
Help with hexadecimal conversion
It uses different ways to encode different data types, with a different prefix for each type.Your project doesn't encode the data though? Wow, I missed all of this – didn't follow this topic. (Pity, since I would've answered your question straight away.)
As deck was saying above (and as I was meaning before), you can work in ‘chunks’ of hex digits of known, limited lengths, and you just convert back and forth as much as you want for each chunk.
I do this all the time in my case-sensitive-hex-based cloud encode/decode project. There really is no difficulty here.
Encoding “255255” results in encoded data of;
0x2255255F
Positive numerical values are encoded with a prefix of “2” (negative with prefix of “a”), and then it keeps the subsequent digits of the string the same (but it encodes a non-digit character, such as +, -, space, dot, into some hex character), with “F” marking end of the data.
Some others it encodes much more tightly (for example, zero is encoded as a single “0”, “Infinity” is encoded as a single “4”, and boolean “False” is encoded as a single “5”), so their prefix is the whole of the data too.
Strings are encoded with a prefix of “8”, then using a pair of case-insensitive hex digits for each character (allowing nearly 500 possible characters), up until it reaches “FF” which marks end of the data.
The point is, you're thinking in a far too restricted way. You're thinking about hex all the time, as if you need to convert between decimal and hex, etc.
You don't. All you need to do is *encode* (each chunk of) data in such a way that you know how to get it all back, i.e. how to decode (each chunk of data). And you can do that however you see fit for your purposes!
Last edited by TheLogFather (Feb. 12, 2017 17:48:47)
- GizzB
-
100+ posts
Help with hexadecimal conversion
I see, well my idea is to make it all hex as it's very efficient at compressing… the only issue is I need to get arbitrary hex in scratch as opposed to normal hex.It uses different ways to encode different data types, with a different prefix for each type.Your project doesn't encode the data though? Wow, I missed all of this – didn't follow this topic. (Pity, since I would've answered your question straight away.)
As deck was saying above (and as I was meaning before), you can work in ‘chunks’ of hex digits of known, limited lengths, and you just convert back and forth as much as you want for each chunk.
I do this all the time in my case-sensitive-hex-based cloud encode/decode project. There really is no difficulty here.
Encoding “255255” results in encoded data of;
0x2255255F
Positive numerical values are encoded with a prefix of “2” (negative with prefix of “a”), and then it keeps the subsequent digits of the string the same (but it encodes a non-digit character, such as +, -, space, dot, into some hex character), with “F” marking end of the data.
Some others it encodes much more tightly (for example, zero is encoded as a single “0”, “Infinity” is encoded as a single “4”, and boolean “False” is encoded as a single “5”), so their prefix is the whole of the data too.
Strings are encoded with a prefix of “8”, then using a pair of case-insensitive hex digits for each character (allowing nearly 500 possible characters), up until it reaches “FF” which marks end of the data.
The point is, you're thinking in a far too restricted way. You're thinking about hex all the time, as if you need to convert between decimal and hex, etc.
You don't. All you need to do is *encode* (each chunk of) data in such a way that you know how to get it all back, i.e. how to decode (each chunk of data). And you can do that however you see fit for your purposes!
- deck26
-
1000+ posts
Help with hexadecimal conversion
In your other topic I've explained why you're only saving about 1 digit in 6 so it's not that efficient actually.I see, well my idea is to make it all hex as it's very efficient at compressing… the only issue is I need to get arbitrary hex in scratch as opposed to normal hex.It uses different ways to encode different data types, with a different prefix for each type.Your project doesn't encode the data though? Wow, I missed all of this – didn't follow this topic. (Pity, since I would've answered your question straight away.)
As deck was saying above (and as I was meaning before), you can work in ‘chunks’ of hex digits of known, limited lengths, and you just convert back and forth as much as you want for each chunk.
I do this all the time in my case-sensitive-hex-based cloud encode/decode project. There really is no difficulty here.
Encoding “255255” results in encoded data of;
0x2255255F
Positive numerical values are encoded with a prefix of “2” (negative with prefix of “a”), and then it keeps the subsequent digits of the string the same (but it encodes a non-digit character, such as +, -, space, dot, into some hex character), with “F” marking end of the data.
Some others it encodes much more tightly (for example, zero is encoded as a single “0”, “Infinity” is encoded as a single “4”, and boolean “False” is encoded as a single “5”), so their prefix is the whole of the data too.
Strings are encoded with a prefix of “8”, then using a pair of case-insensitive hex digits for each character (allowing nearly 500 possible characters), up until it reaches “FF” which marks end of the data.
The point is, you're thinking in a far too restricted way. You're thinking about hex all the time, as if you need to convert between decimal and hex, etc.
You don't. All you need to do is *encode* (each chunk of) data in such a way that you know how to get it all back, i.e. how to decode (each chunk of data). And you can do that however you see fit for your purposes!
- Chrome_Cat
-
40 posts
Help with hexadecimal conversion
For anyone who wants to convert hexadecimal to decimal, I have created the shortest possible code for doing so, available at https://scratch.mit.edu/projects/356320752/
- Discussion Forums
- » Help with Scripts
-
» Help with hexadecimal conversion