number_20conversion_20in_20basic
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
number_20conversion_20in_20basic [2018/03/31 13:19] – external edit 127.0.0.1 | number_20conversion_20in_20basic [2024/01/05 00:21] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
=====Number Conversion in BASIC===== | =====Number Conversion in BASIC===== | ||
- | //by Jonathan Harston and Richard Russell, December 2007//\\ \\ | + | //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 ===== | ||
+ | <code bb4w> | ||
REM Hexadecimal padded with zeros | REM Hexadecimal padded with zeros | ||
DEF FNh0(A%, | DEF FNh0(A%, | ||
Line 10: | Line 14: | ||
DEF FNh(A%, | DEF FNh(A%, | ||
REPEAT A$=STR$~(A% AND 15)+A$: | REPEAT A$=STR$~(A% AND 15)+A$: | ||
- | **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, | + | </ |
+ | |||
+ | **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, | ||
===== Octal Output ===== | ===== Octal Output ===== | ||
+ | <code bb4w> | ||
REM Octal padded with zeros | REM Octal padded with zeros | ||
DEF FNo0(A%, | DEF FNo0(A%, | ||
Line 19: | Line 27: | ||
DEF FNo(A%, | DEF FNo(A%, | ||
REPEAT A$=STR$(A% AND 7)+A$:A%=A% >>> | REPEAT A$=STR$(A% AND 7)+A$:A%=A% >>> | ||
- | **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, | + | </ |
+ | |||
+ | **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, | ||
===== Binary Output ===== | ===== Binary Output ===== | ||
+ | <code bb4w> | ||
REM Binary padded with zeros | REM Binary padded with zeros | ||
DEF FNb0(A%, | DEF FNb0(A%, | ||
Line 28: | Line 40: | ||
DEF FNb(A%, | DEF FNb(A%, | ||
REPEAT A$=STR$(A% AND 1)+A$:A%=A% >>> | REPEAT A$=STR$(A% AND 1)+A$:A%=A% >>> | ||
- | **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, | + | </ |
+ | |||
+ | **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, | ||
===== Decimal Output ===== | ===== Decimal Output ===== | ||
- | For converting numbers to decimal strings refer to the **FNUSING** library supplied with //BBC BASIC for Windows// and documented [[http:// | + | For converting numbers to decimal strings refer to the **FNUSING** library supplied with //BBC BASIC for Windows// and documented [[http:// |
===== 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. |
+ | |||
+ | <code bb4w> | ||
DEF FNh0(A%, | DEF FNh0(A%, | ||
DEF FNh(A%, | DEF FNh(A%, | ||
DEF FNd0(A%, | DEF FNd0(A%, | ||
DEF FNd(A%, | DEF FNd(A%, | ||
+ | </ | ||
+ | |||
Additionally, | Additionally, |
number_20conversion_20in_20basic.1522502370.txt.gz · Last modified: 2024/01/05 00:17 (external edit)