Skip to content

BinToStrinf Function #709

Open
Open
@em00k

Description

Please can the following be added to both hex.bas which prints a binary representation of a byte, eg 8 would return "00001000"

function FASTCALL BinToString(num as ubyte) as String
	asm
	PROC
	push namespace core
	LOCAL END_CHAR
	LOCAL DIGIT
	LOCAL charloop
	LOCAL bitisset
	LOCAL nobitset
	push af   ; save ubyte 
	ld bc,10
	call __MEM_ALLOC
	ld a, h
	or l
	pop bc 
	ld c,b 
	ret z	; NO MEMORY
	
	push hl	; Saves String ptr
	ld (hl), 8
	inc hl
	ld (hl), 0
	inc hl  ; 8 chars string length

	; c holds out entry 8 bit value, b number of bits 

	ld b,8
charloop:
	call DIGIT
	djnz charloop 
	pop hl	; Recovers string ptr
	ret
	
DIGIT:
	ld a,c
	bit 7,a
	jr nz,bitisset 
	ld a,'0'
	jr nobitset
bitisset:
	ld a,'1'
nobitset:	
	
END_CHAR:
	ld (hl), a
	inc hl
	ld a,c 
	sla c 
	ret
	ENDP
	pop namespace
	end asm
end function

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions