-1

I have string with 0's and 1's. It looks like this:

1110011011010000...

With spaces for better visualisation (original string from my program doesn't contain spaces)

11100110 11010000 ...

Every eight elements must be a letter of Unicode in binary.

What i need to do, is put every last 0 or 1 of each letter (every 8th element in full string) to the first place for this letter, so this string would be like this:

0111001101101000...

With spaces for better visualisation (original string from my program doesn't contain spaces)

01110011 01101000 ...

How can I do this in C#?

Errorfreak
  • 79
  • 2
  • 9
  • 1
    What have you tried? Have a look at [C# string members](https://msdn.microsoft.com/en-us/library/system.string_methods(v=vs.110).aspx) – LJᛃ Feb 25 '15 at 18:07
  • You might want to just use hex values, use the >> or << operators to role the bits left or right and convert back into binary string representation for your output, if that's how you need to use it. – TechneWare Feb 25 '15 at 18:11

5 Answers5

1

Start by splitting your string into chunks of size eight (here is how).

This gives you an IEnumerable<string>. Take each string of length 8, and recombine its characters like this:

string orig = "11100110";
string res = orig.Substring(1)+orig[0];

Finally, merge back the parts processed in the above way to get your final string (here is how).

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

strings are immutable so i would use StringBuilder to move characters

    var s =  "1110011011010000";
    var window = 8; // the tail of string which is shoreter than 8 symbols (if any) will be ignored
    System.Text.StringBuilder sb = new System.Text.StringBuilder(s);
    for(int i = 0; i <= s.Length-window; i=i+window)
    {
        char c = sb[i+window-1];
        sb.Remove(i+window-1,1);
        sb.Insert(i,c);         
    }
    string result = sb.ToString();
ASh
  • 34,632
  • 9
  • 60
  • 82
1

Here's my solution. The method ShiftChunkChars takes a string and integer which depicts the amount of characters in each "chunk". Beware that this method does not work if the input string's length is not divisible by chunkSize.

string ShiftChunkChars(string original, int chunkSize)
{
    char[] buffer = new char[chunkSize];
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < original.Length; i += chunkSize)
    {
        char[] chars = original.Substring(i, chunkSize).ToCharArray();
        Array.Copy(chars, 0, buffer, 1, chunkSize - 1);
        buffer[0] = chars[chunkSize - 1];
        sb.Append(buffer);
    }
    return sb.ToString();
}

Examples:

1.

void Main()
{
    int chunkSize = 8;
    string s = "11100110110100000111001101101000";
    ShiftChunkChars(s, chunkSize);
}

Output:

01110011011010001011100100110100
// Original with spaces: 11100110 11010000 01110011 01101000
// Result with spaces:   01110011 01101000 10111001 00110100

2.

void Main()
{
    int chunkSize = 4;
    string s = "11100110110100000111001101101000";
    ShiftChunkChars(s, chunkSize);
}

Output:

01110011111000001011100100110100
// Original with spaces: 1110 0110 1101 0000 0111 0011 0110 1000
// Result with spaces:   0111 0011 1110 0000 1011 1001 0011 0100
cbr
  • 12,563
  • 3
  • 38
  • 63
0

You should be able to do this very simple with Regex:

string formattedBinary = Regex.Replace("0111001101101000", ".{8}", "$0 ");
Icemanind
  • 47,519
  • 50
  • 171
  • 296
0

Try with this. Not sure if I have undestood the question. In this case I looped twice because I had 16 chars.

string s = "1110011011010000";
char[] nc = s.ToCharArray();

for (int i = 0; i < 2; i++)
{
    char tmp = nc[i * 8 + 7];
    for (int j = 6; j > 0; j--)
    {
        nc[i * 8 + j + 1] = nc[i * 8 + j];
    }
    nc[i * 8] = tmp;
}
s = new string(nc);
gvlasov
  • 18,638
  • 21
  • 74
  • 110
andrea
  • 1