python - Number to bytes -


I have a large number that basically has a binary wall of 2. How can I write those bits in a file? Can I read raw bytes and what can I create numbers without accumulating the actual number? Also, how can I decode a raw byte after coming into the file?

Binary: 0b101010011000010000 Decimal: 173584 Byte: ???

If a random, arbitrary long sequence of beats represents non-negative base number 2, So 8 groups of those bits can be considered on the number of base-256 numbers and they can be written as a string of 8-bit characters. After reading the string back to the process, String back to original number D Can be coded.

The following is what I have received from the question.

  def base256_encode (n): # for non-negative integer byte string if n & gt; 0: arr = [] while n: n, rem = divmod (n, 256) arr.append (chr (rm) returns) '' (Reverse (arr)) elif n == 0: return '\ x00' Else: increase valueError ("Negative value can not be converted into a byte string") def base256_decode (s): # (big-endian ) Return the number return for the byte string (Lambda A, C: A * 256 + ord (c), S, 0) def binrepr (s): # utility return '' .join (('{: 08b}' format Print ('c')) for c) n = 173584 print 'n:', n en = base256_encode (n) print 'binary bytes:', with binparap (n) open ('robots.bene', 'wb ') As outflow: Outfree. Written (N) with open (' rawbits.bin ',' rb ') Inf: print from file 'read:', base256_decode (inf.read ())  

Output:

 < Code> n: 173584 Binary bytes: 00000010 10100110 00010000 Read by file: 173584  

Comments

Popular posts from this blog

c# - SignalR: "Protocol error: Unknown transport." when navigating to hub -

android - how to get distance of 2 beacons and make a condtion base on that distance -

c# - WPF Expander overlay used in ItemsControl -