cvex ← {width} ##.hex nums                  ⍝ Hexadecimal from decimal.
nums ←         ##.dec cvex                  ⍝ Decimal from hexadecimal.

[hex]  and  [dec]  convert between simple arrays of non-negative decimal numbers
and their hexadecimal equivalents. Hex's optional left argument [width], specif-
ies  the  number of hexadecimal digits required. Width defaults to 2, 4, 8 or 16
depending on the magnitude of the argument.

Examples:

      hex 12 34 56                          ⍝ convert to hex.
 0c  22  38

      disp hex 12 34 56                     ⍝ (result is nested).
┌→─┬──┬──┐ 
│0c│22│38│
└─→┴─→┴─→┘ 

      hex 100×⍳6                            ⍝ larger numbers.
 0064  00c8  012c  0190  01f4  0258

      hex 100000×⍳4                         ⍝ even larger numbers.
 000186a0  00030d40  000493e0  00061a80

      hex 4 5⍴⍳20                           ⍝ array of numbers.
 01  02  03  04  05
 06  07  08  09  0a
 0b  0c  0d  0e  0f
 10  11  12  13  14

      4 hex 4 5⍴⍳20                         ⍝ specify width.
 0001  0002  0003  0004  0005
 0006  0007  0008  0009  000a
 000b  000c  000d  000e  000f
 0010  0011  0012  0013  0014

      dec'3e8'                              ⍝ convert to decimal.
1000
      dec'3E8'                              ⍝ upper case A-Z is OK, too.
1000
      dec hex 2 4 5⍴⍳40                     ⍝ convert both ways.
 1  2  3  4  5
 6  7  8  9 10
11 12 13 14 15
16 17 18 19 20

21 22 23 24 25
26 27 28 29 30
31 32 33 34 35
36 37 38 39 40

      hex saw (1 2 3)(100 200 300)          ⍝ nested array.
  01  02  03    0064  00c8  012c

      disp hex saw(1 2 3)(100 200 300)
┌→─────────┬────────────────┐ 
│┌→─┬──┬──┐│┌→───┬────┬────┐│ 
││01│02│03│││0064│00c8│012c││
│└─→┴─→┴─→┘│└───→┴───→┴───→┘│ 
└─────────→┴───────────────→┘ 

      dec saw hex saw (1 2 3)(100 200 300)  ⍝ full circle.
 1 2 3  100 200 300

      dec∘hex saw (1 2 3)(100 200 300)      ⍝ full circle.
 1 2 3  100 200 300

⍝ hex of negative integers returns 2's complement or "unsigned" hex number:

      hex ¯3 to 3                           ⍝ hex of negative numbers.
 fd  fe  ff  00  01  02  03

See also: uns saw hexf decf

Back to: contents

Back to: Dyalog APL

Trouble seeing APL font?