# BBC BASIC Programmers' Reference

### Site Tools

number_20conversion_20in_20basic

# Differences

This shows you the differences between two versions of the page.

 number_20conversion_20in_20basic [2018/03/31 13:19]127.0.0.1 external edit number_20conversion_20in_20basic [2018/04/17 17:51]tbest3112 Added syntax highlighting 2018/04/17 17:51 tbest3112 Added syntax highlighting2018/03/31 13:19 external edit 2018/04/17 17:51 tbest3112 Added syntax highlighting2018/03/31 13:19 external edit Line 3: Line 3: //by Jonathan Harston and Richard Russell, December 2007//\\ \\  The following short functions perform conversion of unsigned 32-bit integers to fixed-length strings, in hexadecimal (base 16), octal (base 8) and binary (base 2).\\ \\ //by Jonathan Harston and Richard Russell, December 2007//\\ \\  The following short functions perform conversion of unsigned 32-bit integers to fixed-length strings, in hexadecimal (base 16), octal (base 8) and binary (base 2).\\ \\ ===== Hexadecimal Output ===== ===== Hexadecimal Output ===== + REM Hexadecimal padded with zeros REM Hexadecimal padded with zeros DEF FNh0(A%,​N%):​LOCAL A\$ DEF FNh0(A%,​N%):​LOCAL A\$ Line 10: Line 11: DEF FNh(A%,​N%):​LOCAL A\$ DEF FNh(A%,​N%):​LOCAL A\$ REPEAT A\$=STR\$~(A% AND 15)+A\$:​A%=A% >>>​ 4:UNTIL A%=0:​=RIGHT\$(STRING\$(N%,"​ "​)+A\$,​N%) REPEAT A\$=STR\$~(A% AND 15)+A\$:​A%=A% >>>​ 4:UNTIL A%=0:​=RIGHT\$(STRING\$(N%,"​ "​)+A\$,​N%) + ​ **FNh0()** converts the supplied number to a fixed width hexadecimal string padded with zeros, **FNh()** converts the supplied number to a fixed width hexadecimal string padded with spaces. For example, **FNh0(10,​6)** returns **00000A**.\\ \\ **FNh0()** converts the supplied number to a fixed width hexadecimal string padded with zeros, **FNh()** converts the supplied number to a fixed width hexadecimal string padded with spaces. For example, **FNh0(10,​6)** returns **00000A**.\\ \\ ===== Octal Output ===== ===== Octal Output ===== + REM Octal padded with zeros REM Octal padded with zeros DEF FNo0(A%,​N%):​LOCAL A\$ DEF FNo0(A%,​N%):​LOCAL A\$ Line 19: Line 22: DEF FNo(A%,​N%):​LOCAL A\$ DEF FNo(A%,​N%):​LOCAL A\$ REPEAT A\$=STR\$(A% AND 7)+A\$:A%=A% >>>​ 3:UNTIL A%=0:​=RIGHT\$(STRING\$(N%,"​ "​)+A\$,​N%) REPEAT A\$=STR\$(A% AND 7)+A\$:A%=A% >>>​ 3:UNTIL A%=0:​=RIGHT\$(STRING\$(N%,"​ "​)+A\$,​N%) + ​ **FNo0()** converts the supplied number to a fixed width octal string padded with zeros, **FNo()** converts the supplied number to a fixed width octal string padded with spaces. For example, **FNo0(10,​6)** returns **000012**.\\ \\ **FNo0()** converts the supplied number to a fixed width octal string padded with zeros, **FNo()** converts the supplied number to a fixed width octal string padded with spaces. For example, **FNo0(10,​6)** returns **000012**.\\ \\ ===== Binary Output ===== ===== Binary Output ===== + REM Binary padded with zeros REM Binary padded with zeros DEF FNb0(A%,​N%):​LOCAL A\$ DEF FNb0(A%,​N%):​LOCAL A\$ Line 28: Line 33: DEF FNb(A%,​N%):​LOCAL A\$ DEF FNb(A%,​N%):​LOCAL A\$ REPEAT A\$=STR\$(A% AND 1)+A\$:A%=A% >>>​ 1:UNTIL A%=0:​=RIGHT\$(STRING\$(N%,"​ "​)+A\$,​N%) REPEAT A\$=STR\$(A% AND 1)+A\$:A%=A% >>>​ 1:UNTIL A%=0:​=RIGHT\$(STRING\$(N%,"​ "​)+A\$,​N%) + ​ **FNb0()** converts the supplied number to a fixed width binary string padded with zeros, **FNb()** converts the supplied number to a fixed width binary string padded with spaces. For example, **FNb0(10,​6)** returns **001010**.\\ \\ **FNb0()** converts the supplied number to a fixed width binary string padded with zeros, **FNb()** converts the supplied number to a fixed width binary string padded with spaces. For example, **FNb0(10,​6)** returns **001010**.\\ \\ ===== Decimal Output ===== ===== Decimal Output ===== Line 33: Line 39: ===== Note ===== ===== Note ===== These functions are deliberately written in the same style to show their similarities. Fixed-length hexadecimal and fixed-length decimal can be performed with the following shorter functions.\\ ​ These functions are deliberately written in the same style to show their similarities. Fixed-length hexadecimal and fixed-length decimal can be performed with the following shorter functions.\\ ​ + DEF FNh0(A%,​N%)=RIGHT\$("​0000000"​+STR\$~A%,​N%) :REM Hex padded with zeros DEF FNh0(A%,​N%)=RIGHT\$("​0000000"​+STR\$~A%,​N%) :REM Hex padded with zeros DEF FNh(A%,​N%)=RIGHT\$(" ​      "​+STR\$~A%,​N%) ​ :REM Hex padded with spaces DEF FNh(A%,​N%)=RIGHT\$(" ​      "​+STR\$~A%,​N%) ​ :REM Hex padded with spaces DEF FNd0(A%,​N%)=RIGHT\$("​00000000"​+STR\$A%,​N%) :REM Decimal padded with zeros DEF FNd0(A%,​N%)=RIGHT\$("​00000000"​+STR\$A%,​N%) :REM Decimal padded with zeros DEF FNd(A%,​N%)=RIGHT\$(" ​        "​+STR\$A%,​N%) :REM Decimal padded with spaces DEF FNd(A%,​N%)=RIGHT\$(" ​        "​+STR\$A%,​N%) :REM Decimal padded with spaces + ​ Additionally,​ these functions are tailored for //BBC BASIC for Windows// (and BASIC V on other platforms). They will not work on other versions of BBC BASIC. If you need code to work on other versions of BBC BASIC or need code that is version/​platform neutral, see other appropriate references, such as the [[http://​beebwiki.jonripley.com|BBC Micro Wiki]]. Additionally,​ these functions are tailored for //BBC BASIC for Windows// (and BASIC V on other platforms). They will not work on other versions of BBC BASIC. If you need code to work on other versions of BBC BASIC or need code that is version/​platform neutral, see other appropriate references, such as the [[http://​beebwiki.jonripley.com|BBC Micro Wiki]].
number_20conversion_20in_20basic.txt · Last modified: 2018/04/17 17:51 by tbest3112

### Page Tools 