Guest User

Untitled

a guest
Aug 16th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. running out of memory building massive string
  2. byte[] bytes = File.ReadAllBytes(infile);
  3.  
  4. try
  5. {
  6. StringBuilder sb = new StringBuilder(BitConverter.ToString(bytes)); // <--exception
  7. hexfield.Text = sb.ToString();
  8. }
  9.  
  10. catch(Exception e)
  11. {
  12. MessageBox.Show(e.ToString());
  13. }
  14.  
  15. System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
  16. at System.String.CtorCharArrayStartLength(Char[] value, Int32 startIndex, Int32 length)
  17. at System.BitConverter.ToString(Byte[] value, Int32 startIndex, Int32 length)
  18. at System.BitConverter.ToString(Byte[] value)
  19. at shex.shexx.hexfield_Dragrop(Object sender, DragEventArgs e)**
  20.  
  21. StringBuilder b = new StringBuilder(bytes.Length * 3 - 1);
  22. int pos = 0;
  23. while (pos < bytes.Length - 1000) {
  24. b.Append(BitConverter.ToString(bytes, pos, 1000)).Append('-');
  25. pos += 1000;
  26. }
  27. b.Append(BitConverter.ToString(bytes, pos));
  28. hexfield.Text = b.ToString();
Add Comment
Please, Sign In to add comment