Ähem...

Previous topic - Next topic

Marmor

is Glb really the only Basic without an hex$ or bin$ inline Function.
He Gernot come on  this is standard !
and dont say use your own Function  :D

MrTAToad

Is it really needed though ?

My routine (for hexadecimal) : http://www.glbasic.com/forum/index.php?topic=2859.msg20964#msg20964

Marmor

Yap !   :D 

Ok i know its not important and must not set to high priority and so on but why not implement this ?


matchy

decToHex is what I use and it's fine but need an update where the default length is max rather than trim.

MrTAToad

What do you mean by that - the maximum output size can be changed

matchy

I mean trimming leading hex zeros. I've had to ad code to do that to handle multiple lengths for example; 0xAB and 0xABCD as 0x0000AB and 0x00ABCD.

MrTAToad

Ah, right!

matchy

Righto, so update?  ;)

MrTAToad

Give this a try :

Code (glbasic) Select
FUNCTION decToHex$:value%,length%=4
LOCAL digit%
LOCAL temp%
LOCAL result$

IF length%<=0
RETURN "0"
ENDIF

result$=""
FOR digit%=length% TO 1 STEP -1
IF value%=0 THEN BREAK

temp%=MOD(value%,16)
IF temp%<10
result$=CHR$(temp%+48)+result$
ELSE
result$=CHR$((temp%-10)+65)+result$
ENDIF

value%=value%/16
NEXT

RETURN result$
ENDFUNCTION

Moru

Quote from: Marmor on 2012-Nov-30
is Glb really the only Basic without an hex$ or bin$ inline Function.
He Gernot come on  this is standard !
and dont say use your own Function  :D

Hmm, it can't be the only one. I specifically remember writing those functions as exercises in school when I was young.  =D

Kitty Hello

A hint. But I'll do this better. You can do any file operation with:
mem:/xxxxxx
Where xxxxx is a 4byte pointer to your memory. At that address the first 4 bytes are the lenght, then the data. It's awkard, but would work with loadsprite and such.

Marmor

But not with hex$ ill guess  :nana:


Kitty Hello

Search the forum for hex$. I'm sure it exists, too.

bigsofty

#13
As good TV cooks always seem to say, here's one I made earlier...

Code (glbasic) Select
FUNCTION Hex$:num%
LOCAL h$="0123456789ABCDEF", num$
REPEAT
num$=MID$( h$, bAND(num%,0xF) ,1)+num$
num%=ASR(num%,4)
UNTIL num%=0
RETURN num$
ENDFUNCTION


And a little edit of the one above provides a Bin$()...

Code (glbasic) Select
FUNCTION Bin$:num%
LOCAL B$="01", num$
REPEAT
num$=MID$( B$, bAND(num%,0x1) ,1)+num$
num%=ASR(num%,1)
UNTIL num%=0
RETURN num$
ENDFUNCTION
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)