0

I am trying to convert a byte array to a string, all i need is to simply convert the number in byteArray to string Foreg. "12459865..."

I am trying to do this with this:

fileInString = Encoding.UTF8.GetString(fileInBytes, 0, fileInBytes.Length);

fileInBytes looks like this: 1212549878563212450045.... But resultant fileInString looks like this ID3\0\0\0\0JTENC\0\0\0@\0\0WXXX\0... and alot of weird characters.

I tried different Encoding styles including default but all insert some weird characters into it.

The only option I have is to loop and cast each member into the string

while (currbyte != -1)
{
    currbyte = fileStream.ReadByte();
    //fileInBytes[i++] = (byte)currbyte;
    fileInString += currbyte.ToString();
    progressBar1.Value = i++;
}

But this is TOO slow. Please tell me how can I convert by byte array into string using Encoding.....GetString

4
  • How do you get your bytes like this 1212549878563212450045? could you separate it to each byte? such as 12|12|54|98 ... or 1|2||1|2|5|4|9|8...? Commented Nov 23, 2013 at 14:40
  • i have updated my question to explain how i made the fileInBytes array its basically one like here fileInBytes[i++] = (byte)currbyte; Commented Nov 23, 2013 at 14:42
  • It's slow maybe partly because of your progressBar1.Value = i++;, any UI updating related code will slow everything down, you should update the progressbar in a larger interval of time to reduce this or even in some case we don't even need any exact info on the current process, just some working notification is enough. Commented Nov 23, 2013 at 15:05
  • Go with the answer from Tim S. but fileInString += is slow as string is immutable. If you going down the path should use StringBuilder. And why would you have a progress bar on an operation that should only take a fraction of a second? Commented Nov 23, 2013 at 15:23

3 Answers 3

0

If you want to encode a byte[] as a string, base64 encoding with Convert's ToBase64String and FromBase64String methods is a good way to do this:

string fileInString = Convert.ToBase64String(fileInBytes);
byte[] asBytesAgain = Convert.FromBase64String(fileInString);

Your encoding using fileInString += currbyte.ToString(); appears to be an ambiguous base 10 encoding. E.g. from what I can tell, the arrays with values { 1, 10, 255 } and { 110, 255 } would be encoded the same: "110255", and so cannot be unambiguously changed back into a byte[].

Sign up to request clarification or add additional context in comments.

Comments

0

There is no encoding that can achieve what you want.

You need to loop through the bytes as you do. To make it faster, use a StringBuilder and don't update the ProgressBar too often:

var builder = new StringBuilder();

for (var i = 0; i < fileStream.Length; i++)
{
    builder.Append(fileStream.ReadByte().ToString());

    if (i % 1000 == 0) //update the ProgressBar every 1000 bytes for example
    {
        progressBar.Value = i;
    }
}

var result = builder.ToString();

Comments

0

Well, since you are referring to a byte ARRAY, fileInBytes really can't look like '1212549878563212450045', but rather like this:

{byte[22]}
     [0]: 1
     [1]: 2

     etc.

If that is indeed the case you should be able fileInString using StringBuilder as follows:

StringBuilder sb = new StringBuilder();
foreach (var b in fileInBytes)
{
    sb.Append(b.ToString());
}
fileInString = sb.ToString();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.