473,769 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

delete value in array of byte

kirubagari
158 New Member
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.




Expand|Select|Wrap|Line Numbers
  1. Public Function DeleteByte(pmArray() As Byte, pmPosition As Long, ByVal pmNewValue As Byte)
  2. ' Function to delete a new byte value into a byte array.
  3. Dim ArrayStart As Long, ArrayEnd As Long
  4. Dim I As Long
  5. ArrayStart = LBound(pmArray)
  6. ArrayEnd = UBound(pmArray) -1
  7. ' Expand array to make room for the new value.
  8. ReDim Preserve pmArray(ArrayStart To ArrayEnd)
  9. ' If requested position is outside the array, just stick it
  10. ' at the start or end as appropriate.
  11. If pmPosition < ArrayStart Then
  12. pmPosition = ArrayStart
  13. End If
  14. If pmPosition > ArrayEnd Then
  15. pmPosition = ArrayEnd
  16. End If
  17. ' Unless adding to the end of the array,
  18. ' push everything across to make room.
  19. If pmPosition < ArrayEnd Then
  20. For I = ArrayEnd To pmPosition -1 Step -1
  21. pmArray(I) = pmArray(I - 1)
  22. Next
  23. End If
  24. pmArray(pmPosition) = pmNewValue
  25. End Function
Aug 9 '07
36 3511
kirubagari
158 New Member
Expand|Select|Wrap|Line Numbers
  1.  public function delete( ) 
  2.  
  3. Dim strLookFor As String, strChangeTo As String
  4. strChangeTo = Chr$(&H4) & Chr$(&HFF)
  5. strLookFor = strChangeTo & strChangeTo
  6. strFile = Replace(strFile, strLookFor, strChangeTo)
  7.  
  8. end sub
  9.  
  10.  
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..
Aug 10 '07 #11
Killer42
8,435 Recognized Expert Expert
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...

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. DefLng A-Z
  3.  
  4. Private strFileData As String, strOriginalData As String
  5.  
  6. Public Function ZapDuplicates(ByVal strFileName As String, strLookFor As String, strChangeTo As String) As Boolean
  7.   ' Returned value indicates whether the file was actually changed.
  8.   Dim FileNo As Long
  9.   FileNo = FreeFile
  10.   Open strFileName For Binary Access Read Shared As #FileNo
  11.   strFileData = Space$(LOF(FileNo))
  12.   Get #FileNo, , strFileData
  13.   Close #FileNo
  14.   strOriginalData = strFileData
  15.  
  16.   strFileData = Replace(strFileData, strLookFor, strChangeTo)
  17.   If strOriginalData <> strFileData Then
  18.     If MsgBox("Data changed - rewrite file?", vbQuestion + vbYesNo) = vbYes Then
  19.       Open strFileName For Output Access Write Lock Write As #FileNo
  20.       Print #FileNo, strFileData;
  21.       Close #FileNo
  22.       ZapDuplicates = True
  23.     End If
  24.   End If
  25.   strOriginalData = ""
  26.   strFileData = ""
  27. End Function
I hope you will think carefully about the parameters. If you plug in the actual string "04 FF" I'll be disappointed.
Aug 10 '07 #12
kirubagari
158 New Member
Expand|Select|Wrap|Line Numbers
  1.  Private Sub CMDFIND_Click() 
  2.  
  3.   If lblFileSpec.Caption = "" Then
  4.     Exit Sub
  5.   End If
  6.   txbSearch.Text = "04FF04FF"
  7.   picHexDisp.SetFocus
  8.   doHexSearch
  9. End Sub
  10.  
  11.  
  12. Private Sub doHexSearch()
  13.   On Error Resume Next
  14.   Dim HexCtn As Integer
  15.   Dim I, J
  16.   Dim mMatch As Boolean
  17.   Dim foundStartPos As Long
  18.   Screen.MousePointer = vbHourglass
  19.   HexCtn = Len(txbSearch.Text) / 2
  20.   ReDim arrHexByte(1 To HexCtn)
  21.   For I = 1 To HexCtn
  22.     arrHexByte(I) = CByte("&h" & (Mid(txbSearch.Text, (I * 2 - 1), 2)))
  23.   Next I
  24.   foundStartPos = prevFoundPos + 1
  25.   For I = foundStartPos To (UBound(arrByte) - (HexCtn - 1))
  26.     If arrByte(I) = arrHexByte(1) Then
  27.       mMatch = True
  28.       ' Compare rest bytes
  29.       For J = 1 To (HexCtn - 1)
  30.         If arrByte(I + J) <> arrHexByte(1 + J) Then
  31.           mMatch = False
  32.           Exit For
  33.         End If
  34.       Next J
  35.       If mMatch = True Then
  36.         Dim k
  37.         foundStartPos = I
  38.         prevFoundPos = I
  39.         k = (foundStartPos + 1) / CLng(mPageSize)
  40.         k = NoFraction(k)
  41.         pageStart = k * mPageSize + 1
  42.         pageEnd = pageStart + mPageSize - 1
  43.         If pageEnd > mFileSize Then pageEnd = mFileSize
  44.         k = foundStartPos + (HexCtn - 1)
  45.         If k > pageEnd Then k = pageEnd
  46.         updEditByte
  47.         ShowPage True, foundStartPos, k, &HFFFF00, vbRed
  48.         Screen.MousePointer = vbDefault
  49.         Exit Sub
  50.       End If
  51.     End If
  52.   Next I
  53.   Screen.MousePointer = vbDefault
  54.   prevFoundPos = 0
  55.   MsgBox txbSearch.Text & vbCrLf & vbCrLf & "Searched to end."
  56. End Sub
  57.  
  58.  
  59. Function NoFraction(ByVal inVal As Variant) As Long
  60.   Dim X As Integer
  61.   Dim tmp As String
  62.   Dim k As Long
  63.   tmp = CStr(inVal)
  64.   X = InStr(tmp, ".")
  65.   If X > 0 Then
  66.     tmp = Left(tmp, X - 1)
  67.   End If
  68.   k = Val(tmp)
  69.   NoFraction = k
  70. End Function
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?
Aug 13 '07 #13
Killer42
8,435 Recognized Expert Expert
The function that I provided in my last post will find and replace all occurrences of the string in the file, not just one.
Aug 13 '07 #14
kirubagari
158 New Member
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
Aug 13 '07 #15
kirubagari
158 New Member
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  
  4.  
  5.  
  6. Option Explicit
  7. DefLng A-Z
  8.  
  9. Private strFileData As String, strOriginalData As String
  10.  
  11. Public Function ZapDuplicates(ByVal strFileName As String, strLookFor As String, strChangeTo As String) As Boolean
  12. ' Returned value indicates whether the file was actually changed.
  13. Dim FileNo As Long
  14. FileNo = FreeFile
  15. Open strFileName For Binary Access Read Shared As #FileNo
  16. strFileData = Space$(LOF(FileNo))
  17. Get #FileNo, , strFileData
  18. Close #FileNo
  19. strOriginalData = strFileData
  20.  
  21. strFileData = Replace(strFileData, strLookFor, strChangeTo)
  22. If strOriginalData <> strFileData Then
  23. If MsgBox("Data changed - rewrite file?", vbQuestion + vbYesNo) = vbYes Then
  24. Open strFileName For Output Access Write Lock Write As #FileNo
  25. Print #FileNo, strFileData;
  26. Close #FileNo
  27. ZapDuplicates = True
  28. End If
  29. End If
  30. strOriginalData = ""
  31. strFileData = ""
  32. End Function 
  33.  
  34. private command1_click( )
  35.  
  36. call Function ZapDuplicates(a:\bonding.bin,04 FF 04 FF,04 FF)
  37.  
  38. end sub
  39.  
  40.  
  41.  

i do no wheather im doing rite or not...Am i rite?



Aug 13 '07 #16
Killer42
8,435 Recognized Expert Expert
There's no need to create a function called Replace. The Replace function is built into VB.

Also, in this line...
call Function ZapDuplicates(a :\bonding.bin,0 4 FF 04 FF,04 FF)
your parameters just don't make any sense. The ZapDuplicates function expects three parameters, all strings. You should read the documentation and get familiar with the basics like how to pass a parameter to a function, and how to specify a string.
Aug 13 '07 #17
kirubagari
158 New Member
Expand|Select|Wrap|Line Numbers
  1.  Private Sub Command2_Click() Dim i As String
  2. Dim b As String
  3. Dim m As String
  4. Dim strLookFor As String
  5. Dim strFileName As String
  6. Dim strChangeTo As String
  7. strChangeTo = Chr$(&H4) & Chr$(&HFF)
  8. strChangeTo = i
  9. strLookFor = strChangeTo & strChangeTo
  10. strLookFor = b
  11. strFileName = "a:/bonding.bin"
  12. strFileName = m
  13. Call ZapDuplicates(m, b, i)
  14. End Sub
  15.  

am i doing rite in passing the parameter?







Aug 13 '07 #18
Killer42
8,435 Recognized Expert Expert
Try this...

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command2_Click()
  2.   ' Dim i As String
  3.   ' Dim b As String
  4.   ' Dim m As String
  5.   Dim strLookFor As String
  6.   Dim strFileName As String
  7.   Dim strChangeTo As String
  8.   strChangeTo = Chr$(&H4) & Chr$(&HFF)
  9.   ' strChangeTo = i  <-- What is this for?
  10.   strLookFor = strChangeTo & strChangeTo
  11.   ' strLookFor = b   <-- What is this for?
  12.   strFileName = "a:\bonding.bin"
  13.   ' strFileName = m  <-- What is this for?
  14.   Call ZapDuplicates(strFileName, strLookFor, strChangeTo)
  15. End Sub
  16.  
For some reason, you were placing values in strFileName, strLookFor and strChangeTo, then throwing them away by replacing them with empty strings from i, b and m. (Also, note I changed the slash to a backslash in the file name.)
Aug 13 '07 #19
kirubagari
158 New Member
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. Private Sub Command1_click()
  4. Dim mHandle
  5. mHandle = FreeFile
  6. Open "a:\bonding.bin" For Binary Access Read Write Lock Write As mHandle
  7. ReWrite_Open_File mHandle
  8. End Sub
  9.  
  10. Public Function ZapDuplicates(ByVal strFilename As String, strLookFor As String, strChangeTo As String) As Boolean
  11. ' Returned value indicates whether the file was actually changed.
  12. Dim FileNo As Long
  13. FileNo = FreeFile
  14. Open strFilename For Binary Access Read Shared As #FileNo
  15. strFileData = Space$(LOF(FileNo))
  16. Get #FileNo, , strFileData
  17. Close #FileNo
  18. strOriginalData = strFileData
  19. strFileData = Replace(strFileData, strLookFor, strChangeTo)
  20. If strOriginalData <> strFileData Then
  21. If MsgBox("Data changed - rewrite file?", vbQuestion + vbYesNo) = vbYes Then
  22. Open strFilename For Output Access Write Lock Write As #FileNo
  23. Print #FileNo, strFileData;
  24. Close #FileNo
  25. ZapDuplicates = True
  26. End If
  27. End If
  28. strOriginalData = ""
  29. strFileData = ""
  30. End Function
  31.  
  32.  
  33.  
  34. Public Sub ReWrite_Open_File(ByVal FileNo As Long)
  35.  
  36. Const a As Byte = 4
  37. Const b As Byte = &HFF
  38. Dim i As Long
  39. Dim AnyChanged As Boolean
  40. Dim J As Long
  41. Dim strLookFor As String
  42. Dim strChangeTo As String
  43. Dim strFilename As String
  44. ' This little array will be used to hold the "before" values,
  45. ' so we can display them.
  46. Dim HoldByte(1 To 6) As Byte
  47.  
  48. mFileSize = LOF(FileNo) ' Determine how large the file is (in bytes).
  49. ReDim Buffer(1 To mFileSize) ' Set our buffer to that length.
  50. Get #FileNo, 1, arrByte ' Grab the entire file's data into the array
  51.  
  52.  
  53.  
  54. i = 49
  55. ' Note, not using a FOR loop, as the end-point may shift during processing.
  56. Do Until i >= mFileSize - 6
  57. ' Make a copy of this chunk of data, so we can display the
  58. ' "before" version after changing it.
  59. For J = 1 To 6
  60. HoldByte(J) = arrByte(i + J - 1)
  61. Next
  62. AnyChanged = False
  63.  
  64.  
  65. If arrByte(i) <> a Then
  66. InsertByte arrByte, i, a ' Identified a missing byte. Insert it.
  67. mFileSize = mFileSize + 1 ' Reflect the addition of 1 byte to the array.
  68. changemade = True
  69. End If
  70. If arrByte(i + 1) <> b Then
  71. InsertByte arrByte, i + 1, b
  72. mFileSize = mFileSize + 1 ' Reflect the addition of 1 byte to the array.
  73. changemade = True
  74. End If
  75. ' If we changed this chunk of data, report the change.
  76. If changemade Then
  77. AnyChanged = True
  78. ' This demonstrates how useful user-defined functions can be...
  79. ' Note, since we are reporting only the parts actually changed, you might
  80. ' want to try putting it in a RichTextBox again; the performance should be
  81. ' somewhat improved. (You could use a plain TextBox, too).
  82. Debug.Print "Before ("; Format(i, "000000"); ") : "; FormattedByteArray(HoldByte, 1, 6)
  83. Debug.Print "After ("; Format(i, "000000"); ") : "; FormattedByteArray(arrByte, i, i + 5)
  84. Debug.Print
  85. End If
  86. i = i + 6
  87. Loop
  88.  
  89. If AnyChanged Then
  90. If MsgBox("Write data back to the file?", vbYesNo) = vbYes Then
  91. Put #FileNo, 1, arrByte() ' Write the data back to the file.
  92. End If
  93. End If
  94.  
  95. strChangeTo = Chr$(&H4) & Chr$(&HFF)
  96.  
  97. strLookFor = strChangeTo & strChangeTo
  98.  
  99. strFilename = "a:\bonding.bin"
  100.  
  101. Call ZapDuplicates(strFilename, strLookFor, strChangeTo)
  102.  
  103.  
when i append the duplicate and rewrite function together it give the error saying that file already open....i wana do deletion rite after insertion .
Aug 14 '07 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

4
2894
by: Shea Martin | last post by:
Which of the following do I use delete instead of just delete. //1.) // not sure about this one, as char is of size 1 char *str = new char; //2.) //not sure about this one, as it is a primitive int *array = new int;
11
912
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have nice and working allocation deallocation routines. However, I don't want to loose the nice extras of new operator, like - constructor calling, typecasting the result, keeping the array size, etc. For another bunch of reasons, outside this scope I...
19
4152
by: jeff | last post by:
how do you convert form byte to Int32 while retaining the binary value of the byte array
8
2530
by: Richard Arthur | last post by:
This is a weird problem. 1) I use MediaDet to save a bitmap in a temporary file. 2) I create a bitmap using that temporary file's name. 3) I use the bitmap. 4) I want to destroy the file when the process exits, but I am having trouble I have written a Finalizer on the object that actually generates the bitmaps, as well as implementing IDisposable. The finalizer gets called, I then call Dispose on each of the bitmaps it the object has...
6
5900
by: Aston Martin | last post by:
Hi All, ********************** My Situation ********************** I am working on project that involves passing a structure to unmanaged code from .Net world (well using C#). Perhaps an example will prove useful. structure MyStruct { // this is a complicated struct declaration in the sense
9
2922
by: Money | last post by:
If I allocate memory like this int *ptr = new int; Can I apply delete ptr; instead of delete ptr; since I am only allocating memory for 1 integer.
29
4253
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to a temp array and delete afterwards if necessary but since I'm actually working in a nested situation this could get a little messy. I guess I could set there values to null and remove them afterwards? Thanks, Jon
1
1156
kirubagari
by: kirubagari | last post by:
is it possible to do deletion in visual basic. 1.04 FF F2 03 37 04 04 FF - FF 03 37 04 04 FF F0 03.....line 1 2.37 04 04 FF EF 03 37 04 - 04 FF 04 FF EE 03 37 04.....line 2 3.04 FF ED 03 37 04 04 FF - EC 03 37 04 04 FF EB 03....line 3 There is repeating data in the above value.How i can delete the repeating value.When i check it out from byte 49 to mfile size there is repeating data 04 FF.How i can use vb to delete the repetating...
13
5042
by: Tristan Wibberley | last post by:
Hi I've got implementing overloaded operator new and delete pretty much down. Just got to meet the alignment requirements of the class on which the operator is overloaded. But how does one implement operator new/delete I can't see a way to indicate, on delete, how many objects must be destroyed (or how big the space is) - alternatively I can't figure out what are the alignment requirements so that the implementation, after calling my...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.