Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Fast way to convert from decimal to binary...
Message
From
08/05/2006 16:59:01
 
 
To
08/05/2006 16:26:23
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01120159
Message ID:
01120176
Views:
15
This message has been marked as a message which has helped to the initial question of the thread.
>I know that with TRANSFORM I can change a decimal value to its hexadecimal equivalent.
>
>Are there easy ways to convert from binary to hexadecimal and from hexadecimal to binary?
FUNCTION HexBinary(tcVal)
LOCAL nChars, nVal, x, cRetVal
cRetVal = ""
IF TYPE("tcVal")="C"
  DO CASE
    CASE UPPER(tcVal)="0X"	&& To Binary from Hex
      nVal = VAL(tcVal)
      cRetVal = ""
      FOR x=31 TO 0 STEP -1
        cRetVal = cRetVal + IIF(BITTEST(nVal,x),"1","0")
      ENDFOR
    OTHERWISE 				&& To Hex
      nChars = LEN(ALLTRIM(tcVal))
      nVal = 0
      FOR x=nChars-1 TO 0 STEP -1
        nVal = IIF(SUBSTR(tcVal,nChars-x,1)="1",BITSET(nVal,x),nVal)
      ENDFOR
      cRetVal = TRANSFORM(nVal,"@0")
  ENDCASE
ENDIF
RETURN cRetVal
? hexbinary("0xDD0D")
00000000000000001101110100001101

? hexbinary("00000000000000001101110100001101")
0x000000000000DD0D
Fred
Microsoft Visual FoxPro MVP

foxcentral.net
Previous
Reply
Map
View

Click here to load this message in the networking platform