Split a string and add a space every 4 characters.

vamasoft

New Member
Licensed User
Longtime User
Hi, have searched on here but can only find info on splitting a string by finding certain characters.
I have a 16 character alpha numeric card number from a mifare 1k card, I need to split this every 4 characters but can't work out how.
Code uses the mifare example found on here.
B4X:
Sub Read()
   Dim key(16) As Byte 
   Dim block_data() As Byte
   Dim bc As ByteConverter
   Dim sector As Int
   
   'prepare key
   Dim i As Int
   For i  = 0 To 15
      key(i) = 255 '0xff
   Next

   Dim mfc As MiFare
   Log("Check if the card is MF classic")
    If NFC.IsMifareClassic(Activity.GetStartingIntent) Then
      
      mfc.Initialize(Activity.GetStartingIntent)
      If True = mfc.ReadSector(2,key,0) Then' Read sector 0, key, type 0(as keyA)      
         Log("Read sector done")
         i = 8   
            block_data = mfc.GetBlockData(i)
            StrData =  bc.HexFromBytes(block_data)
            Log( "data:" & StrData)
            i = 8
            Label1.Text = StrData
            AsciiToHex (StrData)
                                tb_cardno.Text = ConvertedToString
            
      End If
   End If
End Sub

Sub AsciiToHex (a As String) As String
    Dim bc As ByteConverter
   Dim barray(0) As Byte
   barray = bc.HexToBytes(a)
   ConvertedToString = bc.StringFromBytes(barray,("ASCII"))
End Sub


I need to split either ConvertedToString or the tb_cardno.Text

Thanks.
 

vamasoft

New Member
Licensed User
Longtime User
Found the way:

B4X:
Sub AsciiToHex (a As String) As String
    Dim bc As ByteConverter
   Dim barray(0) As Byte
   Dim one As String
   Dim two As String
   Dim three As String
   Dim four As String
   
   barray = bc.HexToBytes(a)
   Dim targetLength As String
   ConvertedToString = bc.StringFromBytes(barray,("ASCII"))
   one = sf.Mid(ConvertedToString, 1, 4)
   two = sf.Mid(ConvertedToString, 4, 4)
   three = sf.Mid(ConvertedToString, 8, 4)
   four = sf.Mid(ConvertedToString, 12, 4)
   ConvertedToString = one & " " & two & " " & three & " " & four
End Sub
 
Upvote 0
Top