Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binary Convertion into Decimal Convertion help

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi!!!

I Have an application in Visual Basic 6.0, the point is that I'm reading a Variable with a scanner and a proximity card (A card that register a number read by a scanner).

For example:

I read my card with the scanner and the number that store in the variable could be 10101010 but I need the right conversion to decimal that could be for example 1236.

Is there any object, activeX or something that it can help me with this? Or you know where can I find something about this?

I'll appreciatte it.... thanks!!!

MR
 
I just wrote this real quick- I did it off a button click and an input box- but it will take a binary number and convert it to decimal. It will handle numbers w/in the limits of long/double type numbers. It should be easy to adjust this to fit your needs.

Code:
Private Sub Command1_Click()
Dim placeholder() As Double
Dim binaryin, binarylength, counter As Long
Dim doubleout As Double

doubleout = 0

binaryin = InputBox("Enter Binary Number")

binarylength = Len(binaryin)
ReDim placeholder(binarylength - 1)

For counter = 0 To binarylength - 1
  placeholder(counter) = Mid(binaryin, counter + 1, 1)
  If placeholder(counter) = 1 Then
    doubleout = doubleout + 2 ^ ((binarylength - 1) - counter)
  End If
Next counter

MsgBox "Result in decimal " & doubleout

End Sub
[\code]

  Binary numbers are base 2.  So (reading right to left) the first digit is equal to 2^0.  The second is equal to 2^1 and so on down the line.
 
Hi!!!

That is a good Idea, thanks so much!!!

Just one more question......

I made a test about this with this binary number from Windows Hyperterminal:

00000101001100101100100001 and the Decimal result has to be:

26000

I don't know why I receive other number: 1362721

Do you have any idea!!!

Thanks a lot!

CMRM

 
Hmmmmmmmmmmm,

Briefly, Theologian's conversion is more-or-less correct. A simple inspection of the power of two table will easily show that the 26 "Bits" provided in the example are FAR morte than 26000. I suspect that the device is sending more than the VALUE and you do not know or specify what the other part of the binary string is or -more importantly- WHERE in the string / series it is located. Conversly, you COULD specify the start and stop bits of the part to convert.

Another BRIEF inspection shows that the Value bist Cannot be a contigious set from the left or right, as neither can ever accumulate to the value specified.

Theologian's hint that the value may be a non- integer data type is interesting - but he fails to ask (and you fail to suggest) a decimal placement. Since the number of bits is 26, it is not pratical to rationalize the valus as representing any of the several quasi-standard numerical representations (IEE, Microsoft ...), so basically, without additional information from "CesarR", I'm just not interested enough to try the several thousand possabilities for the conversion to return 26000. MichaelRed
mred@att.net

There is never time to do it right but there is always time to do it over
 
I'm not sure I understand much of the above post- and apparently I don't really understand your problem.

The binary number:
101001100101100100001 (which is your number w/out all the leading zeroes) converts to the decimal number-

1362721- you can verify this on a calculator.

If this is not simply a conversion from binary to decimal as I guess MichaelRed is saying- then I imagine he is correct that much more information is needed to properly convert your results from the hardware.

He also gives me more credit than is due. I just used a double as I believe that it allows for larger numbers than integer or long. Though I could be wrong. I knocked that out pretty quickly this morning.

Sorry I could not be of more assistance.
 
Theologian,

Using a Double does yield a larger "range" for the value. ?Without going into the mechanics, it also is a 'Floating Point' value. Since there is no indication that the number is a representation of a 'floating point' value, the larger range does not provide any capability. The binary string of 32 "1"'s is = (2 ^ 31) -1 which fits into the "Long" data type quite nicely. Sinc we are provided with only 26 bits (mixed bag of "1"'s and "0"'s), the (LONG) Integer data type will easily accomodate the maximum possible value.

MichaelRed
mred@att.net

There is never time to do it right but there is always time to do it over
 
I find the conversion from 00000101001100101100100001 to 26000 very confusing - the final digit in the binary string is a 1 which means that the conversion to decimal should always produce an odd number.

Could you explain the problem in more detail. Where did the number 26000 come from?

Chaz
 
Looking at the binary data a little more closely:

2600: 110010110010000
orig: 101001100101100100001

It looks like only the bottom 16 binary digits are significant, although you are also losing the bottom digit.

Can you provide more information about the datastream? I have a feeling that you are very close...

Chaz
 
Looking at the binary data a little more closely:

26000: 110010110010000
orig : 101001100101100100001

It looks like only the bottom 16 binary digits are significant, although you are also losing the bottom digit.

Can you provide more information about the datastream? I have a feeling that you are very close...

Chaz
 
sorry about the double post - typed in 2600 instead of 26000 by mistake.
 
Another thought - if the bottom digit and top digits are not used the function

((decimalValue \ 2) And 32767

where decimalValue is the number from Theologians calculation gives you the required 26000.

Chaz
 
Thanks a lot to all of you!!!

You are really good in this programming.....

Explaining a little bit more, the reason is that I'm using a Lector (Scanner) called MiniProx from HID Corporation, This is used for proximity cards (Cards read by this scanner).

I'm building a Visual Basic Program that needs to read this cards from this lector....

I Used the HyperTerminal from Windows in Com2 with these parameters:

bits per second: 9600
Dta bits: 8
Parity: none
Stop Bits: 1
Flow Controler: Hardware

And it works, I read the Proximity card that in decimal is 26000 but it is read in Binary like
00000101001100101100100001......

Do you have any idea?

Thanks so much for your help...

CMRM
 
Just a thought, I used to work with digital Truck scales and they produced similiar results. Micheal Red is correct, you need to know where the data starts and ends.... A stream of ones and zero's is useless unless you know where to break them up....

Try using vb to write a file in binary. Then try to retrieve your information from the file without knowing where one piece of data starts and another ends... Troy Williams B.Eng.
fenris@hotmail.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top