kann manchmal nützlich sein, wenn man mit Bits rumspielt
im optionalen Parameter p_bytes kann man mitgeben, wie groß die zurückgelieferte Binärzahl ist 1 Byte = 8 Zeichen, 2 Byte = 16 Zeichen etc. Voreinstellung ist 4 Byte für 32 Bit-Zahlen
PRINT Bin$(2594585588), 0, 0
SHOWSCREEN
KEYWAIT
// ------------------------------------------------------------- //
// --- BIN$ ---
// paramerters: p_num number to convert in binary format
// p_bytes number of bytes, on 32 bit CPU 4 byte
// is maximum
// ------------------------------------------------------------- //
FUNCTION Bin$: p_num, p_bytes = 4
LOCAL l_length = p_bytes * 8
LOCAL l_counter
LOCAL l_result$
FOR l_counter = l_length - 1 TO 0 STEP -1
IF bAND(p_num,POW(2, l_counter))
l_result$ = l_result$ + "1"
ELSE
l_result$ = l_result$ + "0"
ENDIF
NEXT
RETURN l_result$
ENDFUNCTION // BIN$
@Quentin, cool Danke.
Genau das habe ich gerade gesucht.
:happy:
Und jetzt bitte eine Fkt, die das Rückwärts auch noch macht! :D
hin und her, das ist nicht schwer ;)
number = 2578
binnumber$ = Bin$(number, 2)
backnumber = Bin2Dec(binnumber$)
PRINT "Zahl :" + number, 0, 0
PRINT "Binär :" + binnumber$, 0, 30
PRINT "Und zurück :" + backnumber, 0, 60
SHOWSCREEN
KEYWAIT
// ------------------------------------------------------------- //
// --- BIN$ ---
// paramerters: p_num number to convert in binary format
// p_bytes number of bytes, on 32 bit CPU 4 byte
// is maximum
// ------------------------------------------------------------- //
FUNCTION Bin$: p_num, p_bytes = 4
LOCAL l_length = p_bytes * 8
LOCAL l_counter
LOCAL l_result$
FOR l_counter = l_length - 1 TO 0 STEP -1
IF bAND(p_num,POW(2, l_counter))
l_result$ = l_result$ + "1"
ELSE
l_result$ = l_result$ + "0"
ENDIF
NEXT
RETURN l_result$
ENDFUNCTION // BIN$
// ------------------------------------------------------------- //
// --- BIN2DEC ---
// ------------------------------------------------------------- //
FUNCTION Bin2Dec: binnum$
LOCAL l_result
LOCAL l_counter
LOCAL l_length
l_length = LEN(binnum$) - 1
FOR l_counter = l_length TO 0 STEP -1
IF MID$(binnum$, l_counter, 1) = 1
INC l_result, POW(2, l_length - l_counter)
ENDIF
NEXT
RETURN l_result
ENDFUNCTION // BIN2DEC
Thanks, this might be handy. :good: