Hex-ache

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

Post Reply
richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10115
Joined: Fri Feb 19, 2010 10:17 am

Hex-ache

Post by richmond62 » Wed Aug 20, 2025 6:51 pm

If I do this sort of thing:

Code: Select all

put codePointToNum("Ξ")
I get 926, which is that code point's Unicode address in decimal format . . .

which is a bit of a pain in the bum as I should like it in Hexadecimal (39E) without having to play "silly buggers' with the calculator app (especially when I have about 3,000 of those to cope with) . . .

Is there any way to use codepointToNum to get a Hex number?

This is inaccurate:
-
Screenshot 2025-08-20 at 20.52.56.png
-
insofar is does NOT state that that number is returned in decimal format.

This can be done in JavaScript:

Code: Select all

(945).toString(16); // returns "3b1"
Of course I could use baseConvert, but I was wondering if there was a more direct method.

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

Re: Hex-ache

Post by dunbarx » Wed Aug 20, 2025 7:02 pm

richmond.

Try:

Code: Select all

function charToHex pChar
   return format("%04X",codePointToNum(pChar))
end charToHex
Craig

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

Re: Hex-ache

Post by stam » Wed Aug 20, 2025 7:54 pm

richmond62 wrote:
Wed Aug 20, 2025 6:51 pm
Of course I could use baseConvert, but I was wondering if there was a more direct method.
How is this not a direct method? This is the proper tool in liveCode to do just what you're asking, simply.

For your use case, 1 line:

Code: Select all

get baseConvert(codepointToNum(<char>), 10, 16)

To make it easier, you could wrap this into a stack-level function to simplify coding, eg:

Code: Select all

function hexOfChar pChar
    return baseConvert(codepointToNum(pChar), 10, 16)
end hexOfChar
You can then call this simply with:

Code: Select all

put hexOfChar(<char>) into <whatever>
Attached is a tiny stack that has this in its stack script so can be used anywhere, very simply. The stack script fires on textChanged of the source field.
Attachments
Convert to Hex.livecode.zip
(1.31 KiB) Downloaded 5 times

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

Re: Hex-ache

Post by richmond62 » Wed Aug 20, 2025 7:58 pm

Thank you both for your help. 8)

Post Reply