Frage deutsch
~~~~~~~~~~~~~~
Wie konvertiere ich Zahlen von einer Zahlenbasis zu einer anderen?
Question English
~~~~~~~~~~~~~~
How to convert a number from one basis to another ?
Antwort 1
~~~~~~~~
[ von Thomas Antoni, 26.6.2002 ]
Die von QB direkt unterstützten Zahlensysteme sind Oktal, Dezimal und
Hexadezimal. Für die entsprechenden Umwandlungen stehen in QBasic die Befehle
OCT$, VAL und HEX zur Verfügung:
oktal$ = OCT$(dez%) 'Dezimal => Oktal-Wandlung
dez% = VAL("&O" + oktal$) 'Oktal => Dezimal-Wandlung
hexadez$ = HEX$(dez%) 'Dezimal => Hexadezimal-Wandlung
dez% = VAL("&H" + hexadez$) 'Hexadezimal =>
Dezimal-Wandlung
Weitere Informationen findest Du im LEXIKON unter -> Hexadezimalzahl und -> Oktalzahl .
Für alle übrigen Zahlensysteme/Umwandlungsfälle musst Du spezielle FUNCTIONs
verwenden.
Um für alle denkbaren Konvertierungen gewappnet zu sein, brauchst Du einen
"Base Converter", der Zahlen zwischen den verschiedenen Zahlenbasen (Binär,
Oktal, Hex, Dezimal) umrechnen kann.
Auf www.qbasic.de findest Du unter "QBasic
-> Download -> Mathe" den sehr guten, universellen Zahlen-Converter
NSYS.BAS von Dieter Folger. NSYS wandelt Zahlen, die in einer
beliebigen Zahlenbasis von 2 bis 16 vorliegen, in Zahlen einer beliebigen
anderen Zahlenbasis 2...16 um.
*** Dutz- statt Dezimalsystem!!
Meiner Meinung nach ist das heute üblich Dezimalsystem denkbar ungünstig. 10
lässt sich ja nur durch 5 und durch 2 teilen. Viel günstiger wäre doch das
Zwölfersystem, bei dem nach der 11 die Zahl "Dutz" käme. 38 wäre im
Zwölfersystem dann "drei-dutz-zwei" und 148 hieße "Gross-vier" (1 Gross = 100duz
= 144dez) . Die 12 ist durch 2, 3, 4, und 6 teilbar, bietet also viel mehr
Möglichkeiten als die blöde "10". Der Bruch 1/3 ließe sich im Dutzsystem bequem
als 0,4 (=4/12) darstellen statt als 0,33333333... . Wenn wir 12 statt 10 Finger
hätten, dann hätte sich bestimmt das Dutzsystem durchgesetzt, und wir wären viel
glücklicher dran.
Die Zahlen im Dutzsystem lauten wie folgt:
Dezimal 1 2 3 4 5 6 7 8 9 10 11 12 13 ... 23 24 25 ... 143 144 145
Dutz 1 2 3 4 5 6 7 8 9 A B 10 11 ... 1B 20 21 ... BB 100 101
Doch nun Spaß beiseite!
Von Andreas Meile ( http://dreael.catty.ch/Deutsch/BASIC-Knowhow-Ecke ) gibt es die beiden folgenden
Zahlenkonverter-Programme zur Konvertierung von Dezimalzahlen in beliebige
Zahlensysteme und umgekehrt:
*** Konvertierung von Dezimalzahlen zu einer beliebigen
Zahlenbasis
'*****************************************************************
' Dec2Any.bas = Zahlenkonvertierung Dez. -> beliebige Basis
' ===========
' Routine die eine Dezimalzahl in jede andere Zahlenbasis
' zwischen 1 und 35 umrechnet
'
' (c) Andreas Meile
' http://dreael.catty.ch/Deutsch/BASIC-Knowhow-Ecke
'*****************************************************************
CLS
INPUT "Eingabe-Zahl (Dezimal)......................: ", Zahl%
PRINT "Zahlenbasis der Ausgabe-Zahl"
PRINT " (2-Binaer, 8-Octal, 16-Hexadezimal ...) ...: ";
INPUT "", Basis%
NeueZahl$ = ""
Ziffern$ = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
DO
a% = Zahl% MOD Basis%
NeueZahl$ = MID$(Ziffern$, a% + 1, 1) + NeueZahl$
Zahl% = INT(Zahl% / Basis%)
LOOP UNTIL Zahl% = 0
PRINT
PRINT "Ausgabe-Zahl im"; Basis%;
PRINT "-er-System................: "; NeueZahl$
Das obige Programm steht im Verzeichnis Progs\ zur Verfügung sowie online
unter www.antonis.de/faq/progs/dec2any.bas .
*** Konvertierung von einer beliebigen Zahlenbasis in eine
Dezimalzahl
'***************************************************************
' Any2Dec.bas = Zahlenkonvertierung beliebige Basis -> Dez.
' ============
' Wandlung einer in einer beliebigen Zahlenbasis zwischen 2
' und 35 vorliegenden Eingabe-Zahl in eine Dezimalzahl.
' (die Buchstaben in der Eingabezahl muessen in Grossbuchstaben
' geschrieben werden (z.B. A...F bei Hexa-Zahlen)
'
' (c)Andreas Meile
' http://dreael.catty.ch/Deutsch/BASIC-Knowhow-Ecke
'***************************************************************
CLS
INPUT "Gib die Eingabe-Zahl ein ................: "; Zahl$
PRINT "Gib die Zahlenbasis der Eingabe-Zahl ein "
INPUT "...(2=binaer, 8=oktal, 16=hex) ..........: "; basis%
z& = 0&
FOR i% = 1 TO LEN(Zahl$)
ziffer% = ASC(MID$(Zahl$, i%, 1)) - ASC("0")
IF ziffer% >= 10 THEN
ziffer% = ziffer% - ASC("A") + ASC("0") + 10
END IF
z& = CLNG(basis%) * z& + CLNG(ziffer%)
NEXT i%
PRINT "Ausgabe-Zahl .............................: "; z&
Das obige Programm steht im Verzeichnis Progs\ zur Verfügung sowie online
unter www.antonis.de/faq/progs/any2dec.bas .
Answer 2
~~~~~~~~~~
[ from the QB-Forum-FAQ at www.network54.com/Forum/ ]
*** Question
How to convert numbers of any base to binary?
*** Answer
Summary of Digital Number Systems and Converting Any Base to Decimal
B = Base ' Number base, binary = 2, octal = 8, decimal = 10, hexadecimal
= 16, etc. Any number can be used as a base.
D = Digit ' a single digit in any number base in any position.
P = Position or Place value ' The first position on the right of an
integer of any base is position #0. Moving to the left, The position numbers
increment by 1.
N = Number ' any base, includes all its digits in proper place positions
with 0 as a placeholder.
R = number of digits in any given integer, including zeros used as
placeholders.
Any number in a number system can be represented with each digit multiplied
by its base raised to the power of its place value position.
General Example:
If w, x, y, and z represent digits in a 4-digit number of any base, then the
value of the number is:
w * B ^ 3 + x * B ^ 2 + y * B ^ 1 + z * B ^ 0
Decimal Example:
7593 = 7 * 10 ^ 3 + 5 * 2 ^ 10 + 9 * 1 ^ 10 + 9 * 0 ^ 10
= 7000 + 500 + 90 + 3
Binary Example:
1101 = 1 * 2 ^ 3 + 1 * 2 ^ 2 + 0 * 2 ^ 1 + 1 * 2 ^ 0
= b1000 + b100 + b00 + b1 ' binary value
= d8 + d4 + d0 + d1 = d13 ' decimal value
Octal Example:
235 = 2 * 8 ^ 2 + 3 * 8 ^ 1 + 5 * 8 ^ 0
= o200 + o30 + o5 ' octal value
= d128 + d24 + d5 = d157 ' decimal value
Hexadecimal Example:
9D = 9 * 16 ^ 1 + D * 16 ^ 0
= h90 + hD ' hexadecimal value
= d144 + d13 = d157 ' decimal value
The following program will convert an integer number of any base (up to
whatever base uses digit Z) to its decimal equivalent. It places each digit of a
number in an array with the LSD (Least Significant Digit - the one in the
rightmost position) - in the 0th element of the array. C is used as a decrement
counter to place the digits in the correct array order starting with 0.
INPUT "number: ", digit$
INPUT "base: ", B
R = LEN(digit$)
DIM D(R)
C = R
FOR P = 1 TO R
A$ = MID$(digit$, C, 1)
IF ASC(A$) >=65 AND ASC(A$) <=70 THEN
D(P - 1) = ASC(A$) - 55
ELSE
D(P - 1) = VAL(A$)
END IF
C = C - 1
NEXT P
'
FOR P = 0 TO R - 1
S = D(P) * B ^ P
N = N + S
NEXT P
'
PRINT N
SLEEP
SYSTEM
This is a skeletal program - user inputs will still need to be validated not
to exceed value limit of base digit number (or letter symbol). Some variable
types may also need to be declared as LONG or DOUBLE. I'm really not sure about
how digits beyond base 16 are represented (I assumed the letters up to Z, but
there must be more), and I'm not sure about fractions or negatives, so I will
leave it up to the next respondent to this post to elaborate on my program.
[ The QBasic-MonsterFAQ --- Start Page: www.antonis.de/faq ]