JavaScriptのASCIIから16進数および16進数からASCIIへのコンバータークラス
問題
関数をメンバーにする必要があるJavaScriptクラスを作成する必要があります-
- toHex:ASCII文字列を受け取り、16進数に相当するものを返します。
- toASCII:16進文字列を受け取り、それに相当する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
-
HTMLとJavaScriptを使用してウェイトコンバーターを作成するにはどうすればよいですか?
HTMLとJavaScriptを使用してウェイトコンバーターを作成するには、コードは次のとおりです- 例 <!DOCTYPE html> <html> <head> <style> body{ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; } input,span{ font-size: 20p
-
JavascriptのTextDecoderとTextEncoder?
TextEncoderは、指定された文字列をutf-8標準に変換するために使用されます。文字列からUint8Arrayを再調整します。 TextDecoderは、バイトのストリームをコードポイントのストリームに変換するために使用されます。 UTF-8、ISO-8859-2、KOI8-R、GBKなどをデコードできます。 以下は、JavaScriptのTextDecoderとTextEncoderのコードです- 例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="