Little question
Blitz3D Forums/Blitz3D Programming/Little question
| ||
N%=0 H$="01020304" Ok, I have an >int< variable N, and I have a >string< variable H. H holds an hexadecimal value, and I want N to have the int value of the hexadecimal value contained in H (did I say "value" :) What can I do? |
| ||
I tought this was a challenge, so I came up with this quick-and-dirty solution for you (only works with lowercase, so if you make a function out of it, make it convert the string to lowercase first) :N%=0 H$="e0ee" for i=1 to len(h$) h_part$=Mid(h$,i%,1) select (h_part$) case "0" val=0 case "1" val=1 case "2" val=2 case "3" val=3 case "4" val=4 case "5" val=5 case "6" val=6 case "7" val=7 case "8" val=8 case "9" val=9 case "a" val=10 case "b" val=11 case "c" val=12 case "d" val=13 case "e" val=14 case "f" val=15 end select val2=16^(len(h)-i)*val ; some debug info : ;print (len(h)-i)+" "+val+" "+val2 n=n+val2 next print n |
| ||
A few alternatives mentioned in this thread This one looked quite elegant : Function hex2dec(hexin$) Local c, dec, hexval$ = "0123456789ABCDEF" For c=1 To Len(hexin$) dec = (dec Shl 4) Or (Instr(hexval$, Upper$(Mid$(hexin$, c, 1))) - 1) Next Return dec End Function |
| ||
Ahhhhh! :) Thanks you two ! I'm having a look now ... Paolo. |
| ||
if only the $FFFFFFFF code worked in strings :( |
| ||
This one looked quite elegant : You're right - that is a lot more elegant, not to mention about 10x as fast.Oh well, it was fun writing that conversion, took me about 5 minutes :) |
| ||
Please next time use a more descriptive title. Well not a big problem, of course. |