I added more to the 'hex to decimal' program I wrote earlier, by making it accept 8 digits, up to 'FFFF FFFF'. When I enter '7FFF FFFF', I get 2,147,483,647, but goes negative with '8FFF FFFF'. I tried using UINT64, but then the '8FFF FFFF' gives me, 18,446,744,071,562, which I know is completely wrong, as it should be 2,415,919,103. I tried to declare the variables at the start of the program, with the UINT64, and then only 0 to 15, will show. The other columns stay at zeroes. 'FFFF FFFF' is 4,294,967,295, in decimal. How can I achieve this?
Thanks for any help, in advance..
whitenite1
What am I missing?
uint a,b
a=0xffffffff
?"0xffffffff = ",a
b=0x8fffffff
?"0x8fffffff = ",b
LarryMc
Hi,
Does this do the job ..
openconsole
cls
def a$:string
declare Hex2Dec(n1 As String) As double
a$ = "ffffffff"
print "Hex ",a$," Dec ", Hex2Dec(a$)
do:until inkey$<>""
closeconsole
end
sub Hex2Dec(n1 As String) As double
Dim nl1 As int
Dim nGVal As double
Dim nSteper As int
Dim nCount As int
Dim x As int
Dim nVal As int
Dim Stepit As int
Dim hVal As String
nl1 = Len(n1)
nGVal = 0
nSteper = 16
nCount = 1
For x = nl1 To 1 Step -1
hVal = UCase$(Mid$(n1, x, 1))
Select hVal
Case "A"
nVal = 10
Case "B"
nVal = 11
Case "C"
nVal = 12
Case "D"
nVal = 13
Case "E"
nVal = 14
Case "F"
nVal = 15
Default
nVal = Val(hVal)
EndSelect
Stepit = (nSteper ^ (nCount - 1))
nGVal = nGVal + nVal * Stepit
nCount = nCount + 1
Next x
return nGVal
best wishes, :)
Graham
The best I can tell:
from reading the Constants and Literals and Variables sections in the help file;
and from the search results for UINT64 in the forums I've concluded:
the only way you can set the value directly of a INT64/UINT64 is with hex values (0xFFFFFFFFq/0xFFFFFFFFuq).
I did find that I could shift left an uINT into a UINT64
with some print statements I found that I finally got the number correct but the STR$ function always treated it like a signed INT.
So, looking as the STR$() source code, I modified it and made my own that works for uint64.
So, your modified program below appears to be giving the correct results.
This is all due to the fact we're trying to use 64 bit values on a 32 bit machine.
Maybe the problem will go away when LarryS gets the 64bit compiler working.
$use "PBGroupLM.lib"
$include "PBGroupLM.inc"
Declare "kernel32.dll",Beep(dwFreq As int, dwDuration As int),int
' dwFreq - frequency in hertz
' dwDuration - duration in milliseconds
DECLARE TLMSTR$(num as UINT64),HEAP
const PBG1 = 1
const PBG2 = 2
const PBG3 = 3
const PBG4 = 4
const PBG5 = 5
const PBG6 = 6
const PBG7 = 7
const PBG8 = 8
const OnOff = 9
int x,y,Beep
uint64 a,b,c,d,e,f,g,h,hex
dialog d1
CREATEDIALOG d1,10, 0, 447, 375,0x80CA0880,0,"Hexidecimal Program",&d1_handler
CONTROL d1,@STATIC,"",175,15,140,16,0x5000010B,60
CreatePBGroupLM(d1, 4,40,PBG1 )
CreatePBGroupLM(d1, 58,40,PBG2 )
CreatePBGroupLM(d1,112,40,PBG3 )
CreatePBGroupLM(d1,166,40,PBG4 )
CreatePBGroupLM(d1,230,40,PBG5 )
CreatePBGroupLM(d1,284,40,PBG6 )
CreatePBGroupLM(d1,338,40,PBG7 )
CreatePBGroupLM(d1,392,40,PBG8 )
CreatePBGroupLM(d1,382,342,OnOff )
DOMODAL d1
end
SUB d1_handler
centerwindow d1
SELECT @MESSAGE
CASE @IDINITDIALOG
SETFONT d1, "Courier", 6, 800, 0x00FF0000,OnOff
ConfigPBGroupLM(d1,PBGROUP_VERT,16,40,18,PBG1 )
ConfigPBGroupLM(d1,PBGROUP_VERT,16,40,18,PBG2 )
ConfigPBGroupLM(d1,PBGROUP_VERT,16,40,18,PBG3 )
ConfigPBGroupLM(d1,PBGROUP_VERT,16,40,18,PBG4 )
ConfigPBGroupLM(d1,PBGROUP_VERT,16,40,18,PBG5 )
ConfigPBGroupLM(d1,PBGROUP_VERT,16,40,18,PBG6 )
ConfigPBGroupLM(d1,PBGROUP_VERT,16,40,18,PBG7 )
ConfigPBGroupLM(d1,PBGROUP_VERT,16,40,18,PBG8 )
ConfigPBGroupLM(d1,PBGROUP_VERT,1,49,19,OnOff )
y=48
for x=0 to 16
ConfigPBGButtonLM(d1,chr$(y+x),chr$(y+x),RGB(0,0,0),RGB(18,180,255), RGB(0,0,0),RGB(18,222,229),PBG1,16-x )
if x = 9 then y=55
next x
y=48
for x=0 to 16
ConfigPBGButtonLM(d1,chr$(y+x),chr$(y+x),RGB(0,0,0),RGB(18,180,255), RGB(0,0,0),RGB(18,222,229),PBG2,16-x )
if x = 9 then y=55
next x
y=48
for x=0 to 16
ConfigPBGButtonLM(d1,chr$(y+x),chr$(y+x),RGB(0,0,0),RGB(18,180,255), RGB(0,0,0),RGB(18,222,229),PBG3,16-x )
if x = 9 then y=55
next x
y=48
for x=0 to 16
ConfigPBGButtonLM(d1,chr$(y+x),chr$(y+x),RGB(0,0,0),RGB(18,180,255), RGB(0,0,0),RGB(18,222,229),PBG4,16-x )
if x = 9 then y=55
next x
y=48
for x=0 to 16
ConfigPBGButtonLM(d1,chr$(y+x),chr$(y+x),RGB(0,0,0),RGB(18,180,255), RGB(0,0,0),RGB(18,222,229),PBG5,16-x )
if x = 9 then y=55
next x
y=48
for x=0 to 16
ConfigPBGButtonLM(d1,chr$(y+x),chr$(y+x),RGB(0,0,0),RGB(18,180,255), RGB(0,0,0),RGB(18,222,229),PBG6,16-x )
if x = 9 then y=55
next x
y=48
for x=0 to 16
ConfigPBGButtonLM(d1,chr$(y+x),chr$(y+x),RGB(0,0,0),RGB(18,180,255), RGB(0,0,0),RGB(18,222,229),PBG7,16-x )
if x = 9 then y=55
next x
y=48
for x=0 to 16
ConfigPBGButtonLM(d1,chr$(y+x),chr$(y+x),RGB(0,0,0),RGB(18,180,255), RGB(0,0,0),RGB(18,222,229),PBG8,16-x )
if x = 9 then y=55
next x
ConfigPBGButtonLM(d1,"Sound Off","Sound On",RGB(0,0,0),RGB(205,130,144), RGB(0,0,0),RGB(130,244,104),OnOff,1 )
SetStatePBGButtonLM(d1,16,1,PBG1)
SetStatePBGButtonLM(d1,16,1,PBG2)
SetStatePBGButtonLM(d1,16,1,PBG3)
SetStatePBGButtonLM(d1,16,1,PBG4)
SetStatePBGButtonLM(d1,16,1,PBG5)
SetStatePBGButtonLM(d1,16,1,PBG6)
SetStatePBGButtonLM(d1,16,1,PBG7)
SetStatePBGButtonLM(d1,16,1,PBG8)
SetStatePBGButtonLM(d1, 0,0,OnOff)
CASE @IDCONTROL
SELECT @CONTROLID
case PBG1
a = (16-GetStatePBGButtonLM(d1,PBG1))<<12
case PBG2
b = (16-GetStatePBGButtonLM(d1,PBG2))<<8
case PBG3
c = (16-GetStatePBGButtonLM(d1,PBG3))<<4
case PBG4
d = (16-GetStatePBGButtonLM(d1,PBG4))
case PBG5
e=(16-GetStatePBGButtonLM(d1,PBG5))<<12
case PBG6
f = (16-GetStatePBGButtonLM(d1,PBG6))<<8
case PBG7
g = (16-GetStatePBGButtonLM(d1,PBG7))<<4
case PBG8
h = (16-GetStatePBGButtonLM(d1,PBG8))
case OnOff
Beep = GetStatePBGButtonLM(d1,OnOff)
endselect
if Beep then beep(1400,120)
hex = e+f+g+h
uint64 hex1 = ((a+b+c+d)<<16) | hex
SETCONTROLTEXT(d1,60,"Decimal = "+tlmstr$(hex1)+space$(20))
CASE @IDCLOSEWINDOW
CLOSEDIALOG d1,@IDOK
ENDSELECT
RETURN 0
ENDSUB
DECLARE CDECL EXTERN _sprintf(buffer as string,format as STRING,...),INT
SUB TLMSTR$(num as UINT64)
DEF mem as pointer
mem = AllocHeap(512)
_sprintf(#<STRING>mem," %u",num)
RETURN #<STRING>mem
ENDSUB
LarryMc
Yes, I always check out things like this in console mode first.
It works OK in CBasic .. :) .. but apparently not in IWB ..
Graham