GLBasic forum

Main forum => GLBasic - en => Topic started by: fuzzy70 on 2011-Nov-22

Title: Hex$ & Bin
Post by: fuzzy70 on 2011-Nov-22
Is there a way to print hex & binary on the screen or do I have to roll my own function to do it. The latter is not a problem but if it is already built in somewhere there is no point re-inventing the wheel so to speak  =D

Lee
Title: Re: Hex$ & Bin
Post by: MrTAToad on 2011-Nov-22
There is a routine for hexadecimal somewhere - but not for binary...
Title: Re: Hex$ & Bin
Post by: fuzzy70 on 2011-Nov-22
I find the hex one posted in the forum thanks  :D. It is mainly for the project I am working on at the moment & being able to view the data in binary say in the debug window helps visualize if the routine is doing what it should be.

Lee
Title: Re: Hex$ & Bin
Post by: Slydog on 2011-Nov-22
I found the following in my string library.
I *think* it formats an integer as binary!

Code (glbasic) Select

FUNCTION FormatBit$: value%
LOCAL r$ = ""
LOCAL bits%
WHILE value > 0
// Is last bit set? (00000001)
IF bAND(value, 1) > 0
r$ = "1" + r$
ELSE
r$ = "0" + r$
ENDIF
value = ASR(value, 1)
WEND
// Optional for formatting to a full 8 bits
bits = MOD(LEN(r$), 8)
IF (bits > 0) OR (LEN(r$) = 0) THEN r$ = String_PadLeft$(r$, "0", 8 - bits)
RETURN r$
ENDFUNCTION

FUNCTION String_PadLeft$: text$, char$, quantity%
LOCAL ix%
FOR ix = 1 TO quantity
text$ = char$ + text$
NEXT
RETURN text$
ENDFUNCTION
Title: Re: Hex$ & Bin
Post by: fuzzy70 on 2011-Nov-23
Quote from: Slydog on 2011-Nov-22
I found the following in my string library.
I *think* it formats an integer as binary!

Yep, just tested it & indeed it does  =D . I just need to pad out the start so it shows 16/24/32 bit etc (purely so the columns line up for easier comparison) which is very easy to add.

Thank you Slydog  :good:

Lee
Title: Re: Hex$ & Bin
Post by: Slydog on 2011-Nov-23
QuoteI just need to pad out the start so it shows 16/24/32 bit
I edited my post above to do this.
Title: Re: Hex$ & Bin
Post by: fuzzy70 on 2011-Nov-23
Thank you again Slydog  :booze:.

I think I will have a scan through my old Blitz library's & convert the odd bits which I could make use of in GLB. Also will look at some Blitz commands I used to use which have no equivalent in GLB like the BIN/HEX ones. There probably will not be many but would rather have them done & out the way.

I use the IDE called Ideal when using Blitz & am really missing the code snippets feature of that IDE  :'(

Lee