Restored from previous forum. Originally posted by PB.
Hi all,
What's the easiest way to convert a two-byte hex number (say FF) to decimal?
Fred, can a command be added to do this, such as: decnum=Dec(hex$) ?
Convert hex to dec?
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Paul.
Here's a little procedure I use...
It uses El_Choni's MathExtras library to get the IPow command and works with any length of HEX number (as long as it can return a 32bit decimal number)
Edited by - paul on 24 December 2001 01:13:19
Here's a little procedure I use...
Code: Select all
Procedure.l hexdec(hexstr.s)
cnt=Len(hexstr)-1
For tmp=1 To Len(hexstr)
nul.s=UCase(Mid(hexstr,tmp,1))
If nul="0":nu=0:EndIf
If nul="1":nu=1:EndIf
If nul="2":nu=2:EndIf
If nul="3":nu=3:EndIf
If nul="4":nu=4:EndIf
If nul="5":nu=5:EndIf
If nul="6":nu=6:EndIf
If nul="7":nu=7:EndIf
If nul="8":nu=8:EndIf
If nul="9":nu=9:EndIf
If nul="A":nu=10:EndIf
If nul="B":nu=11:EndIf
If nul="C":nu=12:EndIf
If nul="D":nu=13:EndIf
If nul="E":nu=14:EndIf
If nul="F":nu=15:EndIf
nu1=IPow(16,cnt)
nu2=nu*nu1
newdec=newdec+nu2
cnt=cnt-1
Next
ProcedureReturn newdec
EndProcedure
MessageRequester("",Str(hexdec("ff")),0)
It uses El_Choni's MathExtras library to get the IPow command and works with any length of HEX number (as long as it can return a 32bit decimal number)
Edited by - paul on 24 December 2001 01:13:19
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Danilo.
Thats only for a 2-Digit-HexString:
Use it with:
or
cya,
...Danilo
(registered PureBasic user)
Thats only for a 2-Digit-HexString:
Code: Select all
Procedure.l hex2dec(String.s)
If Len(String) > 2:Beep_(1000,1000):ProcedureReturn -1:EndIf
If Asc(String) > 60 : Result.l = Asc(String) - 55
Else : Result.l = Asc(String) - 48 : EndIf
Result 60 : Result.l + Asc(String) - 55
Else : Result.l + Asc(String) - 48 : EndIf
ProcedureReturn Result.l
EndProcedure
Code: Select all
decnum = hex2dec("FF")
MessageRequester("Result:",Str(decnum),0)
Code: Select all
MessageRequester("Result:",Str(hex2dec("FF")),0)
...Danilo
(registered PureBasic user)
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Danilo.
"> 1 = / 2
a >> 2 = / 4
a >> 4 = / 16
...and so on
Addition:
"Shift" has something to do with
the binary number.
Example:
Decimal "10" is Hex "0A" and
Binary "00001010".
If you "shift" the binary number
to the left side (4 times = *16),
you get the binary number "10100000",
and thats 160 decimal ("A0" hex).
10 * 16 = 160, so its right...
cya,
...Danilo
(registered PureBasic user)
Edited by - Danilo on 25 December 2001 22:51:09
Edited by - Danilo on 25 December 2001 22:52:36
"> 1 = / 2
a >> 2 = / 4
a >> 4 = / 16
...and so on
Addition:
"Shift" has something to do with
the binary number.
Example:
Decimal "10" is Hex "0A" and
Binary "00001010".
If you "shift" the binary number
to the left side (4 times = *16),
you get the binary number "10100000",
and thats 160 decimal ("A0" hex).
10 * 16 = 160, so its right...

cya,
...Danilo
(registered PureBasic user)
Edited by - Danilo on 25 December 2001 22:51:09
Edited by - Danilo on 25 December 2001 22:52:36
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Paul.
Wayne1 just updated his extended string library (StringLibraryEx) and it contains a new HexToDec command.
You might want to check it out...
http://www.reelmediaproductions.com/pb
Wayne1 just updated his extended string library (StringLibraryEx) and it contains a new HexToDec command.
You might want to check it out...
http://www.reelmediaproductions.com/pb