Differences between revisions 32 and 33
Revision 32 as of 2017-06-01 16:31:46
Size: 126
Editor: KaiJaeger
Comment:
Revision 33 as of 2017-06-01 16:43:45
Size: 1329
Editor: KaiJaeger
Comment:
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:

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
must be 32 bits signed integer
Run[61] r←1 hex ##.THIS⍎r⊣⎕PP←34
       ∧
}}}

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
      }
}}}

User Commands Int2Hex and Hex2Int

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                              
must be 32 bits signed integer
Run[61] r←1 hex ##.THIS⍎r⊣⎕PP←34

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
      }

Authors: Logic=PhilLast, user command=KaiJaeger


CategoryDyalogUserCommands

UserCommands/Hex (last edited 2019-05-30 07:48:57 by KaiJaeger)