diff --git a/src/tools/text-to-binary/text-to-binary.models.ts b/src/tools/text-to-binary/text-to-binary.models.ts index ad9699af..4a17dd2e 100644 --- a/src/tools/text-to-binary/text-to-binary.models.ts +++ b/src/tools/text-to-binary/text-to-binary.models.ts @@ -1,4 +1,4 @@ -export { convertTextToAsciiBinary, convertAsciiBinaryToText }; +export { convertTextToAsciiBinary, convertAsciiBinaryToText, convertTextToUnicodeBinary, convertUnicodeBinaryToText, convertTextToUtf8Binary, convertUtf8BinaryToText }; function convertTextToAsciiBinary(text: string, { separator = ' ' }: { separator?: string } = {}): string { return text @@ -20,3 +20,45 @@ function convertAsciiBinaryToText(binary: string): string { .map(binary => String.fromCharCode(Number.parseInt(binary, 2))) .join(''); } + +function convertTextToUnicodeBinary(text: string, { separator = '' }: { separator?: string } = {}) { + return text.split('').map((char) => { + const code = char.charCodeAt(0); + if (code > 127) { + const charUnicode = code.toString(16); + return `\\u${charUnicode}`; + } + else { + return char; + } + }).join(separator); +}; + +function convertUnicodeBinaryToText(binary: string) { + const character = binary.split('\\u'); + const native = character[0];// need to remove this char + return native + character.map((code, idx) => { + if (idx === 0) { + return ''; + } + let strValue = String.fromCharCode(Number.parseInt(`0x${code.substring(0, 4)}`)); + if (code.length > 4) { + strValue += code.substring(4, code.length); + } + return strValue; + }).join(''); +} + +function convertTextToUtf8Binary(text: string) { + // eslint-disable-next-line no-control-regex + return text.replace(/[^\u0000-\u00FF]/g, + ($0) => { + return escape($0) + .replace(/(%u)(\w{4})/gi, '$2;'); + }); + // return EncodeUtf8(text); +}; + +function convertUtf8BinaryToText(binary: string) { + return unescape(binary.replace(//g, '%u').replace(/;/g, '')); +} diff --git a/src/tools/text-to-binary/text-to-binary.vue b/src/tools/text-to-binary/text-to-binary.vue index 37aa9bea..22e4511e 100644 --- a/src/tools/text-to-binary/text-to-binary.vue +++ b/src/tools/text-to-binary/text-to-binary.vue @@ -1,40 +1,105 @@ - - + + - + Copy binary to clipboard - - + + - + + Copy text to clipboard + + + + + + + + + Copy binary to clipboard + + + + + + + + + Copy text to clipboard + + + + + + + + + + Copy binary to clipboard + + + + + + + + Copy text to clipboard