Page 2 of 2

Posted: Fri May 06, 2005 8:12 am
by Xombie
Yeah, kind of...

Run this against your procedure:

Code: Select all

*HoldText.l
*HoldText = AllocateMemory(1024)
x_hextomem(*HoldText, "$3056306E")
Debug " ---- WORD "
Debug PeekW(*HoldText)
Debug PeekW(*HoldText + 2)
Debug " ---- WORD Hex"
Debug Hex(PeekW(*HoldText))
Debug Hex(PeekW(*HoldText + 2))
FreeMemory(*HoldText)
The results should be
---- WORD
22064
28208
---- WORD Hex
5630
6E30
I'm still learning about memory and such. The WORD hex values are reversed. 5630 when it should be 3056, etc... How can I fix that so it returns 3056? I'm thinking it's something really obvious but I don't have much experience with this part yet. $5630 will return 22064 in decimal while $3056 returns the proper 12374 in decimal.

Posted: Fri May 06, 2005 6:40 pm
by Xombie
Ahah! I got it figured out. Little did Psychophanta know that he *already* answered my question a while back :D

Code: Select all

Procedure.l HexToMemory(inHex.s)
   ; Original Code by blueznl:  
   ; Endian swapping routine by Psychophanta:  http://forums.purebasic.com/english/viewtopic.php?t=8541
   Protected a.l, b.l, p.l, c.l, l.l, d.w
   Protected *HoldMemory.l
   ; 
   p = @inHex
   c = 0 
   l = Len(inHex)
   ;
   *HoldMemory = AllocateMemory(l * 2)
   ; Allocate the memory needed.
   If Left(inHex,1) = "$" : p = p + 1 : EndIf 
   ; 
   While c + 1 < l 
      a = PeekB(p) - 48 
      If a > 10 : a = a - 7 : EndIf 
      b = PeekB(p + 1) - 48 
      If b > 10 : b = b - 7 : EndIf
      PokeB(*HoldMemory + q, a * 16 + b)
      If q & 1
         d = PeekW(*HoldMemory + q - 1)
         !ROR word[esp + 24], 8 
         ; inHex.s is [esp].  The next declared variable will be [esp + 4].  Every variable after that
         ; is base [esp + x] where x is the total size of the previous variables.
         PokeW(*HoldMemory + q - 1, d)
      EndIf
      p = p+2 
      q = q+1 
      c = c+2 
   Wend
   ;
   ProcedureReturn *HoldMemory
   ; And return the pointer to this string.
EndProcedure
So now it works like I need it to. Thanks a lot, blueznl and Psychophanta!