Hex$ & Bin

Previous topic - Next topic

fuzzy70

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
"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)

MrTAToad

There is a routine for hexadecimal somewhere - but not for binary...

fuzzy70

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
"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)

Slydog

#3
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
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

fuzzy70

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
"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)

Slydog

QuoteI just need to pad out the start so it shows 16/24/32 bit
I edited my post above to do this.
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

fuzzy70

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
"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)