0

Let's say I have a byte array

Byte[] arr;

Then I will convert the array to string 1st.

String inputString = "";
foreach (Byte b in arr)
{
   if (b != 0)
      inputString += (Char) b;
   else inputString += " ";
}

Let's say the string is:

inputString  = @"C:\Program Files\Test C:\Users\User A\AppData\Local\Temp C 32323 C:\Program Files\Test\Temp";

I want it to be split into 4 strings that look like below:

C:\Program Files\Test \\position 0 = test folder
C:\Users\User A\AppData\Local\Temp \\position 1 = windows temp folder
C 32323 \\position 2 = a name. It can be C2313 or C 2312 or whatever string
C:\Program Files\Test\Temp \\position 3 = temp for test folder
\\ position can change by me...

every string in between will be split by space. That's mean I can use .Split(' '). However, as you know some of the path has space in between, 'C:\Program Files\Test' is an example.

How can I get the values I want?

11
  • 1
    Are you sure the original byte[] didn't have a \0 between the various paths? Try changing inputString += " " to inputString += "|" Commented May 14, 2015 at 10:02
  • 1
    substrings C:\Users\User A\AppData\Local\Temp and C 32323? following the pattern, it should be C:\Users\User A\AppData\Local\Temp C 32323 folder, isn't it? Commented May 14, 2015 at 10:02
  • @xanatos it will surely have more than one \0 Commented May 14, 2015 at 10:03
  • 1
    Is it the 0 from the byte array that marks the split? Commented May 14, 2015 at 10:06
  • 1
    Sorry, this doesn't make it any clearer. If any string can be considered as valid, how can you decide if Files\Test is a part of C:\Program Files\Test or if it's a standalone string (and so is C:\Program)? Commented May 14, 2015 at 10:25

2 Answers 2

1

Try doing this:

byte[] arr = ...

string[] inputStrings = Encoding.GetEncoding("iso-8859-1").GetString(arr).Split('\0');

And see the result.

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

4 Comments

the array inputStrings contains a lot of empty strings.
Try posting the result array... I can't debug with this informations.
@kingjia You could post the base64 version of the byte[] arr: string arrToString = Convert.ToBase64String(arr)
Problem solved. Thanks for your advise to use inputString += "|".
0

Here is my answer. Thanks @xanatos for the advise.

String inputString = "";
bool isRepeat = false;
foreach (Byte b in arr)
{
   if (b != 0)
   {
      inputString += (Char)b;
      isRepeat = false;
   }
   else
   {
      if (!isRepeat)
      {
         inputString += "|";
         isRepeat = true;
      }
   }               
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.