Go Premium for a chance to win a PS4. Enter to Win

x
Solved

# Coverting String Format to BCD representation in little endian format

Posted on 2004-09-24
Medium Priority
611 Views
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
Question by:pogt
• 3

LVL 10

Expert Comment

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

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

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

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

## Featured Post

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
###### Suggested Courses
Course of the Month6 days, 7 hours left to enroll