Any suggestions? Thanks!īest way is to keep a copy of the original ASCII and only print the binary.ĭo you want to save the output string or just print it? in the code, must initialize the bits to 0. Int the_bit = bit_string - '0' // convert from char representation '0', '1' to 0, 1ĭest_char |= the_bit<<(7-source_bit_pos) Here’s an example: char bit_string = "01001000" // a single byte in string representation, visible value 48HexĬhar dest_char = 0 // the output, 'H' - must initialize all bits to 0įor (int source_bit_pos = 7 source_bit_pos >= 0 source_bit_pos-) // start from rightmost position What you need in order to put a bit at a certain position within a byte is a combination of bitwise shift and bitwise or. The logic of the conversion is this: in order to convert an array of chars-representing-bits into a byte containing 8 bits you need to reverse the bit order*. Fixed length (8 bit) chars allow you to start from the beginning and not caring for the length. Having variable-length chars means that you need to start from the end of the string and proceed backwards until you find a space, filling the remaining bits with zeroes. A better string representation would be like: Test.Hi, it’s not difficult but you are making it slightly more complicated by having variable-length chars (7 and 6 bits), spaces, and no trailing zeroes. Random_string_binary = ''.join( ''.format(ord(c)) for c in random_string) # Must handle random string random_string = ''.join(cįor c in random.choice( 'qwertyuiopasdfghjklzxcvbnm0123456789')) emotions ran high!', 'Must handle special charaters') Test.assert_equals(binary_to_string( ''), '', 'Must handle empty string')
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |