
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
ASCII to hex and hex to ASCII converter class in JavaScript
Problem
We are required to write a JavaScript class that have to member functions −
- toHex: It takes in a ASCII string and returns its hexadecimal equivalent.
- toASCII: It takes in a hexadecimal string and returns its ASCII equivalent.
For example, if the input to the function is −
Input
const str = 'this is a string';
Then the respective hex and ascii should be −
74686973206973206120737472696e67 this is a string
Example
const str = 'this is a string'; class Converter{ toASCII = (hex = '') => { const res = []; for(let i = 0; i < hex.length; i += 2){ res.push(hex.slice(i,i+2)); }; return res .map(el => String.fromCharCode(parseInt(el, 16))) .join(''); }; toHex = (ascii = '') => { return ascii .split('') .map(el => el.charCodeAt().toString(16)) .join(''); }; }; const converter = new Converter(); const hex = converter.toHex(str); console.log(hex); console.log(converter.toASCII(hex));
Output
74686973206973206120737472696e67 this is a string
- Related Articles
- Hex to ASCII conversion in 8051
- 8085 Program to convert ASCII to HEX
- 8085 Program to convert HEX to ASCII
- Program to convert ASCII to HEX in 8085 Microprocessor
- Program to convert HEX to ASCII in 8085 Microprocessor
- Conversion of four-digit hex to ASCII in 8051
- Convert Hex to ASCII Characters in the Linux Shell
- 8085 Program to convert two-digit hex to two ASCII values
- Program to convert two-digit hex to two ASCII values in 8085 Microprocessor
- How to Convert Hex String to Hex Number in C#?
- Converting ASCII to hexadecimal in JavaScript
- Converting to hex and summing the numeral part in JavaScript
- Generating random hex color in JavaScript
- JavaScript to generate random hex codes of color
- Check for Valid hex code - JavaScript

Advertisements