User Commands Int2Hex and Hex2Int
Outdated: the Dyalog built-in user command got improved and can now deal with the cases outlined on this page. |
Overview
Although Dyalog comes from two built-in user commands for converting a hex number into an integer and vice versa (]ToHex and ]FromHex), fromHex is too limited to be really useful.
For example, is one wishes to convert an error code returned by the Windows Task Scheduler:
]tohex 2147942667 * Command Execution Failed: must be 32 bits signed integer
With the excellent function Hex written and contributed by PhilLast - which is the internal engine of the two user commands - this can be avoided:
]Int2hex 2147942667 8007010b ]Hex2Int 8007010b 2147942667
This is Phil's underlying code:
Hex←{ ⎕IO←0 ⋄ ⎕ML←1 t←0∊⊃⍬⍴0⍴⊂⍵ a←⎕D,'abcdef',⎕D,6⍴⎕A t:a⌷⍨⊂⍉16⊥⍣¯1⊢⍵ 16⊥⍉16|a⍳⍵ ⍝ ⍵ dec-number or hex-string ⍝ ← hex-string or dec-number ⍝ accepts hex as CAPS or small ⍝ returns hex as small ⍝ dec to hex is rank increasing ⍝ hex to dec is rank decreasing }
Download
Download the script.
Authors: Logic=PhilLast, user command=KaiJaeger