?
Solved

Coverting String Format to BCD representation in little endian format

Posted on 2004-09-24
3
Medium Priority
?
592 Views
Last Modified: 2013-12-26
Hi,
I am new to programming and I need some help here. I need to translate the PIN number entered by the user into data for an API call. I need to convert the input string into 4-byte BCD representation, in little-endian format (for example, 0x12345678 in binary is passed to the library as 0x78654312) before passing the PIN to a library function.

Your help is very much appreciated.

Joe
0
Comment
Question by:pogt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
3 Comments
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12141848
Try this function - I think I remember what BCD looks like. Oh for a proper unsigned integer.

Function Pin2BCD(pin As String) As Long
    Dim i As Long
    Dim s As String
    Dim l As Long

    For i = 7 To 1 Step -2
        s = s & Mid$(pin, i, 2)
    Next

    For i = 1 To Len(s)
    On Error GoTo Overflow
        l = l * &H10& Or (CLng(Mid$(s, i, 1)) And &HF&)
    Next
   
    Pin2BCD = l And &HFFFFFFFF
    Exit Function

Overflow:
    'Not it all
    l = (Not ((Not l) And &HFFFFFFF)) * &H10
    l = l Or (CLng(Mid$(s, i, 1)) And &HF&)
    Resume Next
End Function
0
 
LVL 10

Accepted Solution

by:
fds_fatboy earned 750 total points
ID: 12141898
Here it is again cleaned up slightly with test harness code ready to drop into a standard form to test.

Option Explicit

Function Pin2BCD(pin As String) As Long
    Dim i As Long
    Dim s As String
    Dim l As Long

    For i = 7 To 1 Step -2
        s = s & Mid$(pin, i, 2)
    Next

    For i = 1 To Len(s)
    On Error GoTo Overflow
        l = l * &H10& Or (CLng(Mid$(s, i, 1)) And &HF&)
    Next
   
    Pin2BCD = l
    Exit Function

Overflow:
    'Not it all
    l = (Not ((Not l) And &HFFFFFFF)) * &H10 Or (CLng(Mid$(s, i, 1)) And &HF&)
    Resume Next
End Function

Private Sub Form_Load()
    Dim x
    x = Pin2BCD("99999999")
    MsgBox x & vbCrLf & Hex$(x)
   
    x = Pin2BCD("12345678")
    MsgBox x & vbCrLf & Hex$(x)
   
    x = Pin2BCD("87654321")
    MsgBox x & vbCrLf & Hex$(x)
   
    x = Pin2BCD("00000000")
    MsgBox x & vbCrLf & Hex$(x)
End Sub
0
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12149685
Grade B? You're a hard marker ;-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month13 days, 8 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question