forum.vdsworld.com Forum Index forum.vdsworld.com
Visit VDSWORLD.com
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


VDS 5 Binary to hex conversion

 
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> General Help
View previous topic :: View next topic  
Author Message
Hack
Newbie


Joined: 08 Jan 2012
Posts: 3

PostPosted: Tue Jan 10, 2012 1:43 am    Post subject: VDS 5 Binary to hex conversion Reply with quote

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 Very Happy
Back to top
View user's profile Send private message
uvedese
Contributor
Contributor


Joined: 21 Jan 2006
Posts: 169
Location: Spain

PostPosted: Tue Jan 10, 2012 11:22 am    Post subject: Reply with quote

Hello Hack

The @hex() function needs a decimal argument, not binary
Back to top
View user's profile Send private message Visit poster's website
Hack
Newbie


Joined: 08 Jan 2012
Posts: 3

PostPosted: Tue Jan 10, 2012 12:39 pm    Post subject: Binary to Hex conversion Math Reply with quote

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
View user's profile Send private message
Aslan
Valued Contributor
Valued Contributor


Joined: 31 May 2001
Posts: 589
Location: Memphis, TN USA

PostPosted: Tue Jan 10, 2012 8:27 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
Hack
Newbie


Joined: 08 Jan 2012
Posts: 3

PostPosted: Thu Jan 12, 2012 2:02 am    Post subject: Thanks Reply with quote

Very Happy
That was the right answer.
Back to top
View user's profile Send private message
bornsoft
Contributor
Contributor


Joined: 19 Feb 2009
Posts: 113
Location: Germany

PostPosted: Thu Jan 19, 2012 1:48 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Aslan
Valued Contributor
Valued Contributor


Joined: 31 May 2001
Posts: 589
Location: Memphis, TN USA

PostPosted: Thu Jan 19, 2012 1:51 am    Post subject: Reply with quote

Nice Wink
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> General Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum

Twitter@vdsworld       RSS

Powered by phpBB © 2001, 2005 phpBB Group