Results 1 to 16 of 16

Thread: Removing an ASCII from an array

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2008
    Location
    Kent, England
    Posts
    713

    Removing an ASCII from an array

    Hi guys,

    as part of my project, I have data coming in on a socket, which appears to be converted to an array. The string sent back according to the other application is always string only, no special ascii characters. However, on some occassions the message comes back into the array as the correct ascii free message, other times, for some reason it comes back prefixed with an "Ack" (ASCII 6).

    What I am looking to do is to find a way that says "If the array starts with an ACK then step over field 0 and read field 1.

    Any thoughts?
    "Wisdom is only truly achieved, when you realise you dont know everything" ... I must be a genius because I always have to ask stupid questions...

    Pointing an idiot like me in the right direction, is always appreciated by the idiot, explaining how to do what you have pointed the idiot to, is appreciated even more. I apologise to all experienced coders who will think I am an idiot, you are right, I am an idiot, but I am an idiot who is trying to learn

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Removing an ASCII from an array

    It's a bit easier to do in a List, since a List has a .RemoveAt method. You could take the array of bytes, use .ToList to convert to a list, check item 0, and if it is = 6, remove it. Finally use .ToArray if you need to get back to an array.

    That seems a bit convoluted, but simple enough to do. If speed is more important, then what I'd do is to take the incoming array of bytes, check the first byte to see if it is 6, and if it is, use Array.Copy to copy all the other bytes into a new array without including the first byte. Array.Copy is REALLY fast for this kind of work. It's the fastest method I have found for moving chunks of bytes around.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2008
    Location
    Kent, England
    Posts
    713

    Re: Removing an ASCII from an array

    Thanks Shaggy.

    So I have the array is called "EFTReplies", if I want to pull the first field I call EFTReplies(0).

    so if I want to convert it to a string would I do:

    Code:
    DIM ArrayToString as string
    
    ArrayToString = EFTReplies.ToString
    Thanks

    James
    "Wisdom is only truly achieved, when you realise you dont know everything" ... I must be a genius because I always have to ask stupid questions...

    Pointing an idiot like me in the right direction, is always appreciated by the idiot, explaining how to do what you have pointed the idiot to, is appreciated even more. I apologise to all experienced coders who will think I am an idiot, you are right, I am an idiot, but I am an idiot who is trying to learn

  4. #4
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Removing an ASCII from an array

    ToString does not convert any thing. Simply gets the string representation.

  5. #5
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Removing an ASCII from an array

    Copy to another array

    Code:
        Private Function GetNewArray(a() As Integer) As Integer()
            If a(0) = 6 Then
                Dim r(a.Length - 2) As Integer
                Array.ConstrainedCopy(a, 1, r, 0, a.Length - 2)
                Return r
            Else
                Return a
            End If
        End Function



  6. #6
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Removing an ASCII from an array

    To be clear, sockets (sic) transmit bytes and only bytes. It does not send and receive strings. How those bytes are interpreted is up to you; dependent on the protocol.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2008
    Location
    Kent, England
    Posts
    713

    Re: Removing an ASCII from an array

    Thanks for the replies folks. Its not a major headache at this moment in time, but I will look to implement this in the next few days, so may come back with more questions!
    "Wisdom is only truly achieved, when you realise you dont know everything" ... I must be a genius because I always have to ask stupid questions...

    Pointing an idiot like me in the right direction, is always appreciated by the idiot, explaining how to do what you have pointed the idiot to, is appreciated even more. I apologise to all experienced coders who will think I am an idiot, you are right, I am an idiot, but I am an idiot who is trying to learn

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2008
    Location
    Kent, England
    Posts
    713

    Re: Removing an ASCII from an array

    Hi Again Guys,

    So I am looking into this and seem to be doing something wrong. I am looking to try and use 4x2y's code

    I have tried to convert the string I have into an array, but its a string array and not an integer array, I think I am doing something very wrong, as the string will contain valid characters, as well as just integers...

    Code:
        Private Sub lblResult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblResult.Click
            Dim StringSeperators() As String = {","}
            Dim test As Array
            Dim test2 As String()
    
            test2 = txtProgPort.Text.Split(StringSeperators, StringSplitOptions.None)
    
            test = GetNewArray(test2)
    
            test2.CopyTo(GetNewArray)
        End Sub
    The errors I get are:

    Code:
    Error	6	Value of type '1-dimensional array of String' cannot be converted to '1-dimensional array of Integer' because 'String' is not derived from 'Integer'.	C:\Users\James\Desktop\IMPS POS v0.3\ImpsPoS\Transactions\frmTransactions.vb	749	28	ImpsPoS
    Error	7	Argument not specified for parameter 'a' of 'Public Function GetNewArray(a() As Integer) As Integer()'.	C:\Users\James\Desktop\IMPS POS v0.3\ImpsPoS\Transactions\frmTransactions.vb	751	22	ImpsPoS
    What specifically have I stuffed up this time
    "Wisdom is only truly achieved, when you realise you dont know everything" ... I must be a genius because I always have to ask stupid questions...

    Pointing an idiot like me in the right direction, is always appreciated by the idiot, explaining how to do what you have pointed the idiot to, is appreciated even more. I apologise to all experienced coders who will think I am an idiot, you are right, I am an idiot, but I am an idiot who is trying to learn

  9. #9
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Removing an ASCII from an array

    how can you convert a string into a array?

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Removing an ASCII from an array

    Maybe more than one thing.

    What is GetNewArray? In the first usage, it looks like a function call, but in the second, it looks like an array. The error shows that it is a method, so I guess it must be, but what is the point of the second line, then? CopyTo takes two arguments, where the first is an array. You are supplying a method name.

    On the other hand, I can't figure out what the goal is, so I can't suggest any way to correct what you are doing. In what way do you want test() to be different from test2()? What's the goal?
    My usual boring signature: Nothing

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2008
    Location
    Kent, England
    Posts
    713

    Re: Removing an ASCII from an array

    Apologies if I wasnt so clear, unfortunately I am suffering from insomnia at the moment so sleep deprivation is kicking in!

    The reason for this particular removal is because the software receives data from the socket.

    Code:
     Public Function ReadMessage() As String
            On Error Resume Next
    
            Dim terminator As Byte = 13
    
            Dim dtTimeout As Date = Date.Now.AddSeconds(60)
    
            Dim bRead As Byte()
            Do Until Date.Now > dtTimeout
                bRead = Read()
                'MsgBox("variable set")
    
                For Each b As Byte In bRead
                    If b <> terminator Then
                        _receivedData.Add(b)
                    Else
                        Dim strReturn As String = System.Text.Encoding.ASCII.GetString(_receivedData.ToArray(GetType(Byte)))
                        'frmTransactionLog.txtTXNLog.AppendText(Now() & " Incoming Data : " & _tcpClient.GetHashCode & strReturn)
                        'MsgBox("strereturn-ed" & strReturn)
                        _receivedData.Clear()
                        'MsgBox("should be clear")
                        Return strReturn
                        clsLogging.WriteToLogFile("Read", 0, _tcpClient.GetHashCode, "Data Received : " & strReturn)
                        'MsgBox(strReturn)
                    End If
                Next
            Loop
        End Function
    The software then has a thread running which triggers listening to the socket. If the string received is different to the string it has in memory, it sets it to the variable "EFTReplies".

    A timer runs on the form, and checks for the data that "EFTReplies" has....

    Code:
    Select Case clsNewSockets.EFTreplies(0)
                        Case "0"
                            txtEFTPORT.Text = "Transaction Successful"
                            clsNewSockets.EFTreplies = ""
                            Exit Sub
                        Case "6"
                            ' MsgBox("AUTHORISED")
                            txtEFTPORT.Text = ("Transaction Authorised")
                        Case Is = "7"
                            txtEFTPORT.Text = "Transaction Declined"
                            Exit Sub
                        Case Else
                            txtEFTPORT.Text = clsNewSockets.EFTreplies
                            Exit Sub
                    End Select
    6 on its own is a valid response (the other software can return 6 as an intermediate response) and for the first transaction or so, the data comes back correctly, - i.e. the first field of the string is the valid 6 response, and the software interprets that as an "authorised" response, in which case it then updates the appropriate text box with "Transaction Authorised".

    However, after a few transactions, the data then seems to show an "ACK," before the actual socket received data. Because the Case Else is just to show that data in the box, you can see the 'marginally elongated minus symbol' as the ACK. Copy this data into NotePad++ and it shows "ACK" as the Ascii response.

    The purpose of this code, is to basically take the "EFTReplies" data, and run it through a function, so that if the data read for some reason starts with an ACK, it can be stripped out and then the remaining data replaces the original "EFTReplies" message, subsequently setting the first field to be what is expected.

    I will play with this, generate a log file and an example and show you what I mean...
    "Wisdom is only truly achieved, when you realise you dont know everything" ... I must be a genius because I always have to ask stupid questions...

    Pointing an idiot like me in the right direction, is always appreciated by the idiot, explaining how to do what you have pointed the idiot to, is appreciated even more. I apologise to all experienced coders who will think I am an idiot, you are right, I am an idiot, but I am an idiot who is trying to learn

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2008
    Location
    Kent, England
    Posts
    713

    Re: Removing an ASCII from an array

    Right, so reproduced the issue. For the record, the other software replied with "TX (60073280-a473-4715-9a3c-c47d08c22375) 14:43:14.0490238: 0,1,10.00,0.00,0.00,,1217,,0115,20150206144248,21249872,22736839,MASTERCARD,,3212,789DE,,PayPass Contactless NO CARDHOLDER VERIFICATION"

    Which doesnt have an ACK at the start of the string.

    Anyway, the App logs are attached, so you can see what I mean, which shows the ACK being present, otherwise when it comes in as 6 as the first character that triggers the "transaction authorised" message.

    06022015.txt
    "Wisdom is only truly achieved, when you realise you dont know everything" ... I must be a genius because I always have to ask stupid questions...

    Pointing an idiot like me in the right direction, is always appreciated by the idiot, explaining how to do what you have pointed the idiot to, is appreciated even more. I apologise to all experienced coders who will think I am an idiot, you are right, I am an idiot, but I am an idiot who is trying to learn

  13. #13
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Removing an ASCII from an array

    Deal with this before you convert the array of bytes to a string. Working with strings can seem pretty easy, but a lot of that is window dressing. Working with the array of bytes is going to be faster.

    If the leading byte is either Ack or not Ack, then that's the easiest thing to check for, because you can look at bRead(0) to see whether it is the byte you expected, or the ACK. If it is the Ack, then you would just copy the array:

    1) Check the first byte of bRead. If it is ACK, then create a new array of byte with a size one less than bRead (if it is not ACK, then skip the next steps).
    2)Copy the array like so:

    Array.Copy(bRead, 1, yourNewByteArray,0,bRead.Length - 1)

    3) Proceed.

    Of course, if the first byte is not ACK, then just assign bRead to yourNewByteArray. Either way, yourNewByteArray would hold the valid set of bytes, which you can then deal with as you have been.
    My usual boring signature: Nothing

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2008
    Location
    Kent, England
    Posts
    713

    Re: Removing an ASCII from an array

    Thanks SH, does the -1 remove just the ack and leave the comma, or does it also remove the comma? unless I am misunderstanding in which case as it appears to be its own field, would I do bread.length - 2? and would that automatically chop them off the front, or do I need to do anything in particular to say "remove from front of array 2 bytes?"
    "Wisdom is only truly achieved, when you realise you dont know everything" ... I must be a genius because I always have to ask stupid questions...

    Pointing an idiot like me in the right direction, is always appreciated by the idiot, explaining how to do what you have pointed the idiot to, is appreciated even more. I apologise to all experienced coders who will think I am an idiot, you are right, I am an idiot, but I am an idiot who is trying to learn

  15. #15
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Removing an ASCII from an array

    Yeah, if you need to remove the ACK byte AND a comma Byte, then you'd have to create a new array that is 2 smaller than bRead, and you would need to use bRead.Length-2. That -1 doesn't remove anything. What copy does is takes the source array, the starting point in the source array, the destination array, and the number of elements you want to copy over. So, I am taking the initial array (bRead), starting with the second byte (index 1, because the first byte is the ACK), copying to the destination array (yourNewByteArray), and copying over one less than all the bytes in bRead. If there is a second byte that has to be removed, then you would start with index 2 in bRead, and would have to copy over bRead.Length - 2.
    My usual boring signature: Nothing

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2008
    Location
    Kent, England
    Posts
    713

    Re: Removing an ASCII from an array

    Thanks SH, I appreciate the guidance. I must admit, the socket stuff you see above (primarily dealing with the Send and Recieve classes) was given to me by a former colleague, so I didnt write it at all, which is primarily my problem!

    I must admit, I build this app based mostly on what I do know, and using the other guys code and trial and error until I got it to do what I wanted it to do (I dont think thats coding, I think its probably considered fluking!) so I am a bit nervous I will fluff this up

    I do appreciate the advice as always and will give this a crack a bit later, but for now, I think I need a whisky and to get my mind away from the software, as I cant see the wood for the trees at the moment. I am sure I will be back soon enough to ask how I am being an idiot now!
    "Wisdom is only truly achieved, when you realise you dont know everything" ... I must be a genius because I always have to ask stupid questions...

    Pointing an idiot like me in the right direction, is always appreciated by the idiot, explaining how to do what you have pointed the idiot to, is appreciated even more. I apologise to all experienced coders who will think I am an idiot, you are right, I am an idiot, but I am an idiot who is trying to learn

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width