Convert HEX to Dezimal ??

Everything else that doesn't fall into one of the other PB categories.
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 skypa.

Hi,

i need to convert a hexdezimal string, for example "6C" to a Dezimal Value. Any Ideas how to do this with PureBasic? (Inline ASM tipps are welcome, as long as they are copy&paste&runable :D )


thx in advance


skypa
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.

There is a code snippet on the PB Resources Site to do this called HEXDEC...
http://www.reelmediaproductions.com/pb

As well as on this forum, just do a search.
(it's been discussed before)
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 Schlowski.
Hi,

i need to convert a hexdezimal string, for example "6C" to a Dezimal Value. Any Ideas how to do this with PureBasic? (Inline ASM tipps are welcome, as long as they are copy&paste&runable :D )


thx in advance


skypa

Hi Skypa,

here a little "pure" PureBasic example of how to do it, it's not optimized and has no error-checking and will only convert upper case correct... But it's a beginning

Procedure.l Hex2Long (Hex.s)
Erg.l=0
For i.l=1 To Len(Hex)
c.l = Asc(Mid(Hex,i,1))
If c>64
c - 55
EndIf
If c>47
c - 48
EndIf
Erg = Erg << 4 + c
Next
ProcedureReturn Erg
EndProcedure

OpenConsole()
PrintN (Str(Hex2Long("6C")))
a.s = Input()
CloseConsole()

Schlowski
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 Schlowski.

Hey, cut and paste kills my indentation - hopfully you get the idea nevertheless.
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 skypa.

Thanks you guys, that was a big help :)

For later problems I'll use the searchengine before, sorry.


skypa
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.

> i need to convert a hexdezimal string, for example "6C" to a Dezimal Value.

Here's what I use, and it can accept mixed-case hex from 0 to FFFFFFF.
To use it, simply call it like so: dec.l=hex2dec(hex$)

Code: Select all

Procedure.l hex2dec(h$)
  ; h$ can be 0-FFFFFFF.
  h$=UCase(h$)
  For r=1 To Len(h$)
    d60
      d+Asc(a$)-55
    Else
      d+Asc(a$)-48
    EndIf
  Next
  ProcedureReturn d
EndProcedure

PB - Registered PureBasic Coder

Edited by - PB on 25 March 2002 19:43:08
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.

> Hey, cut and paste kills my indentation

Next time, surround your code examples with and .
Note: Use square braces, [ and ], and NOT as shown.


PB - Registered PureBasic Coder
Post Reply