VAR long BCX0, BCX1, BCX2, BCX3 'BCX Workspace byte Symbols[7] 'Special symbols (7 characters) byte StrBuf[49] 'Internal String Buffer PUB Init ''Initialize to default settings. Init MUST be called before first object use. Config(@DefaultSymbols) PUB Config(SymAddr) bytemove(@Symbols, SymAddr, 7) PUB ToStr(Num, Format): StrAddr BCXToText(Format >> 19 & 7, Format >> 13 & $3F, Format >> 12 & 1, Format >> 11 & 1, Format >> 5 & $3F, BinToBCX(Num, Format & $1F #> 2 <# 16)) StrAddr := @StrBuf PRI BinToBCX(Num, Base): Digits | N longfill(@BCX0, 0, 4) 'Clear BCX Workspace N := (Num < 0) & $10000000 'Remember if Num negative repeat 'Calc all BCX digits byte[@BCX0][Digits++ >> 1] += ||(Num // Base) << (4 * Digits&1) while Num /= Base BCX3 |= N 'If negative, set flag (highest digit of BCX Workspace) PRI BCXToText(IChar, Grouping, ShowPlus, SPad, Field, Digits): Size | Idx, GCnt, SChar, GChar, X X := 1-(IChar > 0) 'Xtra char count (1 or 2, for sign and optional base indicator) IChar := Symbols[--IChar] 'Get base indicator character SChar := "+" + 2*(BCX3 >> 28) + 11*(not (ShowPlus | (BCX3 >> 28)) or ((Digits == 1) and (BCX0 == 0))) 'Determine sign character ('+', ' ' or '-') GChar := Symbols[Grouping & 7 - 1 #> 0] 'Get group character if Field > 0 and SPad^1 and Digits < 32 'Need to add extra zero-padding? BCX3 &= $0FFFFFFF ' then clear negative flag and set to 32 digits Digits := 32 Grouping := -((Grouping >>= 3) - (Grouping > 0))*(Grouping+1 < Digits) 'Get group size (0 if not enough Digits) Size := (Field - (Field==0)*(Digits+X+((Digits-1)/Grouping))) <# 49 'Field = 0? Set Size to Digits+X+Grouping (max 49). if Grouping 'Insert group chars bytefill(@StrBuf+(Size-Digits-(Digits-1)/Grouping #> 2), GChar, Digits+(Digits-1)/Grouping <# Size-4) Idx~~ 'Insert digits repeat while (++Idx < Digits) and (Idx + (GCnt := Idx/Grouping) < Size-X) byte[@StrBuf][Size-Idx-1-GCnt] := lookupz(byte[@BCX0][Idx>>1] >> (4 * Idx&1) // 16: "0".."9","A".."F") bytefill(@StrBuf, " ", Size-Idx-(Idx-1)/Grouping #> 0) 'Left pad with spaces, if necessary byte[@StrBuf][Size-X-Idx-(Idx-1)/Grouping #> 0] := SChar 'Insert sign if X == 2 byte[@StrBuf][Size-1-Idx-(Idx-1)/Grouping #> 1] := IChar 'Insert base indicator, if necessary byte[@StrBuf][Size] := 0 'Zero-terminate string DAT DefaultSymbols byte ",_$%xxx" 'Special, default, symbols ("x" means unused) CON ' III ZZZ GGG P S FFFFFF BBBBB CHAR2 = %000_000_000_0_0_000010_00000 'Fixed Width (size includes sign and special symbols) CHAR3 = %000_000_000_0_0_000011_00000 CHAR4 = %000_000_000_0_0_000100_00000 CHAR5 = %000_000_000_0_0_000101_00000 CHAR6 = %000_000_000_0_0_000110_00000 CHAR7 = %000_000_000_0_0_000111_00000 CHAR8 = %000_000_000_0_0_001000_00000 CHAR9 = %000_000_000_0_0_001001_00000 DEC = %000_000_000_0_0_000000_01010 'Decimal, variable widths HEX = %000_000_000_0_0_000000_10000 'Hexadecimal, variable widths HEX2 = HEX+CHAR2 'Hexadecimal, fixed widths, zero padded HEX3 = HEX+CHAR3 HEX4 = HEX+CHAR4 HEX5 = HEX+CHAR5 HEX6 = HEX+CHAR6 HEX7 = HEX+CHAR7 HEX8 = HEX+CHAR8 HEX9 = HEX+CHAR9