c# - Convert hex string to bytearray and write to file -


I wrote an app using C # to read data from a serial port, and shows data in a text box in hex string format. I

Finally, I had saved all the data in the binary file. If the data is large (maybe> 20mb) it throws out of memory error, how can it be solved? Here is my code:

  Private zeros btn_Save_Click (Object Sender, EventArgs e) {SaveFileDialog save_log = New SaveFileDialog (); Save_log.DefaultExt = ".bin"; Save_log.Filter = "Binary file (* .bini) | * .bin"; // determine that the user has chosen the name of a file from saveFileDialog. If (save_log.ShowDialog () == System.Windows.Forms.DialogResult.OK & save_log.FileName.Length & gt; {} {try {string hexString = Content.Text.ToString ()} file stream stream = new FileStream (save_log.FileName, FileMode.Create, FileAccess.ReadWrite); Stream Write (hex_to_byteArray (hexstring), 0, hex_to_byteArray (hexstringing lamp.); Stream.Close (); } Hold (Exception pre) {Message Box. Show (ex.Message); }}} Private byte [] hex_to_byteArray (strings) {s = s.Replace ("", ""); Byte [] buffer = new byte [s.Length / 2]; To convert (buffer i / 2) = (byte) to tobit (s.Substring (i, 2), 16); } Return buffer; }    

You are creating a two-byte array simultaneously, . Change This type of long string does not help you get rid of all this:

  Try {var stream = new filestream (save_log.phileName, FileMode.Create, FileAccess .ReadWrite); WriteHexStringToFile (content. Text, stream); Stream.Close (); } Hold (Exception pre) {Message Box. Show (ex.Message); } Private Zero WriteHexStringToFile (string hexstring, file stream stream) {var twoCharacterBuffer = new StringBuilder (); Var ebits = new byte [1]; Fortecha (Where is the variable in the hex string (C => c! = '')) {Two character buffer Append (character); If (two converter buffer length == 2) {convert one bite [0] = (byte). Toight (two character buffer. Toastring (), 16); Stream Write (Ekbitite, 0, 1); twoCharacterBuffer.Clear ();  

Update:

In addition, take a look and / or who can do all this for you. should do. You should process your data in small parts. Due to this nonsense I am unable to provide you demo (for example IDEONE) working due to the limitations of the online compiler. I have tested the code on my machine, and as you can see I can also process 50 MB strings - but all this depends on the amount of memory available to you. If you do such things, it will be easy to reach the limit of memory available on each machine. And the ways in which you ask about this particular question are irrelevant - the problem is that you fill in your memory with lots of data in your content.Text string. When the archive is almost complete, then OutOfMemoryException can be in almost any place in your code.

You can see the full picture in your browser to see all the details.

see freehand circle

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -