6 Replies - 3750 Views - Last Post: 23 November 2012 - 08:33 AM Rate Topic: -----

#1 Darith1974  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-November 12

Convert a stringbuilder of binary to a unicode string?

Posted 19 November 2012 - 01:42 PM

I have a string builder consisting binary characters e.g. 101010101. How do i convert this to a unicode string,RetreivedMessage is my string builder pulled from a image; i have been experimenting with the above code but it always returns the same value?
string FinalRetreivedMessage  = RetreivedMessage.ToString();
            //Byte[] buf = Encoding.Unicode.GetBytes(RetreivedMessage.ToString());
            Byte[] buf = Encoding.Unicode.GetBytes(RetreivedMessage.ToString());
            string result = System.Text.Encoding.Unicode.GetString(buf);
            //String result = Encoding.Unicode.GetString(buf);
            StringBuilder r2 = new StringBuilder();
            //foreach (Byte b in Encoding.Unicode.GetBytes(FinalRetreivedMessage))
           // {
           //    r2.Append(Convert.ToString());
            //}


Is This A Good Question/Topic? 0
  • +

Replies To: Convert a stringbuilder of binary to a unicode string?

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Convert a stringbuilder of binary to a unicode string?

Posted 19 November 2012 - 02:06 PM

Strings are made of characters, so their binary representation (your GetBytes) is going to be the values of the characters, not the bits 1 and 0. You'll need to convert the text to a binary value and then do the conversion.

int v = 0;
for (int i = 0; i < 32; i++) {
    v = v * 2 + (FinalRetreivedMessage[i] == '0' ? 0 : 1);
}

This will convert a 32 'bit' value stored in the string into the integer value it represents. You can then convert that to Unicode. Change the loop limit to 8 or 16 if you are using 8 or 16 bit unicode.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3666
  • View blog
  • Posts: 11,497
  • Joined: 05-May 12

Re: Convert a stringbuilder of binary to a unicode string?

Posted 19 November 2012 - 05:39 PM

Unicode's Basic Multilingual Plane is at least 16 bits, so you can't use just 8 bits.

I think what Momerath meant is that 8 bits is enough to get the encoding characters of UTF-8, but remember that .NET keeps things in memory as as UTF-16, so you'll need to convert from UTF-8 to UTF-16 at the very least.
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,500
  • Joined: 29-May 08

Re: Convert a stringbuilder of binary to a unicode string?

Posted 19 November 2012 - 10:17 PM

UTF-7 is 7 bits.
Was This Post Helpful? 1
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3666
  • View blog
  • Posts: 11,497
  • Joined: 05-May 12

Re: Convert a stringbuilder of binary to a unicode string?

Posted 20 November 2012 - 08:00 AM

True, but the UTF-7 bits still need to be decoded into a 16 bit value that CLR can use internally.
Was This Post Helpful? 0
  • +
  • -

#6 Darith1974  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-November 12

Re: Convert a stringbuilder of binary to a unicode string?

Posted 21 November 2012 - 03:00 AM

Thanks for the updates everybody; i have not worked on this project in the last few days, hope to update this thread soon.
Was This Post Helpful? 0
  • +
  • -

#7 Darith1974  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-November 12

Re: Convert a stringbuilder of binary to a unicode string?

Posted 23 November 2012 - 08:33 AM

string FinalRetreivedMessage = RetreivedMessage.ToString();
            int count = FinalRetreivedMessage.Length / 8;
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
            UTF8Encoding enc = new UTF8Encoding();
            var StringBytes = new byte[count];
            for (int i = 0; i < count; i++)
                StringBytes[i] = Convert.ToByte(FinalRetreivedMessage.Substring(i * 8, 8), 2);
            //StringBytes[i] = encoding.GetBytes(FinalRetreivedMessage.Substring(i * 8, 8), 2);
            string FinalResult = enc.GetString(StringBytes);


The above code solved my issue
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1