Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6752809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:05:17+00:00 2026-05-26T13:05:17+00:00

I have a program that converts a byte[] to a string of hex: byte[]

  • 0

I have a program that converts a byte[] to a string of hex:

byte[] bytes = File.ReadAllBytes(infile);

try
{
    StringBuilder sb = new StringBuilder(BitConverter.ToString(bytes)); // <--exception 
    hexfield.Text = sb.ToString();  
}

catch(Exception e) 
{
    MessageBox.Show(e.ToString()); 
} 

This works fine for most case. But when I use a huge file, for example a 103 MB .flv video file it runs out of memory it throws an exception:

System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
at System.String.CtorCharArrayStartLength(Char[] value, Int32 startIndex, Int32 length)
at System.BitConverter.ToString(Byte[] value, Int32 startIndex, Int32 length)
at System.BitConverter.ToString(Byte[] value)
 at shex.shexx.hexfield_Dragrop(Object sender, DragEventArgs e)** 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:05:17+00:00Added an answer on May 26, 2026 at 1:05 pm

    The BitConverter.ToString method first creates a char array, then creates a string from that, so you would need room for the string twice in memory.

    You can convert the data in smaller chunks, and create the StringBuilder with an initial size so that it has all the room it needs. This way the large string exists only once in memory:

    StringBuilder b = new StringBuilder(bytes.Length * 3 - 1);
    int pos = 0;
    while (pos < bytes.Length - 1000) {
      b.Append(BitConverter.ToString(bytes, pos, 1000)).Append('-');
      pos += 1000;
    }
    b.Append(BitConverter.ToString(bytes, pos));
    hexfield.Text = b.ToString();
    

    However, note the comment from sehe about the usefulness of such a huge string. The best solution might actually be to not create the entire string at all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that convert image file to binary and also it converts
I have a program that handles byte arrays in Java, and now I would
I have this program that is reading the test string from a textbox and
I have a Java program that spits out, in space-separated hexadecimal format, 16 bytes
I have a byte array that has hex values and I initially put those
i'm having problems creating a program that converts a string of numbers into an
I have a PHP program that encrypts a PDF file into .xxx file this
I have code written that converts my Document to a string prior to printing
I have a program that uses gethostbyname (in Windows) in order to convert IP
I have a VB6 program that someone recently helped me convert to VB.NET In

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.