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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert Binary to Signed Integer value 3

Status
Not open for further replies.

egstatus

Programmer
Apr 14, 2005
143
US
Hi all,
I am working with a binary file. The byte at position 16 of the binary file has the number of records in the file in a binary format.

So for example, if the binary representation of byte 16 in the file is 11111100, this would translate to 790780 records. 00011101 is equal to 29 records.

How do I know the equivalent? As of now I can open each file and go to the end of the file and that gives me the number of records. It is a tedious job becuase there are days that I need to find the count for upto 500 files.

How do I convert the binary representation of 11111100 to 790780 in vb.

Is this possible?

Thanks for any help.

Ed
 
Something does not compute here.

"00011101" does indeed equate to 29 but "11111100" is 252 ... not 790780. You would need "11000001000011111100" to represent that.

A single byte cannot represent a value greater than 255.

Is it possible that the bytes before byte 16 are also involved in the count?
 
I guess it could be possible that the bytes before or after are involved.

I am not sure, how can I tell?

I checked the prior 3 btyes:
Byte 13 = 00000000
Byte 14 = 00000000
Byte 15 = 00000001

and the next 3 bytes
Byte 17 = 00010000
Byte 18 = 00001100
Byte 19 = 00000000

Can you tell if these bytes have to do with the 790780 result?


Thanks again.

Ed
 
As I said, you need "11000001000011111100" ...

Breaking that down on byte boundaries
[tt]
00001100 [red]<--- Byte 18[/red]
00010000 [red]<--- Byte 17[/red]
11111100 [red]<--- Byte 16[/red]
[/tt]
Looks like your number is in bytes 16-18 with the low-order bytes first.

Here's a very simple-minded binary converter for you
Code:
Public Function ConvBin(Bin As String) As Long
    Dim n                           As Integer
    Dim s As String
    s = Replace(Bin, " ", "")
    For n = Len(s) To 1 Step -1
        ConvBin = ConvBin + Val(Mid(s, n, 1)) * 2 ^ (Len(s) - n)
    Next
End Function
 
Thanks very much. That does it.

One more question and pardon my ignorance on this. How do I read the "11111100" in byte 16 and store it in a variable?
I was using and editor to read the content of the bytes.

I know how to open the file in vb, I just don't know how to read the binary representation and store in a variable.

Thanks again for all your help.

Ed
 
And just to help you understand binary better:

1 BYTE = 8 BITS : 1 BIT = (ONE OR ZERO) , you seem to know this already

EACH BIT POSITION IS MULTIPLIED BY THE FOLLOWING:


x128 x64 x32 x16 x8 x4 x2 x1

SO LETS PLUG IN A BYTE

00101101

GOING FROM LEFT TO RIGHT

0 X 128 = 0
0 X 64 = 0
1 X 32 = 32
0 X 16 = 0
1 X 8 = 8
1 X 4 = 4
0 X 2 = 0
1 X 1 = 1

NOW ADD THE RESULTS
32 + 8 + 4 + 1 = 45

BINARY(00101101) = DECIMAL(45)
 
Probably the easiest way is to ignore the binary representation and treat the thing as a character string. Something along the lines of
Code:
Dim S    As String
Dim f    As Scripting.TextStream
Dim Recs As Long
With New Scripting.FileSystemObject
   Set f = .OpenTextFile("c:\testfile.txt", ForReading)
   S = f.Read(18)
   Recs = Asc(Mid(S,16,1)) + _
          Asc(Mid(S,17,1)) * 256 + _
          Asc(Mid(S,18,1)) * 65536
End With

You will need a reference to Microsoft Scripting Runtime to use the FileSystemObject and TextStreams.
 
I'd hazard that byte 19 might also be involved ... and that makes it a long. And we can read that directly, no nasty conversions necessary:
Code:
Dim result As Long
Dim hFile as long
hFile=FreeFile
Open "c:\temp\out.dat" For Binary As hFile
Get hFile, 16, result
Close hFile
 
Thanks for both replies.

I tryed both but none returned the disered results.

The one Golom sugested gives me a run time error '6' overflow.

The one from Strongm returned '202439680'. I cheked byte 19 and it has 00000000 as is binary value, I gues it could be part of the calculation as well.

However if i put bytes 19, 18, 17, and 16 in the following string "00000000 00001100 00010000 11111100" and use the function [ConvBin] that Golom wrote then I get the value of 790780 which is the correct number of records in the data file.

How can I achive what I am tryng to acomplish?

Thanks in advance for your help.

Ed
 
Strongm

I checked the value that the OP is getting from your code and, converting to binary
Code:
      00001100 00010000 11111100 00000000
Byte     [red]18       17       16       19[/red]
is 202439680.

Can you think of any reason why Byte 19 would be at the end rather than the beginning?

Big-endian? Little-endian?
 
Golom,
Is there a way to read a byte, or its binary representation and store it in a variable?

If I can do this then I can concatenate the content of those bytes and pass them to the function you wrote and achive the result.

Thanks

Ed

 
... Is there a way to read a byte ...

That's why I suggested a character string. Each character in a string is one byte.

In the little blob of code I gave you, change the longs to Doubles and, before the calculation, insert a line
Code:
Debug.Print Asc(Mid$(S, 16, 1)), Asc(Mid$(S, 17, 1)), Asc(Mid$(S, 18, 1))
To see what you are getting.
 
Ok - it now becomes clear that you were counting from 0 rather than 1, so simply change my code to


Dim result As Long
Dim hFile as long
hFile=FreeFile
Open "c:\temp\out.dat" For Binary As hFile
Get hFile, 15, result
Close hFile
 
Well, Changing it to 15 did not work, but changing it to 17 did.
Don't know exactly why, but this works perfect. I tryed other files to verify that the number of records, and it returned the right count on all files I tried

Thank you both Strongm and Golom for your help.

Ed
 
Nice solution strongm. I often forget about using binary I/O with simple scalar types.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top