delete value in array of byte

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kirubagari
    New Member
    • Jun 2007
    • 158

    delete value in array of byte

    How about the deletion function.i just modify the insert function to delete function.is it correct.actualy i would like to delete if 04 FF 04 FF (got repeating value) i would like to delete one of the 04 FF.So can i use the function that i modify .The same thing i wana do as insert function but i wana delete this time the condition is if there is repeating value as i mentioned just now.




    [CODE=vb]Public Function DeleteByte(pmAr ray() As Byte, pmPosition As Long, ByVal pmNewValue As Byte)
    ' Function to delete a new byte value into a byte array.
    Dim ArrayStart As Long, ArrayEnd As Long
    Dim I As Long
    ArrayStart = LBound(pmArray)
    ArrayEnd = UBound(pmArray) -1
    ' Expand array to make room for the new value.
    ReDim Preserve pmArray(ArraySt art To ArrayEnd)
    ' If requested position is outside the array, just stick it
    ' at the start or end as appropriate.
    If pmPosition < ArrayStart Then
    pmPosition = ArrayStart
    End If
    If pmPosition > ArrayEnd Then
    pmPosition = ArrayEnd
    End If
    ' Unless adding to the end of the array,
    ' push everything across to make room.
    If pmPosition < ArrayEnd Then
    For I = ArrayEnd To pmPosition -1 Step -1
    pmArray(I) = pmArray(I - 1)
    Next
    End If
    pmArray(pmPosit ion) = pmNewValue
    End Function[/CODE]
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    I don't think the code is quite right. But before we get into the code, I'd like you to try and state fairly simply, the method to be used. In other words, can you describe to me, in a fairly simple overview, how you would go about removing a value from the middle of an array?

    Turning it into code is quite simple, and we'll get that done quite quickly. But first you have to understand what you're trying to code.

    I'll give you a hint. It's not the same sort of process as inserting. If you start by shortening the array, you've just thrown away whatever was at the end of it.

    Comment

    • kirubagari
      New Member
      • Jun 2007
      • 158

      #3
      Actualy are u saying that it is not possible to delete the byte in the middle of array.My problem is now

      Code:
       04 FF 89 67 87 90 89 04 FF 04 FF 
      89 65 67 78 93 78 78 89 84 67 04

      i wana delete one of the repeating 04 FF in the array of byte.is it possible in vb?

      Comment

      • kirubagari
        New Member
        • Jun 2007
        • 158

        #4
        Actualy when i insert one i have delete one of the 04 FF value in my program

        The arrangement of array sometime like 04 FF 04 FF.So when i insert one 04 FF,i should delete one 04 FF.so i have to find the position where should i delete,sowhen there is repeating value like 04 FF 04 FF ,so i wana delete 1 of the 04 FF.So i have to check until EOF to find out the location to be deleted.So i think it will be explain problem more clear.

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Certainly it's possible to remove a value from the middle of an array. I'd just like you to think about how you would go about removing one from somewhere in the middle.

          You might try thinking of the array as a row of boxes, or something similar. What would be the steps involved in taking one out of the middle and not leaving a gap? If you can explain that in detail, you will be very close to writing the code for it.

          (Note, here I'm just talking about the actual removal of a byte. This has nothing to do with deciding what byte to remove.)

          Comment

          • kirubagari
            New Member
            • Jun 2007
            • 158

            #6
            Code:
             
             
            04 FF F2 03 37 04 04 FF- F1 03 37 04 04 FF F0 03
            37 04 04 FF EF 03 37 04 - [b]04 FF 04 FF[/b] EE 03 37 04
            04 FF ED 03 37 04 04 FF- EC 03 37 04 04 FF EB 03
            For eg i wana find out where the repeating value of 04 FF 04 FF occur,then i wana delete one of the 04 FF
            then i wana merge the array.


            Code:
             
             
            04 FF F2 03 37 04 04 FF - F1 03 37 04 04 FF F0 03
            37 04 04 FF EF 03 37 04 - 04 FF EE 03 37 04 04 FF
            ED 03 37 04 04 FF EC 03 - 37 04 04 FF EB 03 89 67
            This is i wana do after the insertion and do deletion also.How to start?Wana check until EOF and do deletion as the location where there is repeating structure

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              You keep jumping ahead. I'm still trying to get you to spell out how you would go about removing a byte from an array. Let's get working tools before we try to use them, huh?

              Comment

              • kirubagari
                New Member
                • Jun 2007
                • 158

                #8
                sir so how if u give a function that delete byte from array as u give me as insertion function.Like delete a value from array of byte as starting so that i can understand more[/font]

                Comment

                • kirubagari
                  New Member
                  • Jun 2007
                  • 158

                  #9
                  how to delete a value from a byte of array.like HELLO.i wana delete the 2nd value.How i can do in vb

                  Comment

                  • Killer42
                    Recognized Expert Expert
                    • Oct 2006
                    • 8429

                    #10
                    Originally posted by kirubagari
                    how to delete a value from a byte of array.like HELLO.i wana delete the 2nd value.How i can do in vb
                    Now that I think about if further, if you are just looking to replace duplicated strings, I think it will be much simpler to work with a string. You probably won't need the "delete a byte" routine.

                    Let's assume, for the sake of argument, that you have read your file into string variable strText, instead of a byte array. I think the Replace() function might do the fix for you, all in one go, like so...

                    [CODE=vb]Dim strLookFor As String, strChangeTo As String
                    strChangeTo = Chr$(&H04) & Chr$(&HFF)
                    strLookFor = strChangeTo & strChangeTo
                    strFile = Replace(strFile , strLookFor, strChangeTo)[/CODE]

                    Comment

                    • kirubagari
                      New Member
                      • Jun 2007
                      • 158

                      #11
                      [CODE=vb] public function delete( )

                      Dim strLookFor As String, strChangeTo As String
                      strChangeTo = Chr$(&H4) & Chr$(&HFF)
                      strLookFor = strChangeTo & strChangeTo
                      strFile = Replace(strFile , strLookFor, strChangeTo)

                      end sub

                      [/CODE]
                      Can i used the function.It can do deletion is it.....is it deleting the repeating value.can i call the function as i did in the insertion part....open the binary file and check it out until EOF to check whether there is repeating structure....an d do replacement..

                      Comment

                      • Killer42
                        Recognized Expert Expert
                        • Oct 2006
                        • 8429

                        #12
                        Here's a code module containing a function which I think will do the trick. You need to call it, passing the file name, the string to look for (04 FF 04 FF) and the string to replace it with (04 FF). Hopefully, it will do the rest. I haven't tested it, though...

                        [CODE=vb]Option Explicit
                        DefLng A-Z

                        Private strFileData As String, strOriginalData As String

                        Public Function ZapDuplicates(B yVal strFileName As String, strLookFor As String, strChangeTo As String) As Boolean
                        ' Returned value indicates whether the file was actually changed.
                        Dim FileNo As Long
                        FileNo = FreeFile
                        Open strFileName For Binary Access Read Shared As #FileNo
                        strFileData = Space$(LOF(File No))
                        Get #FileNo, , strFileData
                        Close #FileNo
                        strOriginalData = strFileData

                        strFileData = Replace(strFile Data, strLookFor, strChangeTo)
                        If strOriginalData <> strFileData Then
                        If MsgBox("Data changed - rewrite file?", vbQuestion + vbYesNo) = vbYes Then
                        Open strFileName For Output Access Write Lock Write As #FileNo
                        Print #FileNo, strFileData;
                        Close #FileNo
                        ZapDuplicates = True
                        End If
                        End If
                        strOriginalData = ""
                        strFileData = ""
                        End Function[/CODE]
                        I hope you will think carefully about the parameters. If you plug in the actual string "04 FF" I'll be disappointed.

                        Comment

                        • kirubagari
                          New Member
                          • Jun 2007
                          • 158

                          #13
                          [CODE=vb] Private Sub CMDFIND_Click()

                          If lblFileSpec.Cap tion = "" Then
                          Exit Sub
                          End If
                          txbSearch.Text = "04FF04FF"
                          picHexDisp.SetF ocus
                          doHexSearch
                          End Sub


                          Private Sub doHexSearch()
                          On Error Resume Next
                          Dim HexCtn As Integer
                          Dim I, J
                          Dim mMatch As Boolean
                          Dim foundStartPos As Long
                          Screen.MousePoi nter = vbHourglass
                          HexCtn = Len(txbSearch.T ext) / 2
                          ReDim arrHexByte(1 To HexCtn)
                          For I = 1 To HexCtn
                          arrHexByte(I) = CByte("&h" & (Mid(txbSearch. Text, (I * 2 - 1), 2)))
                          Next I
                          foundStartPos = prevFoundPos + 1
                          For I = foundStartPos To (UBound(arrByte ) - (HexCtn - 1))
                          If arrByte(I) = arrHexByte(1) Then
                          mMatch = True
                          ' Compare rest bytes
                          For J = 1 To (HexCtn - 1)
                          If arrByte(I + J) <> arrHexByte(1 + J) Then
                          mMatch = False
                          Exit For
                          End If
                          Next J
                          If mMatch = True Then
                          Dim k
                          foundStartPos = I
                          prevFoundPos = I
                          k = (foundStartPos + 1) / CLng(mPageSize)
                          k = NoFraction(k)
                          pageStart = k * mPageSize + 1
                          pageEnd = pageStart + mPageSize - 1
                          If pageEnd > mFileSize Then pageEnd = mFileSize
                          k = foundStartPos + (HexCtn - 1)
                          If k > pageEnd Then k = pageEnd
                          updEditByte
                          ShowPage True, foundStartPos, k, &HFFFF00, vbRed
                          Screen.MousePoi nter = vbDefault
                          Exit Sub
                          End If
                          End If
                          Next I
                          Screen.MousePoi nter = vbDefault
                          prevFoundPos = 0
                          MsgBox txbSearch.Text & vbCrLf & vbCrLf & "Searched to end."
                          End Sub


                          Function NoFraction(ByVa l inVal As Variant) As Long
                          Dim X As Integer
                          Dim tmp As String
                          Dim k As Long
                          tmp = CStr(inVal)
                          X = InStr(tmp, ".")
                          If X > 0 Then
                          tmp = Left(tmp, X - 1)
                          End If
                          k = Val(tmp)
                          NoFraction = k
                          End Function[/CODE]

                          This is the function that I did to find out the string 04 FF 04 FF.So assign it to text box and when I click the FIND button it find from beginning of file until end. So is it correct way and give me some comments how I can enhance it on how it can find out the value without I click each time as find next value. So when I find out the value may be I can use the function that you give previously as making 04 FF 04 FF To 04 FF. Need your help?
                          Last edited by Killer42; Aug 13 '07, 05:19 AM. Reason: Indent code for readability

                          Comment

                          • Killer42
                            Recognized Expert Expert
                            • Oct 2006
                            • 8429

                            #14
                            The function that I provided in my last post will find and replace all occurrences of the string in the file, not just one.

                            Comment

                            • kirubagari
                              New Member
                              • Jun 2007
                              • 158

                              #15
                              sir am i doing rite sir.So can i use the function to find the string and use the function that u give to replace it

                              Comment

                              Working...