Home » Archimedes archive » Acorn User » AU 1997-10 A.adf » Extras » Apple][e/PD/BOB/ARMBOB/!ArmBob/progs/h/string/string

Apple][e/PD/BOB/ARMBOB/!ArmBob/progs/h/string/string

This website contains an archive of files for the Acorn Electron, BBC Micro, Acorn Archimedes, Commodore 16 and Commodore 64 computers, which Dominic Ford has rescued from his private collection of floppy disks and cassettes.

Some of these files were originally commercial releases in the 1980s and 1990s, but they are now widely available online. I assume that copyright over them is no longer being asserted. If you own the copyright and would like files to be removed, please contact me.

Tape/disk: Home » Archimedes archive » Acorn User » AU 1997-10 A.adf » Extras
Filename: Apple][e/PD/BOB/ARMBOB/!ArmBob/progs/h/string/string
Read OK:
File size: 0347 bytes
Load address: 0000
Exec address: 0000
File contents
/* GCW     06/06/94   */
/* convert a number n to a signed string to base b */

string(n,b)  
{
 local neg,s,d;
 if (n == 0) return ("0");
 if (neg = (n<0)) n = -n;
 s = "";
 while (n)
   {
    d = n%b;
    s = ((d<10)?(d +'0'):(d+'W'))+s;
    n /= b;
   }
 return (((neg)?"-":"")+s);
}

/* convert a number n to an unsigned hexadecimal string */
hex(n)
{
 local s,byte,top,bottom,b,i,zero;
 s = "&";zero = TRUE;i = 4;
 b = @(newstring(4));
 in b put { n; }
 while (i>0 && zero)
   {
    i--;
    if (byte=`(b+i)) zero = FALSE;
   }
 top = byte/16; bottom = byte%16;
 if (top) s += (top<10)?(top+'0'):(top+'W');
 s += (bottom<10)?(bottom+'0'):(bottom+'W'); 
 while (i>0)
   {
    i--; byte = `(b+i); top = byte/16; bottom = byte%16;
    s += (top<10)?(top+'0'):(top+'W');
    s += (bottom<10)?(bottom+'0'):(bottom+'W');
   }
 return s;
} 
00000000  2f 2a 20 47 43 57 20 20  20 20 20 30 36 2f 30 36  |/* GCW     06/06|
00000010  2f 39 34 20 20 20 2a 2f  0a 2f 2a 20 63 6f 6e 76  |/94   */./* conv|
00000020  65 72 74 20 61 20 6e 75  6d 62 65 72 20 6e 20 74  |ert a number n t|
00000030  6f 20 61 20 73 69 67 6e  65 64 20 73 74 72 69 6e  |o a signed strin|
00000040  67 20 74 6f 20 62 61 73  65 20 62 20 2a 2f 0a 0a  |g to base b */..|
00000050  73 74 72 69 6e 67 28 6e  2c 62 29 20 20 0a 7b 0a  |string(n,b)  .{.|
00000060  20 6c 6f 63 61 6c 20 6e  65 67 2c 73 2c 64 3b 0a  | local neg,s,d;.|
00000070  20 69 66 20 28 6e 20 3d  3d 20 30 29 20 72 65 74  | if (n == 0) ret|
00000080  75 72 6e 20 28 22 30 22  29 3b 0a 20 69 66 20 28  |urn ("0");. if (|
00000090  6e 65 67 20 3d 20 28 6e  3c 30 29 29 20 6e 20 3d  |neg = (n<0)) n =|
000000a0  20 2d 6e 3b 0a 20 73 20  3d 20 22 22 3b 0a 20 77  | -n;. s = "";. w|
000000b0  68 69 6c 65 20 28 6e 29  0a 20 20 20 7b 0a 20 20  |hile (n).   {.  |
000000c0  20 20 64 20 3d 20 6e 25  62 3b 0a 20 20 20 20 73  |  d = n%b;.    s|
000000d0  20 3d 20 28 28 64 3c 31  30 29 3f 28 64 20 2b 27  | = ((d<10)?(d +'|
000000e0  30 27 29 3a 28 64 2b 27  57 27 29 29 2b 73 3b 0a  |0'):(d+'W'))+s;.|
000000f0  20 20 20 20 6e 20 2f 3d  20 62 3b 0a 20 20 20 7d  |    n /= b;.   }|
00000100  0a 20 72 65 74 75 72 6e  20 28 28 28 6e 65 67 29  |. return (((neg)|
00000110  3f 22 2d 22 3a 22 22 29  2b 73 29 3b 0a 7d 0a 0a  |?"-":"")+s);.}..|
00000120  2f 2a 20 63 6f 6e 76 65  72 74 20 61 20 6e 75 6d  |/* convert a num|
00000130  62 65 72 20 6e 20 74 6f  20 61 6e 20 75 6e 73 69  |ber n to an unsi|
00000140  67 6e 65 64 20 68 65 78  61 64 65 63 69 6d 61 6c  |gned hexadecimal|
00000150  20 73 74 72 69 6e 67 20  2a 2f 0a 68 65 78 28 6e  | string */.hex(n|
00000160  29 0a 7b 0a 20 6c 6f 63  61 6c 20 73 2c 62 79 74  |).{. local s,byt|
00000170  65 2c 74 6f 70 2c 62 6f  74 74 6f 6d 2c 62 2c 69  |e,top,bottom,b,i|
00000180  2c 7a 65 72 6f 3b 0a 20  73 20 3d 20 22 26 22 3b  |,zero;. s = "&";|
00000190  7a 65 72 6f 20 3d 20 54  52 55 45 3b 69 20 3d 20  |zero = TRUE;i = |
000001a0  34 3b 0a 20 62 20 3d 20  40 28 6e 65 77 73 74 72  |4;. b = @(newstr|
000001b0  69 6e 67 28 34 29 29 3b  0a 20 69 6e 20 62 20 70  |ing(4));. in b p|
000001c0  75 74 20 7b 20 6e 3b 20  7d 0a 20 77 68 69 6c 65  |ut { n; }. while|
000001d0  20 28 69 3e 30 20 26 26  20 7a 65 72 6f 29 0a 20  | (i>0 && zero). |
000001e0  20 20 7b 0a 20 20 20 20  69 2d 2d 3b 0a 20 20 20  |  {.    i--;.   |
000001f0  20 69 66 20 28 62 79 74  65 3d 60 28 62 2b 69 29  | if (byte=`(b+i)|
00000200  29 20 7a 65 72 6f 20 3d  20 46 41 4c 53 45 3b 0a  |) zero = FALSE;.|
00000210  20 20 20 7d 0a 20 74 6f  70 20 3d 20 62 79 74 65  |   }. top = byte|
00000220  2f 31 36 3b 20 62 6f 74  74 6f 6d 20 3d 20 62 79  |/16; bottom = by|
00000230  74 65 25 31 36 3b 0a 20  69 66 20 28 74 6f 70 29  |te%16;. if (top)|
00000240  20 73 20 2b 3d 20 28 74  6f 70 3c 31 30 29 3f 28  | s += (top<10)?(|
00000250  74 6f 70 2b 27 30 27 29  3a 28 74 6f 70 2b 27 57  |top+'0'):(top+'W|
00000260  27 29 3b 0a 20 73 20 2b  3d 20 28 62 6f 74 74 6f  |');. s += (botto|
00000270  6d 3c 31 30 29 3f 28 62  6f 74 74 6f 6d 2b 27 30  |m<10)?(bottom+'0|
00000280  27 29 3a 28 62 6f 74 74  6f 6d 2b 27 57 27 29 3b  |'):(bottom+'W');|
00000290  20 0a 20 77 68 69 6c 65  20 28 69 3e 30 29 0a 20  | . while (i>0). |
000002a0  20 20 7b 0a 20 20 20 20  69 2d 2d 3b 20 62 79 74  |  {.    i--; byt|
000002b0  65 20 3d 20 60 28 62 2b  69 29 3b 20 74 6f 70 20  |e = `(b+i); top |
000002c0  3d 20 62 79 74 65 2f 31  36 3b 20 62 6f 74 74 6f  |= byte/16; botto|
000002d0  6d 20 3d 20 62 79 74 65  25 31 36 3b 0a 20 20 20  |m = byte%16;.   |
000002e0  20 73 20 2b 3d 20 28 74  6f 70 3c 31 30 29 3f 28  | s += (top<10)?(|
000002f0  74 6f 70 2b 27 30 27 29  3a 28 74 6f 70 2b 27 57  |top+'0'):(top+'W|
00000300  27 29 3b 0a 20 20 20 20  73 20 2b 3d 20 28 62 6f  |');.    s += (bo|
00000310  74 74 6f 6d 3c 31 30 29  3f 28 62 6f 74 74 6f 6d  |ttom<10)?(bottom|
00000320  2b 27 30 27 29 3a 28 62  6f 74 74 6f 6d 2b 27 57  |+'0'):(bottom+'W|
00000330  27 29 3b 0a 20 20 20 7d  0a 20 72 65 74 75 72 6e  |');.   }. return|
00000340  20 73 3b 0a 7d 20 0a                              | s;.} .|
00000347