[RESOLVED] text box to byte array to function parameter how to??
guys!!
let me show you the code first:
Code:
ReType = 0
Cm = &H53
Pm = &H33
St1 = 0
St0 = 0
CLA = &H0
INS = &HB0
P1 = &H0
P2 = 0
Dim TxArray() As Byte
TxArray = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text)
Dim TxArrayLen As Byte = TxArray.Length
TxData = {CLA, INS, P1, &H10, TxArrayLen, TxArray}
TxDataLen = TxData.Length
RxDataLen = 0
i = CRT288KDLL.DllClass.RS232_ExeCommand(Hdle, Cm, Pm, TxDataLen, TxData, ReType, St1, St0, RxDataLen, RxData)
Now here it is... I am writing to a smartcard and there I'm taking the "To be written" from a textbox (textbox1). After I take the value from the text box, I have to construct a specific format of data for using that with the API. Thus the thing for MISHU should come as:
TxData = {CLA, INS, P1, &H10, TxArrayLen, &H4d, &H49, &H53, &H48, &H55}
any guideline for this plz...
Mishu~
Re: text box to byte array to function parameter how to??
A way to construct could be like this
Code:
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim ReType As Byte = 0
Dim Cm As Byte = &H53
Dim Pm As Byte = &H33
Dim St1 As Byte = 0
Dim St0 As Byte = 0
Dim CLA As Byte = &H0
Dim INS As Byte = &HB0
Dim P1 As Byte = &H0
Dim P2 As Byte = 0
Dim TxArray() As Byte
TxArray = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text)
Dim TxArrayLen As Byte = TxArray.Length
Dim TxData((TxArrayLen - 1) + 5) As Byte
TxData(0) = CLA
TxData(1) = INS
TxData(2) = P1
TxData(3) = &H10
TxData(4) = TxArrayLen
Array.Copy(TxArray, 0, TxData, 5, TxArrayLen)
'Visualize the array
For j = 0 To TxData.Length - 1
Debug.Write(TxData(j).ToString("X2") & " ")
Next
Debug.WriteLine("")
'Dim TxDataLen As Byte = TxData.Length
'Dim RxDataLen As Byte = 0
'i = CRT288KDLL.DllClass.RS232_ExeCommand(Hdle, Cm, Pm, TxDataLen, TxData, ReType, St1, St0, RxDataLen, RxData)
End Sub
Note: If TxArrayLen exceeds 255 then you must preserve more bytes in TxData array to store it properly.
Re: text box to byte array to function parameter how to??
is there are other way? bcoz the TxArray will be taken from some other app and will be a paste (an encrypted string that needs to be written in card)...
And on pressing a button for "WRITE" it will actually write... via a lot of process like INIT CARD, PASS CODE AUTH, then WRITE... and each time the same variables will actually be used as that's part of API. So hands are stuck...
BTW, can I take this approach??
https://stackoverflow.com/questions/...one-byte-array
Re: text box to byte array to function parameter how to??
Quote:
Originally Posted by
aq_mishu
is there are other way? bcoz the TxArray will be taken from some other app and will be a paste (an encrypted string that needs to be written in card)...
And on pressing a button for "WRITE" it will actually write... via a lot of process like INIT CARD, PASS CODE AUTH, then WRITE... and each time the same variables will actually be used as that's part of API. So hands are stuck...
No matter from where TxArray come from, the important thing is to know the exact format of the array TxData which you are going to pass to the API.
Is it different than my approach? ;)
Re: text box to byte array to function parameter how to??
Quote:
Originally Posted by
4x2y
Is it different than my approach? ;)
doesn't seem so...
Just that i was kind of not in a mood of using text.change rather a single shot... i mean via the button... i changed it the code to this below... and found it is working with a bit of tuning (but that's okey for now)
Code:
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
If (Hdle <> 0) Then
Dim Pm As Byte
Dim Cm As Byte
Dim RxDataLen As UInt16
Dim TxDataLen As UInt16
Dim TxData() As Byte = New Byte((1024) - 1) {}
Dim RxData() As Byte = New Byte((1024) - 1) {}
Dim ReType As Byte = 0
Dim St1 As Byte
Dim St0 As Byte
Dim CLA As Byte
Dim INS As Byte
Dim P1 As Byte
Dim P2 As Byte
Cm = &H53
Pm = &H30
St1 = 0
St0 = 0
TxDataLen = 0
RxDataLen = 0
Dim i As Integer
i = CRT288KDLL.DllClass.RS232_ExeCommand(Hdle, Cm, Pm, TxDataLen, TxData, ReType, St1, St0, RxDataLen, RxData)
If (i = 0) Then 'Activate Card first
If (ReType = 80) Then
MsgBox("SLE4442 Activated" & vbCrLf & "Status Code : " & St1 + St0 & "SLE4442 Activated", vbOKOnly)
'password auth first
ReType = 0
'Dim St1 As Byte
'Dim St0 As Byte
Cm = &H53
Pm = &H33
St1 = 0
St0 = 0
CLA = &H0
INS = &H20
P1 = &H3
P2 = &H1
TxData = {CLA, INS, P1, P2, &H3, &H12, &H12, &H12}
TxDataLen = TxData.Length
RxDataLen = 0
i = CRT288KDLL.DllClass.RS232_ExeCommand(Hdle, Cm, Pm, TxDataLen, TxData, ReType, St1, St0, RxDataLen, RxData)
If (i = 0) Then
If (ReType = 80) Then 'Auth success
MsgBox("AUTH OK" & vbCrLf & "Status Code : " & St1 + St0, vbOKOnly)
ReType = 0
Cm = &H53
Pm = &H33
St1 = 0
St0 = 0
CLA = &H0
INS = &HD0
P1 = &H0
P2 = 0
Dim TxArrayB() As Byte
TxArrayB = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text)
Dim TxArrayLen As Byte = TxArrayB.Length
Dim TxArrayA() As Byte = {CLA, INS, P1, &H10, TxArrayLen}
'TxData = {CLA, INS, P1, &H10, TxArrayLen, TxArrayB}
TxData = New Byte(TxArrayA.Length + TxArrayB.Length) {}
TxArrayA.CopyTo(TxData, 0)
TxArrayB.CopyTo(TxData, TxArrayA.Length)
TxDataLen = TxData.Length
RxDataLen = 0
i = CRT288KDLL.DllClass.RS232_ExeCommand(Hdle, Cm, Pm, TxDataLen, TxData, ReType, St1, St0, RxDataLen, RxData)
If (i = 0) Then 'Read Card Data
MsgBox("Cmnd executed with ReType=" & ReType)
If (ReType = 80) Then
MsgBox("Read OK" & vbCrLf & "Status Code : " &
St1 + St0, vbOKOnly)
MsgBox("DataLength:" & RxDataLen, vbOKOnly)
Dim RxString As String
Label2.Text = ""
RxString = System.Text.UnicodeEncoding.UTF8.GetString(RxData)
RxString = RxString.Substring(0, 5)
Label2.Text = RxString
Else
MsgBox("Read Failed" & vbCrLf & "Status Code : " &
St1 + St0, vbOKOnly)
End If
Else
MsgBox("Read CMD Not executed" & vbCrLf & "Status Code : " &
St1 + St0, vbOKOnly)
End If
Else
MsgBox("Auth Failed" & vbCrLf & "Status Code : " &
St1 + St0, vbOKOnly)
End If
Else
MsgBox("Auth CMD Not executed" & vbCrLf & "Status Code : " &
St1 + St0, vbOKOnly)
End If
Else
MsgBox("SLE4442 Activation ERROR" & vbCrLf & "Status Code : " &
St1 + St0 & "SLE4442 Activation Error", vbOKOnly)
End If
Else
MsgBox("Communication Error", "Caution")
End If
Else
MsgBox("Comm. port is not Opened", "Caution")
End If
End Sub
Re: text box to byte array to function parameter how to??
Quote:
Originally Posted by
aq_mishu
Just that i was kind of not in a mood of using text.change rather a single shot... i mean via the button...
Of course, i used TextChanged event just for test purpose to quickly see the constructed array in the Immediate window.