![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3
![]() |
String of bits to byte[]
how can i convert a string of bit String S = "01010101010101010101"
into a byte b[] dont worry about left over bits.. assume that the size of S is a multiple of 8-bits |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
Re: String of bits to byte[]
Start with a byte of 0. Traverse each character from left to right, shifting the byte left with
<< 1. Each time you encounter a bit that's 1, set the first bit with | 1.Do this for each grouping of 8 bits to get your byte[]. For example, given the string "01010101010101010101": Byte Operation String During Iteration -------------------------------------------------- 0 (Shift << 1) 01010101010101010101 0 (Don't Set ) 0 (Shift << 1) 1010101010101010101 1 (Set | 1) 10 (Shift << 1) 010101010101010101 10 (Don't Set ) 100 (Shift << 1) 10101010101010101 101 (Set | 1) 1010 (Shift << 1) 0101010101010101 1010 (Don't Set ) 10100 (Shift << 1) 101010101010101 10101 (Set | 1) Etc etc ... |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| An Attempt at a DBMS | grimpirate | PHP | 8 | Apr 17th, 2007 1:01 PM |
| Throwing an exception when using string constructor | csrocker101 | C# | 3 | Apr 8th, 2007 2:04 PM |
| Help with breaking apart a string | csrocker101 | C# | 6 | Apr 6th, 2007 7:50 AM |
| Function Parameters | grimpirate | PHP | 10 | Mar 14th, 2007 6:55 PM |
| Problems with String to MD5 Conversion | emdiesse | Visual Basic .NET | 0 | Feb 2nd, 2006 10:25 AM |