Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Silly excersise
Message
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Title:
Environment versions
Environment:
C# 2.0
OS:
Windows XP SP2
Network:
Windows 2000 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01500484
Message ID:
01500503
Views:
35
>>>>Hi All, just messing around - not a work thing - if I have a string of binary digits such as
>>>>
>>>>
>>>>string s = "0101000001000101010101000100010101010010"
>>>>
>>>>
>>>>how do I convert it to text ?, all the Convert.* methods I've looked at require a byte[] array as an arg and I don't know how to create one !
>>>
>>>Pete,
>>>
>>>I'm not sure whether it's the best way to go , and I have not tested it either, but
>>>
>>>(1) initialize a BitArray
>>>(2) loop throught the string and assign the elements of the bitArray depending on 0 or 1
>>>(3) initialize a Byte array
>>>(4) I see that the BitARray has a CopyTo method wich accepts a Byte array
>>>http://msdn.microsoft.com/en-us/library/system.collections.bitarray.copyto.aspx
>>>
>>>
>>>Alternatively, you can directly create a byte array and loop through the string
>>>
>>>
>>>if( string[stringindex] == '1' ) {
>>>	the byte index = stringIndex / 8
>>>	the bit to set =  7 - (stringIndex %8),
>>>
>>>	// you may have to cast the right hand side to (byte)
>>>
>>>	byteArray[ stringIndex / 8] |= ( 1 <<  (  7 - (stringIndex %8)))
>>>
>>>	or maybe due to widening
>>>	byteArray[ stringIndex / 8]  = (byte)( byteArray[ stringIndex / 8]  | byteArray[ stringIndex / 8] | ( 1 <<  (  7 - (stringIndex %8))))
>>>}
>>>
>>
>>Hi Greg, thanks for your time - if you see my reply to Neil I can create the byte array but the ConvertToBase64String method returns rubbish. I read the M$ article and can't really see the point of BitArrays ? - maybe that's me :-(
>
>Try this:
string result = string.Empty;
>            for (int i = 0; i < s1.Length; i += 8)
>            {
>                 result += Convert.ToChar(Convert.ToInt32(s1.Substring(i, 8), 2));
>            }
Hi Viv, that's what I did in the end thanks for the heads up ( as you can probably gather I'm not very busy )
Regards,
Peter J. Kane



Pete
Previous
Reply
Map
View

Click here to load this message in the networking platform