NodeJS Program to Hex To Buffer with Explanation
NodeJS
Medium
Streams & Buffers
35 views
1 min read
80 words
This problem helps you practice core NodeJS fundamentals in a practical way. It builds intuition around hex, utf8, buffer. Let’s break it down step by step so you can implement it confidently.
Problem Statement
Read a hex string and print the utf8 text.
Input Format
stdin: one hex string.
Output Format
Print utf8.
Constraints
Assume valid hex.
Code Solution
This explanation is written for learning purposes and to help beginners understand the concept clearly.
const fs = require('fs');
const hex = fs.readFileSync(0, 'utf8').trim();
if (!hex) process.exit(0);
const buf = Buffer.from(hex, 'hex');
console.log(buf.toString('utf8'));
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Solution Guide
Problem
Read a hex string and print the utf8 text.
Input / Output
Input
stdin: one hex string.
Constraints
Assume valid hex.
Details
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Official Solution
const fs = require('fs');
const hex = fs.readFileSync(0, 'utf8').trim();
if (!hex) process.exit(0);
const buf = Buffer.from(hex, 'hex');
console.log(buf.toString('utf8'));
Solutions (0)
No solutions submitted yet. Be the first!