Convert hex to dec?

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

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$) ?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Paul.

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
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

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
Use it with:

Code: Select all

decnum = hex2dec("FF")
MessageRequester("Result:",Str(decnum),0)
or

Code: Select all

MessageRequester("Result:",Str(hex2dec("FF")),0)
cya,
...Danilo



(registered PureBasic user)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

Thanks Danilo, that's perfect.

A quick question: what does Result << 4 do?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

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... :wink:

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
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.
"<<" is a Shift.

"10 * 16" is the same as "10 << 4",
but shift is faster.
I see... so you mean bit-shifting, I assume?

BTW, does anyone have a list of these undocumented commands?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

[quote]
"10 * 16" is the same as "10 .

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

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
Post Reply