← Back to Node.js Course | Chapter 2: Core Modules | Lesson 7 of 7

Buffer के Basics

एक Buffer raw bytes का एक fixed-size chunk है, files और network packets जैसे binary data के लिए इस्तेमाल होता है।

In this page:

  1. Buffer Basics
Syntax
javascript
const buf = Buffer.from('text', 'utf8');
const empty = Buffer.alloc(size);
buf.toString('hex');

Buffer Basics

Buffer.from एक string, array, या दूसरे buffer से एक buffer बनाता है, और Buffer.alloc एक zero-filled वाला बनाता है। toString bytes को वापस text में convert करता है utf8, hex, या base64 जैसी एक encoding इस्तेमाल करते हुए। Buffers Uint8Array की एक subclass हैं।

Note: Buffer.alloc old Buffer constructor से safer है क्योंकि यह memory zero-fill करता है।

उदाहरण: Buffer basics

javascript
const b = Buffer.from("héllo", "utf8");
console.log("chars:", "héllo".length, "bytes:", b.length);
console.log(b.toString("hex"));
console.log(Buffer.from("Node").toString("base64"));
console.log(Buffer.alloc(3));

// Output:
// chars: 5 bytes: 6
// 68c3a96c6c6f
// Tm9kZQ==
// <Buffer 00 00 00>

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. deprecated new Buffer() इस्तेमाल करना
  2. string length और byte length mix up करना
  3. convert करते वक्त encoding भूल जाना
चैप्टर सारांश
  • Buffer raw bytes hold करता है
  • from और alloc buffers बनाते हैं
  • toString एक encoding लेता है
  • Buffers Uint8Arrays हैं
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.