⍝ Hexadecimal from decimal:

    hex 1234 5678
┌────┬────┐
│04d2│162e│
└────┴────┘
    2 hex 1234 5678
┌──┬──┐
│d2│2e│
└──┴──┘
    4 hex 1234 5678
┌────┬────┐
│04d2│162e│
└────┴────┘
    8 hex 1234 5678
┌────────┬────────┐
│000004d2│0000162e│
└────────┴────────┘
    hex ¯3 to 3                             ⍝ hex of signed ints,
┌──┬──┬──┬──┬──┬──┬──┐
│fd│fe│ff│00│01│02│03│
└──┴──┴──┴──┴──┴──┴──┘
    hex 57005 48879
┌────┬────┐
│dead│beef│
└────┴────┘

    ⍕↑{⍵,hex ⍵}¨¯1 1∘.×,⍉¯1 0∘.+2*0 8 16 32     ⍝ Nicolas' test.
          0  00               
         ¯1  ff               
       ¯255  01               
       ¯256  ff00             
     ¯65535  0001             
     ¯65536  ffff0000         
¯4294967295  00000001         
¯4294967296  ffffffff00000000 
                              
          0  00               
          1  01               
        255  ff               
        256  0100             
      65535  ffff             
      65536  00010000         
 4294967295  ffffffff         
 4294967296  0000000100000000 

⍝ Check the examples from the notes:

    hex 12 34 56                            ⍝ convert to hex.
┌──┬──┬──┐
│0c│22│38│
└──┴──┴──┘
    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 (1 2 3)(100 200 300)                ⍝ nested array.
┌──────────┬────────────────┐
│┌──┬──┬──┐│┌────┬────┬────┐│
││01│02│03│││0064│00c8│012c││
│└──┴──┴──┘│└────┴────┴────┘│
└──────────┴────────────────┘

    dec hex (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│
└──┴──┴──┴──┴──┴──┴──┘

    hex ¯1+2*53                             ⍝ biggest number
┌────────────────┐
│001fffffffffffff│
└────────────────┘
    hex 2*113                               ⍝ too-big number
11::Too big

⍝∇ hex dec to

Back to: code

Back to: Workspaces