Перевести байты в килобайты python

How can I convert bytes object to decimal or binary representation in python?

I wanted to convert an object of type bytes to binary representation in python 3.x. For example, I want to convert the bytes object b’\x11′ to the binary representation 00010001 in binary (or 17 in decimal). I tried this:

error struct.error: unpack requires a bytes object of length 2 

2 Answers 2

Starting from Python 3.2, you can use int.from_bytes .

Second argument, byteorder , specifies endianness of your bytestring. It can be either ‘big’ or ‘little’ . You can also use sys.byteorder to get your host machine’s native byteorder.

import sys int.from_bytes(b'\x11', byteorder=sys.byteorder) # => 17 bin(int.from_bytes(b'\x11', byteorder=sys.byteorder)) # => '0b10001' 

I tried finding the bit representation of a UUID using this method. When i used len function on that bit string, it came out to be 127. But UUID’s are 128 bit numbers. Can you please explain why the length calculated was 127?

Iterating over a bytes object gives you 8 bit ints which you can easily format to output in binary representation:

import numpy as np >>> my_bytes = np.random.bytes(10) >>> my_bytes b'_\xd9\xe97\xed\x06\xa82\xe7\xbf' >>> type(my_bytes) bytes >>> my_bytes[0] 95 >>> type(my_bytes[0]) int >>> for my_byte in my_bytes: >>> print(f'8b>', end=' ') 01011111 11011001 11101001 00110111 11101101 00000110 10101000 00110010 11100111 10111111 

A function for a hex string representation is builtin:

>>> my_bytes.hex(sep=' ') '5f d9 e9 37 ed 06 a8 32 e7 bf' 

Источник

Читайте также:  Naming constants in python

Python Code to Convert Bytes to KB, MB, GB, & TB

Python Code to Convert Bytes to KB, MB, GB, & TB | In this article, we will discuss how to convert bytes into KB, MB, GB, and TB in Python. We will also see Python code to convert bytes to GB i.e. how to convert bytes to GB in Python. So, let’s start from the beginning. Firstly, we should know a little bit about Byte, KB, MB, GB, and TB.

Byte:- A byte is a unit of data memory equal to eight bits, depending on whether it requires error correction. One byte is equivalent to eight bits. A byte is a unit most computers use to describe a character such as a number, letter, or typographic symbol. 1 Byte = 8 bits.

KiloByte (KB):- A KiloByte is a unit of computer information or memory equivalent to 1024 bytes. 1 KB = 1024 bytes.

MegaByte (MB):- A MegaByte is a unit of computer information or memory equivalent to 1,048,576 bytes. 1 MB = 1024 KB = 1024 * 1024 bytes = 1,048,576 bytes.

GigaByte (GB):- A GigaByte is a unit of computer information or memory that is approximately equivalent to 1 billion bytes. 1 GB = 1024 MB = 1024 * 1024 * 1024 bytes = 1,07,37,41,824 bytes.

TeraByte (TB):- A TeraByte is a unit of computer information or memory that is approximately equivalent to about 1 trillion bytes. 1 TB = 1024 GB = 1024 * 1024 * 1024 * 1024 bytes = 1.099511628×10 ¹² bytes.

Now let us see how can we convert Bytes to KB, MB, GB, and TB in Python programming language. Below we have developed separate programs for these conversions and also for Python code to convert bytes to GB.

Python Code to Convert Bytes to KB

Here, we will develop a program to convert bytes into KiloBytes in Python programming language. One KiloByte (KB) is equal to 1024 Bytes therefore one byte is equivalent to 1/1024 KBs.

1 KB = 1024 Bytes
1 Byte = 1/1024 KB

Python Code to Convert Bytes to KB

byte = int(input("Enter Bytes: ")) kb = byte/1024 print("<> Kilo Bytes".format(kb))

Outputs for different input values:-

Enter Bytes: 1024
1.0 Kilo Bytes

Enter Bytes: 99999
97.6552734375 Kilo Bytes

Python Code to Convert Bytes to MB

Here, we will develop a program to convert bytes into MegaBytes in Python programming language. One MegaByte (MB) is equal to 1024 KiloBytes, so one MB is equivalent to 1024 * 1024 bytes.

1 MB = 1024 KB
1 MB = 1024 * 1024 Bytes = 10,48,576 Bytes
1 Byte = 1/10,48,576 MB

byte = int(input("Enter bytes: ")) mb = byte/(1024 * 1024) print("<> MegaBytes".format(mb))

Outputs for different input values:-

Enter bytes: 9999
0.009535789489746094 MegaBytes

Источник

Python format size application (converting B to KB, MB, GB, TB)

I am trying to write an application to convert bytes to kb to mb to gb to tb. Here’s what I have so far:

The problem is, when I try the application I get everything after the decimal zeroing out. example size_format(623) yields ‘623B’ but with size_format(6200) , instead of getting ‘6.2kb’ I’m getting ‘6.0kb’. Any ideas why?

A hint for the future: when you paste in code, select it all and use the <> button to format it as code.

21 Answers 21

Fixed version of Bryan_Rch’s answer:

def format_bytes(size): # 2**10 = 1024 power = 2**10 n = 0 power_labels = while size > power: size /= power n += 1 return size, power_labels[n]+'bytes' 

This converts 12625 bytes into (12.3291015625, ‘megabytes’) instead it should be 0.01204 Megabytes am I missing someting?

def humanbytes(B): """Return the given bytes as a human friendly KB, MB, GB, or TB string.""" B = float(B) KB = float(1024) MB = float(KB ** 2) # 1,048,576 GB = float(KB ** 3) # 1,073,741,824 TB = float(KB ** 4) # 1,099,511,627,776 if B < KB: return ''.format(B,'Bytes' if 0 == B > 1 else 'Byte') elif KB KB'.format(B / KB) elif MB MB'.format(B / MB) elif GB GB'.format(B / GB) elif TB TB'.format(B / TB) tests = [1, 1024, 500000, 1048576, 50000000, 1073741824, 5000000000, 1099511627776, 5000000000000] for t in tests: print(" == ".format(t,humanbytes(t))) 
1 == 1.0 Byte 1024 == 1.00 KB 500000 == 488.28 KB 1048576 == 1.00 MB 50000000 == 47.68 MB 1073741824 == 1.00 GB 5000000000 == 4.66 GB 1099511627776 == 1.00 TB 5000000000000 == 4.55 TB 

and for future me here it is in Perl too:

sub humanbytes < my $B = shift; my $KB = 1024; my $MB = $KB ** 2; # 1,048,576 my $GB = $KB ** 3; # 1,073,741,824 my $TB = $KB ** 4; # 1,099,511,627,776 if ($B < $KB) < return "$B " . (($B == 0 || $B >1) ? 'Bytes' : 'Byte'); > elsif ($B >= $KB && $B < $MB) < return sprintf('%0.02f',$B/$KB) . ' KB'; >elsif ($B >= $MB && $B < $GB) < return sprintf('%0.02f',$B/$MB) . ' MB'; >elsif ($B >= $GB && $B < $TB) < return sprintf('%0.02f',$B/$GB) . ' GB'; >elsif ($B >= $TB) < return sprintf('%0.02f',$B/$TB) . ' TB'; >> 

@AdamMarples: this is a rich comparison chain, which was introduced in PEP 207. It translates to: «use the plural version, if B is not exactly one (1)». You resolve it from left to right: if B is zero or B is greater than 1

WARNING: other answers are likely to contain bugs. The ones posted before this one were unable to handle filesizes that are close to the boundary of the next unit.

Dividing bytes to get a human-readable answer may seem easy, right? Wrong!

Many answers are incorrect and contains floating point rounding bugs that cause incorrect output such as «1024 KiB» instead of «1 MiB». They shouldn’t feel sad about it, though, since it’s a bug that even Android’s OS programmers had in the past, and tens of thousands of programmer eyes never noticed the bug in the world’s most popular StackOverflow answer either, despite years of people using that old Java answer.

So what’s the problem? Well, it’s due to the way that floating point rounding works. A float such as «1023.95» will actually round up to «1024.0» when told to format itself as a single-decimal number. Most programmers don’t think about that bug, but it COMPLETELY breaks the «human readable bytes» formatting. So their code thinks «Oh, 1023.95, that’s fine, we’ve found the correct unit since the number is less than 1024», but they don’t realize that it will get rounded to «1024.0» which SHOULD be formatted as the NEXT size-unit.

Furthermore, many of the other answers are using horribly slow code with a bunch of math functions such as pow/log, which may look «neat» but completely wrecks performance. Most of the other answers use crazy if/else nesting, or other performance-killers such as temporary lists, live string concatenation/creation, etc. In short, they waste CPU cycles doing pointless, heavy work.

Most of them also forget to include larger units, and therefore only support a small subset of the most common filesizes. Given a larger number, such code would output something like «1239213919393491123.1 Gigabytes», which is silly. Some of them won’t even do that, and will simply break if the input number is larger than the largest unit they’ve implemented.

Furthermore, almost none of them handle negative input, such as «minus 2 megabytes», and completely break on such input.

They also hardcode very personal choices such as precision (how many decimals) and unit type (metric or binary). Which means that their code is barely reusable.

So. okay, we have a situation where the current answers aren’t correct. so why not do everything right instead? Here’s my function, which focuses on both performance and configurability. You can choose between 0-3 decimals, and whether you want metric (power of 1000) or binary (power of 1024) representation. It contains some code comments and usage examples, to help people understand why it does what it does and what bugs it avoids by working this way. If all the comments are deleted, it would shrink the line numbers by a lot, but I suggest keeping the comments when copypasta-ing so that you understand the code again in the future. 😉

from typing import List, Union class HumanBytes: METRIC_LABELS: List[str] = ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"] BINARY_LABELS: List[str] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"] PRECISION_OFFSETS: List[float] = [0.5, 0.05, 0.005, 0.0005] # PREDEFINED FOR SPEED. PRECISION_FORMATS: List[str] = ["<> <>", "<> <>", "<> <>", "<> <>"] # PREDEFINED FOR SPEED. @staticmethod def format(num: Union[int, float], metric: bool=False, precision: int=1) -> str: """ Human-readable formatting of bytes, using binary (powers of 1024) or metric (powers of 1000) representation. """ assert isinstance(num, (int, float)), "num must be an int or float" assert isinstance(metric, bool), "metric must be a bool" assert isinstance(precision, int) and precision >= 0 and precision = 1023.95" will # be rounded to "1024.0". Obviously we don't want ugly output such # as "1024.0 KiB", since the proper term for that is "1.0 MiB". break if unit != last_label: # We only shrink the number if we HAVEN'T reached the last unit. # NOTE: These looped divisions accumulate floating point rounding # errors, but each new division pushes the rounding errors further # and further down in the decimals, so it doesn't matter at all. num /= unit_step return HumanBytes.PRECISION_FORMATS[precision].format("-" if is_negative else "", num, unit) print(HumanBytes.format(2251799813685247)) # 2 pebibytes print(HumanBytes.format(2000000000000000, True)) # 2 petabytes print(HumanBytes.format(1099511627776)) # 1 tebibyte print(HumanBytes.format(1000000000000, True)) # 1 terabyte print(HumanBytes.format(1000000000, True)) # 1 gigabyte print(HumanBytes.format(4318498233, precision=3)) # 4.022 gibibytes print(HumanBytes.format(4318498233, True, 3)) # 4.318 gigabytes print(HumanBytes.format(-4318498233, precision=2)) # -4.02 gibibytes 

By the way, the hardcoded PRECISION_OFFSETS is created that way for maximum performance. We could have programmatically calculated the offsets using the formula unit_step_thresh = unit_step — (0.5/(10**precision)) to support arbitrary precisions. But it really makes NO sense to format filesizes with massive 4+ trailing decimal numbers. That’s why my function supports exactly what people use: 0, 1, 2 or 3 decimals. Thus we avoid a bunch of pow and division math. This decision is one of many small attention-to-detail choices that make this function FAST. Another example of performance choices was the decision to use a string-based if unit != last_label check to detect the end of the List, rather than iterating by indices and seeing if we’ve reached the final List-index. Generating indices via range() or tuples via enumerate() is slower than just doing an address comparison of Python’s immutable string objects stored in the _LABELS lists, which is what this code does instead!

Sure, it’s a bit excessive to put that much work into performance, but I hate the «write sloppy code and only optimize after all the thousands of slow functions in a project makes the whole project sluggish» attitude. The «premature optimization» quote that most programmers live by is completely misunderstood and used as an excuse for sloppiness. 😛

I place this code in the public domain. Feel free to use it in your projects, both freeware and commercial. I actually suggest that you place it in a .py module and change it from a «class namespace» into a normal module instead. I only used a class to keep the code neat for StackOverflow and to make it easy to paste into self-contained python scripts if you don’t want to use modules.

Источник

Оцените статью