문제
우리는 멤버 함수를 가져야 하는 JavaScript 클래스를 작성해야 합니다 -
- toHex:ASCII 문자열을 받아서 해당하는 16진수를 반환합니다.
- toASCII:16진수 문자열을 받아 ASCII에 해당하는 문자열을 반환합니다.
예를 들어, 함수에 대한 입력이 -
인 경우입력
const str = 'this is a string';
그러면 각각의 16진수와 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