Repeat loops using Hexadecimal?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10096
Joined: Fri Feb 19, 2010 10:17 am

Repeat loops using Hexadecimal?

Post by richmond62 » Mon Oct 07, 2024 11:18 am

Well, err, yeah . . .

Instead of this sort of thing:

Code: Select all

put 1 into KOUNT
repeat until KOUNT > 10
-- do something
add 1 to KOUNT
end repeat
which will send that loop round 10 (Ten) times, is there a way I can make my loop count up 1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
so it can pump out Hex numbers, so that loop (for the sake of argument) goes round '10' (Sixteen) times?

stam
Posts: 3069
Joined: Sun Jun 04, 2006 9:39 pm

Re: Repeat loops using Hexadecimal?

Post by stam » Mon Oct 07, 2024 12:01 pm

richmond62 wrote:
Mon Oct 07, 2024 11:18 am
Well, err, yeah . . .

Instead of this sort of thing:

Code: Select all

put 1 into KOUNT
repeat until KOUNT > 10
-- do something
add 1 to KOUNT
end repeat
which will send that loop round 10 (Ten) times, is there a way I can make my loop count up 1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
so it can pump out Hex numbers, so that loop (for the sake of argument) goes round '10' (Sixteen) times?

What you are requesting doesn't make much sense. I'm pretty sure there is not a single language that does this, but I may be wrong.

The loop you give in your example will never use hex digits because the counter never goes above 10.
You can't make a loop "that loop round 10 (Ten) times" starting from 1 and make it count up to F in hexadecimal ;)
Presumably you meant to write

Code: Select all

repeat until KOUNT > 16
If you need to iterate base-10 numbers and pass them as base-16, you could iterate 1 to 16 and convert the current iteration number to hexadecimal using

Code: Select all

baseConvert(<number>, <originalBase>, <destinationBase>)

-- or in your case:
baseConvert(KOUNT, 10, 16)

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10096
Joined: Fri Feb 19, 2010 10:17 am

Re: Repeat loops using Hexadecimal?

Post by richmond62 » Mon Oct 07, 2024 1:10 pm

I'm pretty sure there is not a single language that does this
I am sure I did manage this in Zilog in about 1984. 8)
What you are requesting doesn't make much sense.
Err: why not: after all our Maths teacher had us doing this with dried peas in 1975/6?
And if a load of spotty teenagers could go 1,2,3,4,5,6,7,8,9,A,B,C,D,E,F, it doesn't say much of computer languages that they cannot manage that: especially when all those languages get "boiled down" to Hex before the computer does any chewing.

Mind you I do remember getting a bit bogged down when I was asked to multiply Annty by Fifty-Dot. 8)

I do remember that we had blobs marked on our finger joints with board marker ink.
Presumably you meant to write
No, I did not: had I 'presumed' there would have been no point in my asking the question.

I need a repeat loop to go through about 500 Unicode Hex addresses.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: Repeat loops using Hexadecimal?

Post by dunbarx » Mon Oct 07, 2024 2:43 pm

Richmond,
I am sure I did manage this in Zilog in about 1984.
Really, you set up as:

Code: Select all

repeat with y = 1 to F
and the machine knew to iterate 16 times? There was a "hex" setting of some kind? That would mean that the code knew how to handle:

Code: Select all

repeat with y = 1 to F
if y = "C" then answer "You made it to a dozen. Congrats" 
Craig

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10096
Joined: Fri Feb 19, 2010 10:17 am

Re: Repeat loops using Hexadecimal?

Post by richmond62 » Mon Oct 07, 2024 4:13 pm

https://stackoverflow.com/questions/215 ... hex-values
Javascript, like most (all?) programing languages, knows how to handle Hexadecimal.

Code: Select all

for (i=0x0; i < 0x30; i++) { print("0x0" + i.toString(16) +" = " + i) }
So should we not be able to do this sort of thing:

Code: Select all

put 0x1 into KOUNT
repeat until KOUNT > 0xF
--do something
add 1 to KOUNT
end repeat
However, this:

Code: Select all

on mouseUp
   put empty into fld "SCL"
   put 1 into LYNE
   put 0x1 into KOUNT
   repeat until KOUNT > 0xF
      put KOUNT into line LYNE of fld "SCL"
      add 1 to LYNE
      add 0x1 to KOUNT
   end repeat
end mouseUp
spits out this:

0x1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

which is far from satisfactory.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10096
Joined: Fri Feb 19, 2010 10:17 am

Re: Repeat loops using Hexadecimal?

Post by richmond62 » Mon Oct 07, 2024 4:24 pm

This:

Code: Select all

on mouseUp
   put empty into fld "SCL"
   put 1 into KOUNT
   repeat until KOUNT > 0xF
      put baseConvert(KOUNT, 10, 16) into line KOUNT of fld "SCL"
      add 1 to KOUNT
   end repeat
end mouseUp
spits out this:

1
2
3
4
5
6
7
8
9
A
B
C
D
E
F

which, while being preferable to a poke in the face with a sharp stick, is a fudge.

stam
Posts: 3069
Joined: Sun Jun 04, 2006 9:39 pm

Re: Repeat loops using Hexadecimal?

Post by stam » Tue Oct 08, 2024 12:38 am

Not sure if there is a question there…

If LiveCode doesn’t natively iterate by hex numbers then your options are
1. Demand an engine re-write to include your niche need
2. Use native methods to convert and return the format you need

“Fudge” here means using the language’s abilities as is and work around its limitations. Like we all do. Every day.

Having said that, that you are even asking this question suggests you are trying either to do something in a non-LC-way (and failing, then complaining), or that you are trying to just use code from another language without converting it to LC compatible algorithms. Neither of which is flattering.

Unless someone knows better I would assume that that you cannot iterate in base-16 or base-2. So you work with the tools you have.

Or submit a change request like a the experienced developer you should be.

Complaining about it on the forum is not helpful to any readers and I doubt will further your cause.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Re: Repeat loops using Hexadecimal?

Post by bn » Tue Oct 08, 2024 8:42 am

richmond62 wrote:
Mon Oct 07, 2024 4:24 pm
This:
####
spits out this:
####
which, while being preferable to a poke in the face with a sharp stick, is a fudge.
Hi Richmond,

I don't see what you do not like about it.

Does this help?

Code: Select all

on mouseUp
   local KOUNT, tHex
   put empty into fld "SCL"
   put 0 into KOUNT
   put empty into tHex
   repeat until KOUNT > 1024
      put baseconvert(KOUNT, 10, 16) into tHex
      put KOUNT & ": "& "0x" & tHex into line KOUNT + 1 of fld "SCL"
      add 1 to KOUNT
   end repeat
end mouseUp
Kind regards
Bernd

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10096
Joined: Fri Feb 19, 2010 10:17 am

Re: Repeat loops using Hexadecimal?

Post by richmond62 » Tue Oct 08, 2024 9:58 am

Here's something odd:
-
SShot 2024-10-08 at 11.55.57.png
SShot 2024-10-08 at 11.55.57.png (15.2 KiB) Viewed 11255 times
-
SShot 2024-10-08 at 11.56.22.png
SShot 2024-10-08 at 11.56.22.png (18.65 KiB) Viewed 11255 times
-
SShot 2024-10-08 at 11.58.15.png
SShot 2024-10-08 at 11.58.15.png (23.01 KiB) Viewed 11255 times

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10096
Joined: Fri Feb 19, 2010 10:17 am

Re: Repeat loops using Hexadecimal?

Post by richmond62 » Tue Oct 08, 2024 10:04 am

Oh, "YUM!": serious inconsistency here:
-
SShot 2024-10-08 at 12.02.32.png
SShot 2024-10-08 at 12.02.32.png (18.1 KiB) Viewed 11253 times
-
So a Hex number that contains ONLY digits does NOT need double quotes, but a Hex number that contains alphabetics needs double quotes.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Re: Repeat loops using Hexadecimal?

Post by bn » Tue Oct 08, 2024 10:10 am

richmond62 wrote:
Tue Oct 08, 2024 10:04 am
Oh, "YUM!": serious inconsistency here:
So a Hex number that contains ONLY digits does NOT need double quotes, but a Hex number that contains alphabetics needs double quotes.
But well documented:

From the dictionary baseConvert
The number to be converted, expressed in its original base. The number
must be an integer between zero and 4,294,967,295 (2^32 - 1). If the
number includes non-digits (as, for example, a base-16 number can
include letters A-F), enclose the number in quotes
.
Kind regards
Bernd

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10096
Joined: Fri Feb 19, 2010 10:17 am

Re: Repeat loops using Hexadecimal?

Post by richmond62 » Tue Oct 08, 2024 10:43 am

But:
-
SShot 2024-10-08 at 12.02.32.png
SShot 2024-10-08 at 12.02.32.png (18.1 KiB) Viewed 11232 times
-
Works perfectly in the MessageBox.
-
SShot 2024-10-08 at 12.42.49.png
-
Seems not to in a button.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Re: Repeat loops using Hexadecimal?

Post by bn » Tue Oct 08, 2024 10:56 am

Code: Select all

on mouseUp
   put "2D80" into MAGIC
   put baseConvert(MAGIC,16,10)
end mouseUp
works in a button

Kind regards
Bernd

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10096
Joined: Fri Feb 19, 2010 10:17 am

Re: Repeat loops using Hexadecimal?

Post by richmond62 » Tue Oct 08, 2024 11:12 am

That thing with the quotes is confusing.

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Repeat loops using Hexadecimal?

Post by bogs » Sat Dec 28, 2024 4:46 pm

richmond62 wrote:
Tue Oct 08, 2024 11:12 am
That thing with the quotes is confusing.
You have the ability to make it less confusing, simply by ALWAYS quoting the number in quotes regardless of whether it is simple digits or mixed digits/alphabetical.
Image

Post Reply