From d898ecec21bdbf543260235e2256f559ef64c16f Mon Sep 17 00:00:00 2001 From: Larry Yuanlin Li Date: Sat, 4 Nov 2023 18:57:31 +0800 Subject: [PATCH] add Chinese characters convert to unicode and utf-8 --- src/tools/text-to-binary/index.ts | 2 +- .../text-to-binary/text-to-binary.models.ts | 44 ++++++++- src/tools/text-to-binary/text-to-binary.vue | 93 ++++++++++++++++--- 3 files changed, 123 insertions(+), 16 deletions(-) diff --git a/src/tools/text-to-binary/index.ts b/src/tools/text-to-binary/index.ts index 40ac93d6..6fd7fd27 100644 --- a/src/tools/text-to-binary/index.ts +++ b/src/tools/text-to-binary/index.ts @@ -4,7 +4,7 @@ import { defineTool } from '../tool'; export const tool = defineTool({ name: 'Text to ASCII binary', path: '/text-to-binary', - description: 'Convert text to its ASCII binary representation and vice versa.', + description: 'Convert text to its ASCII binary,Unicode or Utf8 representation and vice versa.', keywords: ['text', 'to', 'binary', 'converter', 'encode', 'decode', 'ascii'], component: () => import('./text-to-binary.vue'), icon: Binary, 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, '&#x$2;'); + }); + // return EncodeUtf8(text); +}; + +function convertUtf8BinaryToText(binary: string) { + return unescape(binary.replace(/&#x/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 @@