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());
//}
6 Replies - 870 Views - Last Post: 23 November 2012 - 08:33 AM
#1
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?
Replies To: Convert a stringbuilder of binary to a unicode string?
#2
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.
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.
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.
#3
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.
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.
#4
Re: Convert a stringbuilder of binary to a unicode string?
Posted 19 November 2012 - 10:17 PM
UTF-7 is 7 bits.
#5
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.
#6
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.
#7
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
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|