সমস্যা
আমাদের একটি জাভাস্ক্রিপ্ট ক্লাস লিখতে হবে যাতে দুটি মেম্বার ফাংশন থাকে –
toHex: এটি একটি ASCII স্ট্রিং নেবে এবং এর হেক্সাডেসিমেল সমতুল্য ফেরত দেবে।toASCII: এটি একটি হেক্সাডেসিমেল স্ট্রিং নেবে এবং এর ASCII সমতুল্য ফেরত দেবে।
উদাহরণস্বরূপ, যদি ইনপুট হয় –
ইনপুট
const str = 'this is a string';
তবে সংশ্লিষ্ট হেক্স এবং ASCII হবে –
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