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

Int to Binary 2

Status
Not open for further replies.

kermitforney

Technical User
Mar 15, 2005
374
US
Have an issue trying to convert Int to Binary, reversing the result, then binary to decimal. So . . . 1(int) to 13(int).
Not familiar with VBA though. :eek:(

Basically I have a text box with an Invoice Number within . .
txtbox = Invoice#1802750
I have another textbox next to this one that I would like to have display the conversion. So . . .
txtbox1 = Invoice#1802750 textbox2 = 24088977

1802750=> Int2Binary=> Reverse Binary=> Binary2int txtbox1 textbox2

Any suggestions would be great . .thanks!!!
 




You are mixing numeric conversion with ascii conversion.

You have the TEXT value
[tt]
I n v o i c e # 1 8 0 2 7 5 0
073 110 118 111 105 099 101 035 049 056 048 050 055 053 048
[/tt]
the invouce NUMBER is not a NUMBER at all. Its a STRING.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Sorry about that skip, Invoice# was the label for the textbox.
I only want to convert the number not the sting Invoice#.
 



and what is the business case for this exersize?

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
The higher up's would like for me to apply a "special code" to our store invoices. This code would be used to make sure a store was visited. Upon completion our merchandiser will need to write this "special code" onto a close out sheet when they have completed their job. This "special code" that is placed on the close out will prove that the store was visited as it should have been.

So to make it simple :eek:) a colleague felt that we should convert the already present Invoice number into the "special code". This way when we receive the close out sheet with the code on it, we can convert it back and if it does not match the invoice number, then we know something is up.


So another technical example:

Invoice # Special Code
--------- ------------
1234567 14845501

Decimal2Binary=>TrimLeadingZero's=>ReverseString=>Binary2Decimal
 




Your "SpecialCode" is not binary, though. It's still decimal

Use HEX. There's a Hex funciton in VBA and here's a conversion the other way...
Code:
Function Hex2Dec(nH As String)
    Dim i, s
    For i = Len(nH) - 1 To 0 Step -1
        s = Mid(nH, Len(nH) - i, 1)
        Select Case s
            Case "A" To "F"
                s = Asc(s) - 55
        End Select
        Hex2Dec = Hex2Dec + 16 ^ i * s
    Next
End Function

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Well the conversion that we created in VB.Net converts decimal to binary, trims leading zero's, then reverses the string before the last conversion which is binary to Decimal.

So:
Step 1
Invoice#
--------
1234567
Step 2
Binary Conversion (Decimal to Binary)
-----------------
100101101011010000111
Step 3
Trim Leading Zero's(doesn't apply here)then Reverse Binary
---------------------------------------
111000010110101101001
Step 4
Binary to Decimal (Special Code)
--------------------------------
1846633

I apologize if I have you pulling out your hair Skip! :eek:D
Thanks for all of your help, it is greatly appreciated.
 
This should convert your "binary string' back into an integer for you.....

Code:
        Dim StartingValue As String = Me.TextBox1.Text   ' Binary string
        Dim Multiple As Int32 = 1                        ' Multiple of 2 to work with
        Dim SpecialCode As Int32 = 0                     ' Start our special code at 0
        For i As Int32 = (StartingValue.Length - 1) To 0 Step -1   ' Staring from the last character, work backwards
            SpecialCode += Convert.ToInt32(StartingValue.Substring(i, 1)) * Multiple   ' Multiply the value of the 
                                                                                       ' character time the multiple 
                                                                                       ' and add it to the current 
                                                                                       ' value of our special code
            Multiple *= 2                                ' Increase the multiple by a factor of 2
        Next                                             ' Go to the next character

        Me.TextBox2.Text = SpecialCode.ToString          'Put the Special Code into a textbox to see

Any questions, let me know.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
Hmmm.....thought I was in a different forum.

That is VB.Net code, but it should be pretty close for Access VBA. If you can't figure it out, let me know and I will convert it for you....

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 

So:
Step 1
Invoice#
--------
1234567
Step 2
Binary Conversion (Decimal to Binary)
-----------------
100101101011010000111
Step 3
Trim Leading Zero's(doesn't apply here)then Reverse Binary
---------------------------------------
111000010110101101001
Step 4
Binary to Decimal (Special Code)
--------------------------------
1846633
So what's the question?

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Skip: I need to be able to apply this conversion in VBA within Access. Invoice# is inside of a textbox and the Special code textbox is created based on the entry in the Invoice textbox.

So . .. How would I code this in VBA?
 




Check this out
Code:
Function Dec2Bin(n As Long, Optional bReverse = False) As String
    Dim m
    m = n
    Do
        If bReverse Then
            Dec2Bin = Dec2Bin & m Mod 2
        Else
            Dec2Bin = m Mod 2 & Dec2Bin
        End If
        m = Int(m / 2)
    Loop Until m <= 0
End Function
Function Bin2Dec(s As String, Optional bReverse = False) As Long
    Dim i, st
    For i = Len(s) - 1 To 0 Step -1
        If bReverse Then
            st = Mid(s, i + 1, 1)
        Else
            st = Mid(s, Len(s) - i, 1)
        End If
        Bin2Dec = Bin2Dec + 2 ^ i * st
    Next
End Function


Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Thanks, Skip!

Just one question, What does this mean,
"Dec2Bin = Dec2Bin & m Mod 2"
 




Code:
Dec2Bin = Dec2Bin & m Mod 2
concatenate the value in Dec2Bin with the remainder of m/2 and assign to Dec2Bin. The reminder of any integer divided by 2 will be either 0 or 1.

BTW, if you did not figure it out, by passing TRUE as the second argument, the order of the return value is reversed. The default is FALSE, no reverse.

Also, you previously stated, "Trim Leading Zero's". When you REVERSE the binary STRING, trimming ANYTHING, fundamentally destroys any chance of reconstructing the original value, as you supposedly want to do.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Skip, I have gotten the two functions to work, but they are not working properly.

Functions:
Code:
Public Static Function fncInt2Bin(n As Long, Optional bReverse = False) As String
    Dim m
    Dim Dec2Bin As Long
    m = n
    Do
        If bReverse Then
            Dec2Bin = Dec2Bin & m Mod 2
        Else
            Dec2Bin = m Mod 2 & Dec2Bin
        End If
        m = Int(m / 2)
    Loop Until m <= 0
    
    fncInt2Bin = Dec2Bin
End Function

Public Static Function fncBin2Dec(s As String, Optional bReverse = False) As Long
    Dim i, st
    Dim Bin2Dec As Long
    For i = Len(s) - 1 To 0 Step -1
        If bReverse Then
            st = Mid(s, i + 1, 1)
        Else
            st = Mid(s, Len(s) - i, 1)
        End If
        Bin2Dec = Bin2Dec + 2 ^ i * st
    Next
End Function

I am using this procedure to call them:
Code:
Private Sub PageHeaderSection_Format(Cancel As Integer, FormatCount As Integer)

Dim InvoiceID As Long 'As int32
Dim Int2Bin As String
Dim ControlNumber As Long

'.Text is only available if the control has focus
InvoiceID = Me.txtFauxInvoiceID
Int2Bin = fncInt2Bin(InvoiceID)
ControlNumber = fncBin2Dec(Int2Bin)

Me.txtControlNumber = ControlNumber
'Me.txtControlNumber = "hi"

End Sub

Any suggestions?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top