View previous topic :: View next topic |
Author |
Message |
Hack Newbie
Joined: 08 Jan 2012 Posts: 3
|
Posted: Tue Jan 10, 2012 1:43 am Post subject: VDS 5 Binary to hex conversion |
|
|
Greetings all,
I have a variable that contains 1111 when I use the @Hex I get 0479 instead of F am I missing something or do I need a DLL in order to do the proper math?
Thanks |
|
Back to top |
|
|
uvedese Contributor
Joined: 21 Jan 2006 Posts: 169 Location: Spain
|
Posted: Tue Jan 10, 2012 11:22 am Post subject: |
|
|
Hello Hack
The @hex() function needs a decimal argument, not binary |
|
Back to top |
|
|
Hack Newbie
Joined: 08 Jan 2012 Posts: 3
|
Posted: Tue Jan 10, 2012 12:39 pm Post subject: Binary to Hex conversion Math |
|
|
Ok, so I have a variable that contains 1111 how would I go about converting it to hex?
i.e. 1000 = 8 0001 = 1 1111 = F
Thanks, Hack |
|
Back to top |
|
|
Aslan Valued Contributor
Joined: 31 May 2001 Posts: 589 Location: Memphis, TN USA
|
Posted: Tue Jan 10, 2012 8:27 pm Post subject: |
|
|
You will need to convert the binary value to decimal first then use that result to apply to the @HEX() function.
example using some of SnarlingSheeps old Bit wise code.
Code: | #DEFINE FUNCTION,BIN2DEC
%1 = @input(Please enter a binary value)
If @ok()
info BIN = %1@cr()DEC = @BIN2DEC(%1)@cr()HEX = @HEX(@BIN2DEC(%1))
end
exit
:BIN2DEC
%%B2DResult = 0
%%B2DX = 1
%%B2DStrValue = %1
REPEAT
%%B2DCharValue = @POS(@UPPER(@SUBSTR(%%B2DStrValue,%%B2DX,%%B2DX)),"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
#return -1 if character is not supported by base
If @GREATER(%%B2DCharValue,2)
%%B2DResult = -1
%%B2DX = @SUCC(@LEN(%%B2DStrValue))
END
%%B2DResult = @FADD(@FMUL(%%B2DResult,2),@FSUB(%%B2DcharValue,1))
%%B2DX = @SUCC(%%B2DX)
UNTIL @GREATER(%%B2DX,@LEN(%%B2DStrValue))
exit %%B2DResult |
|
|
Back to top |
|
|
Hack Newbie
Joined: 08 Jan 2012 Posts: 3
|
Posted: Thu Jan 12, 2012 2:02 am Post subject: Thanks |
|
|
That was the right answer. |
|
Back to top |
|
|
bornsoft Contributor
Joined: 19 Feb 2009 Posts: 113 Location: Germany
|
Posted: Thu Jan 19, 2012 1:48 am Post subject: |
|
|
Here is my way:
I gladly confess, it's derived from SnarlingSheeps code that Aslan mentioned before, but it's much less code an no global variables are used.
Code: |
#DEFINE Function,bsB2D
%%binary = 1111
%%decimal = @bsB2D(%%binary)
%%hex = @hex(%%decimal)
info %%hex
exit
:bsB2D
%R = 0
%x = 1
repeat
%C = @pos(@substr(%1,%x),"01")
if @greater(%C,2)
%R = -1
%x = @succ(@len(%1))
end
%R = @fadd(@fmul(%R,2),@fsub(%C,1))
%x = @succ(%x)
until @greater(%x,@len(%1))
exit %R
|
Greetings.
bornSoft
. |
|
Back to top |
|
|
Aslan Valued Contributor
Joined: 31 May 2001 Posts: 589 Location: Memphis, TN USA
|
Posted: Thu Jan 19, 2012 1:51 am Post subject: |
|
|
Nice |
|
Back to top |
|
|
|