WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Numbers

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Decimal to Hex conversion

 Keywords:  Hex Decimal conversion xHex

This UDF is nice for Decimal to Hex conversions when one does not wish the additional baggage of the WILX extender.

Use the StrFixLeft function to pad the hex string with zeros if a particular string size is desired.


#DefineFunction Dec2Hex(Dec)
   IsZero=@TRUE
   str="0123456789ABCDEF"
   hex=""
   
   for x=7 to 0 by -1
       nibble= (dec >> (x*4)) & 15
       if nibble==0 && IsZero==@TRUE then continue
       IsZero=@FALSE
       hex=strcat(hex,Strsub(str,nibble+1,1))
   next
   return(hex)
#EndFunction


;Example Usage
a="2000"
b=Dec2Hex(a)
Message(a,b)

;Pad to 8 places
b=strfixleft(b,0,8)
Message(a,b)


If you need to convert a number larger than 2147483647 to hex,
#DefineFunction Dec2Hex(num)
   AddExtender("WWHUG34I.DLL")
   msvcrt = DllLoad("msvcrt.dll")
   two32 = huge_Multiply(4,2 ** 30)
   buffsize = 1000
   buff = BinaryAlloc(buffsize)
   If num=="0" Then Return num
   _fmt = ""
   _nums = ""
   While num!="0"
      _x = ItemExtract(1,huge_Divide(num,two32),".")
      _fmt = StrCat("%%08X",_fmt)
      _nums = StrCat(",long:",huge_Subtract(num,huge_Multiply(_x,two32)),_nums)
      num = _x
   EndWhile
   BinaryEodSet(buff,DllCallCDecl(msvcrt,long:"sprintf",lpbinary:buff,lpstr:_fmt%_nums%))
   Hex = BinaryPeekStr(buff,0,buffsize)
   BinaryFree(buff)
   DllFree(msvcrt)
   Return Hex
#EndFunction

Dec = "999999999999"
Hex = Dec2Hex(Dec)
Message(Dec,Hex)

Article ID:   W14747
Filename:   Decimal to Hex conversion.txt
File Created: 2010:05:18:12:35:56
Last Updated: 2010:05:18:12:35:56