decimal UNIDs to hex format

Freeform discussion about anything related to modding Transcendence.
Post Reply
relanat
Militia Commander
Militia Commander
Posts: 438
Joined: Tue Nov 05, 2013 9:56 am

Wed Oct 18, 2017 1:27 am

Jumping between ENTITY and hex UNIDs in the debug screen is tedious. Also some functions return decimal UNIDs.

gc has sorted the ENTITY UNIDs with a contribution.

But is there a function that converts hex UNIDs to decimal form and back again? That would be really helpful.

Or is there a download or easy way to do this?

User avatar
digdug
Fleet Admiral
Fleet Admiral
Posts: 2603
Joined: Mon Oct 29, 2007 9:23 pm
Location: Decoding hieroglyphics on Tan-Ru-Dorem

Wed Oct 18, 2017 8:13 am

Alterecco's DSF had hex2dec and dec2hex
I've been using them too for my mods.

There is bug in one of the 2 (don't remember) that returns and error if you pass 0 (zero).

In your case, this should not be a problem, because UNIDs are never 0.
If you want, I can post my modified (blatantly hacked) version.

relanat
Militia Commander
Militia Commander
Posts: 438
Joined: Tue Nov 05, 2013 9:56 am

Thu Oct 19, 2017 12:09 am

digdug wrote: If you want, I can post my modified (blatantly hacked) version.
Yes, please. Greatly appreciated. :D

User avatar
JohnBWatson
Fleet Officer
Fleet Officer
Posts: 1360
Joined: Tue Aug 19, 2014 10:17 pm

Thu Oct 19, 2017 7:56 am

There are plenty of websites with hex to decimal converters. That's what I use whenever I need a conversion.

Edit: Misread - you wanted a function in the game.
Last edited by JohnBWatson on Sun Oct 22, 2017 3:44 am, edited 1 time in total.

User avatar
digdug
Fleet Admiral
Fleet Admiral
Posts: 2603
Joined: Mon Oct 29, 2007 9:23 pm
Location: Decoding hieroglyphics on Tan-Ru-Dorem

Sat Oct 21, 2017 11:23 pm

ok, so before starting, if you want to convert a hex number to decimal, just do

(int hexNumber) --> decNumber

for converting a dec to hex I use this:

Code: Select all

	;taken from alterecco's DSF, no need to reinvent the wheel, i suppose.
	;remember, to do the opposite, you just need to (int hexNumber) returns decNumber
	;original function had a bug, if input dec = 0 it won't return 0, so I just put a blatant hack to fix it.
(setq WE_DecToHex (lambda (dec)
                (block (tokens tmp hex)
                    (setq tokens (list '0 '1 '2 '3 '4 '5 '6 '7 '8 '9 'A 'B 'C 'D 'E 'F))
                    (setq tmp (list))
                    (setq hex "")
                    (if (eq dec 0)
                    	    (setq tmp 0)
                    (loop (gr dec 0)  (block Nil
                        (lnkAppend tmp (@ tokens (modulo dec 16)))
                        (setq dec (divide dec 16))
                    ))
                    )
                    (apply cat (WE_ListReverse tmp))
                )
            ))

	    ;again taken from alterecco's DSF
            ;; Reverse the sequence of a lists elements
            ;;
            ;; @example: (dsf_ListReverse '(1 2 3)) : (3 2 1)
            ;;
            (setq WE_ListReverse (lambda (lst)
                (block (result)
                    (enum lst el
                        (setq result (append el result))
                    )
                )
            ))

NMS
Militia Commander
Militia Commander
Posts: 400
Joined: Tue Mar 05, 2013 8:26 am

Sun Oct 22, 2017 3:04 am

Not bad, but it doesn't work on negative numbers. Here's a version that does. Maybe I'll add it to my pull request for core.

Code: Select all

			(setq mathDecToHex (lambda (decimal)
				(block (tokens result negative)
					(setq tokens '(0 1 2 3 4 5 6 7 8 9 A B C D E F))
					(setq decimal (convertTo 'int32 decimal))
					(if (ls decimal 0)
						(block Nil
							(setq decimal (- decimal 0x80000000))
							(setq negative True)
							)
						)
					(if (eq decimal 0)
						(setq result 0)
						(loop (gr decimal 0)
							(block Nil
								(setq result (cat (@ tokens (modulo decimal 16)) result))
								(setq decimal (divide decimal 16))
								)
							)
						)
					(if negative
						(block Nil
							(for i 1 (- 8 (count result))
								(setq result (cat 0 result))
								)
							(setq result (cat (@ tokens (+ (subset result 0 1) 8))
											(subset result 1)
											)
								)
							)
						)
					(cat "0x" result)
					)
				))

relanat
Militia Commander
Militia Commander
Posts: 438
Joined: Tue Nov 05, 2013 9:56 am

Sun Oct 22, 2017 8:55 am

Thanks all. :D

Post Reply