समस्या
हमें एक जावास्क्रिप्ट वर्ग लिखना है जो सदस्य कार्यों के लिए है -
- toHex:यह एक ASCII स्ट्रिंग लेता है और इसके हेक्साडेसिमल समकक्ष को लौटाता है।
- toASCII:यह एक हेक्साडेसिमल स्ट्रिंग लेता है और इसके ASCII समकक्ष को लौटाता है।
उदाहरण के लिए, यदि फ़ंक्शन का इनपुट है -
इनपुट
const str = 'this is a string';
तब संबंधित हेक्स और एएससीआई होना चाहिए -
74686973206973206120737472696e67 this is a string
उदाहरण
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)); आउटपुट
74686973206973206120737472696e67 this is a string