# Thread: decimal to binary conversion?

1. ## decimal to binary conversion?

Does anyone know of a way in QTP to convert a decimal to it's binary equivalent?

I'm doing this because I have 7 fields that are going to either be true or false; I plan to start a loop (0-127) and take the decimal value of the loop's position, convert it to a 7 digit binary number and depending on the character in each position send the true(1) or false(0) value to the application.

The only other way that I've figured out is to read the binary value from an data file into an array, but this seems bulky, complicated and unnecessary.

I've checked the QTP help files and this forum but haven't seen any commands to do this. Any assistance would be greatly appreciated.

2. ## Re: decimal to binary conversion?

You might want to try this website... not sure how helpful it will be though as you have to register to get any sort of absolute info.... www.tek-tips.com
Just select the VBScript link.

You could also try the MSDN (Microsoft) site

HTH

3. ## Re: decimal to binary conversion?

There is no VBScript function to convert decemal to Binary, however you can use the scientific calculator in your QTP script to do the conversion for you or you wait until tomorrow and I will script a VB function for you.

HTH

4. ## Re: decimal to binary conversion?

Hi,
This is customized for 7 digit binary string. And assuming you will use only +ve numbers. </font><blockquote><font size="1" face="Verdana, Arial, Helvetica">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">
myNum = 127
str = &quot;&quot;
Do until myNum &lt; 2
remainder = myNum mod 2
str = remainder &amp; str
myNum = int(myNum/2)
Loop
myNum = myNum &amp; str
Do until len(myNum)&gt;6
myNum = &quot;0&quot; &amp; myNum
Loop
msgbox myNum</pre><hr /></blockquote><font size="2" face="Verdana, Arial, Helvetica">

5. ## Re: decimal to binary conversion?

Hi, You can mask on the number without converting it. Something like:
For y = 1 to 127
if (y AND 64) &gt; 0 then ... true
if (y AND 32) &gt; 0 then ... true
if (y AND 16) &gt; 0 then ... true
if (y AND 8) &gt; 0 then ... true
if (y AND 4) &gt; 0 then ... true
if (y AND 2) &gt; 0 then ... true
if (y AND 1)&gt; 0 then ... true
Next
hth,
MJP.

6. ## Re: decimal to binary conversion?

I think I figured it out - I googled 'vbscript decimal binary' and got a snippet that seemed to work:

Function Dec2Bin(DecVal)
strResult = ""
intValue = DecVal
intExp = 65536
while intExp &gt;= 1
if intValue &gt;= intExp then
intValue = intValue - intExp
strResult = strResult & "1"
else
strResult = strResult & "0"
end if
intExp = intExp / 2
wend
Dec2Bin = strResult
End Function

After that, I'm calling

varX = right(Dec2Bin(PassLoop),7)

and acting on the mid(varX,[whichever_position],1) value.

7. ## Re: decimal to binary conversion?

here is your function :
</font><blockquote><font size="1" face="Verdana, Arial, Helvetica">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">Function Dbin(MyDecimal)
Dim a(32) ' This Array will work for 32 bit binary number
arraySize = 0
Result = MyDecimal
If Result = 0 Then
Dbin = 0
Exit Function
End If
' Convert from D to B and keep results in Array
While Result &gt; 0
a(arraySize)= Result Mod 2
Result = int(Result/2)
arraySize = arraySize + 1
Wend
'Reverse The Array
str = &quot;&quot;
For i= arraysize-1 To 0 Step -1
str = str&amp;a(i)
Next
Dbin = str
End Function </pre><hr /></blockquote><font size="2" face="Verdana, Arial, Helvetica">Usage: msgbox Dbin(9995)
let me know if you need anything else
ciao

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

vBulletin Optimisation provided by vB Optimise v2.6.0 Beta 4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
User Alert System provided by Advanced User Tagging v3.0.9 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Questions / Answers Form provided by vBAnswers (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
vBNominatevBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Feedback Buttons provided by Advanced Post Thanks / Like (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Username Changing provided by Username Change (Free) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
BetaSoft Inc.
Digital Point modules: Sphinx-based search
All times are GMT -8. The time now is 06:31 AM.