JavaScript Convert bytes to human-readable string Last Updated : 12 Jun, 2023 Comments Improve Suggest changes Like Article Like Report Given the size of a file (in Bytes), the task is to convert it into human-readable form using JavaScript. Here are a few methods to discuss. Example 1: This example converts the file Size(in Bytes) into human-readable form. It shows the values in decimal and for less than 1024 Bytes, it remains in Bytes. JavaScript let size = function (bytes) { if (bytes === 0) { return "0.00 B"; } let e = Math.floor(Math.log(bytes) / Math.log(1024)); return (bytes / Math.pow(1024, e)).toFixed(2) + ' ' + ' KMGTP'.charAt(e) + 'B'; } let bytes = 2007777777770; console.log(bytes + " bytes = " + size(bytes)); Output2007777777770 bytes = 1.83 TB Example 2: This example converts the file Size(in Bytes) into human-readable form. It shows the values in decimal and for less than 1024 Bytes, it remains in Bytes. But, with a different approach. JavaScript function getSize(size) { let sizes = [' Bytes', ' KB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB']; for (let i = 1; i < sizes.length; i++) { if (size < Math.pow(1024, i)) return (Math.round((size / Math.pow( 1024, i - 1)) * 100) / 100) + sizes[i - 1]; } return size; } let bytes = 1024; console.log(bytes + " bytes = " + getSize(bytes)); Output1024 bytes = 1 KB Comment More infoAdvertise with us Next Article JavaScript Convert bytes to human-readable string P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads JavaScript - Convert Byte Array to String Here are the various methods to convert Byte Array to string in JavaScript.1. Using WebAPI TextDecoder.decode() MethodThe TextDecoder API is a modern and efficient way to convert a byte array (Uint8Array) to a string. Itâs supported in both browsers and Node.js.JavaScriptconst byteA = new Uint8Array 2 min read Convert base64 String to ArrayBuffer In JavaScript A Base64 string represents binary data in an ASCII string format by translating it into a radix-64 representation. Often used to encode binary data in text-based formats like JSON or HTML, it needs to be converted back into its original binary format for further processing. An ArrayBuffer in JavaScr 2 min read How to Get the Length of a String in Bytes in JavaScript ? In JavaScript, determining the length of a string in characters is straightforward using the length property. However, in many cases, particularly when dealing with file sizes, network protocols, or database storage, you might need to know the length of a string in bytes. This is because characters 3 min read JavaScript | Encode/Decode a string to Base64 To encode or decode strings in JavaScript, we can use the built-in functions provided by the language. These functions help in encoding special characters in a URL or decoding encoded strings back to their original form. 1. btoa() MethodThis method encodes a string in base-64 and uses the "A-Z", "a- 6 min read How to convert blob to base64 encoding using JavaScript ? Blob in JavaScript is a fundamental data type representing bytes of data. It serves as the underlying structure for files and is supported by web browsers, capable of storing and retrieving data from memory. Blobs have specific sizes and file types like regular files and can be handled with Buffers, 2 min read JavaScript Program to Convert String to Bytes In this article, we are going to learn ho to Convert String to bytes in JavaScript. Converting a string to bytes in JavaScript involves encoding the characters using a specific character encoding (such as UTF-8) to represent the string as a sequence of bytes. There are several methods that can be us 3 min read JavaScript Program to Convert Byte Array to JSON In this article, we are going to learn about the conversion of a Byte Array into JSON. Converting a byte array to JSON means transforming a sequence of bytes into a structured JSON format, often involving decoding bytes to a text string and then parsing it into JSON data.Example:Input : [71, 101, 10 3 min read How to Convert Byte Array to String in PHP? Given a Byte Array, the task is to convert the byte array to a String in PHP. It is used in various scenarios, such as processing binary data, handling file uploads, or working with data transmission protocols. Below are the approaches to convert byte array to string in PHP:Table of ContentWhat is a 3 min read Convert a String to a ByteBuffer in Java In Java, ByteBuffer can be used to perform operations at the Byte level one more thing is this class provides different types of methods for reading writing, and manipulating bytes in a structured way only. In this article, we will learn about String to ByteBuffer in Java. Java Program to Convert St 4 min read Java Program to Convert String to Byte Array Using getBytes() Method In Java, strings are objects that are backed internally by a char array. So to convert a string to a byte array, we need a getByte() method. It is the easiest way to convert a string to a byte array. This method converts the given string to a sequence of bytes using the platform's default charset an 2 min read Like