সমস্যা
আমাদের একটি জাভাস্ক্রিপ্ট ক্লাস লিখতে হবে যার সদস্য ফাংশনগুলি আছে −
- 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