Remember that a JavaScript code unit is 16 bits wide. Therefore the hex string form will be 4 digits per code unit.
usage:
var str = "\u6f22\u5b57"; // "\u6f22\u5b57" === "漢字" alert(str.hexEncode().hexDecode());
String to hex form:
String.prototype.hexEncode = function(){ var hex, i; var result = ""; for (i=0; i<this.length; i++) { hex = this.charCodeAt(i).toString(16); result += ("000"+hex).slice(-4); } return result }
Back again:
String.prototype.hexDecode = function(){ var j; var hexes = this.match(/.{1,4}/g) || []; var back = ""; for(j = 0; j<hexes.length; j++) { back += String.fromCharCode(parseInt(hexes[j], 16)); } return back; }
Related Posts:
- Tallest Unicode character?
- RGB to hex and hex to RGB
- How to add a class to a given element?
- How to make a word underline in Markdown
- Creating multiline strings in JavaScript
- What is lexical scope?
- Remove element by id
- await is only valid in async function
- Remove duplicate values from JS array [duplicate]
- How to read an external local JSON file in JavaScript?
- u’\ufeff’ in Python string
- SyntaxError: Unexpected token o in JSON at position 1
- Remove duplicate values from JS array
- How can I add a key/value pair to a JavaScript object?
- Adding an onclick event to a div element
- Typescript : Property does not exist on type ‘object’
- How to check if array is empty or does not exist?
- How to Create simple drag and Drop in angularjs
- stop all instances of node.js server
- Working with $scope.$emit and $scope.$on
- Using Node.js require vs. ES6 import/export
- How can jQuery deferred be used?
- How to print elements from array with javascript
- What is the JavaScript version of sleep()?
- How do I scroll to an element using JavaScript?
- Finding largest integer in an array in JavaScript
- javascript set cookie with expire time
- Angular: conditional class with *ngClass
- How can I add a key/value pair to a JavaScript object?
- How do I change an HTML selected option using JavaScript?
- Failed to load resource: net::ERR_CONTENT_LENGTH_MISMATCH
- TypeError: Invalid attempt to spread non-iterable instance
- Declaring multiple variables in JavaScript
- How to print a number with commas as thousands separators in JavaScript
- Convert Array to Object
- How to set the id attribute of a HTML element dynamically with angularjs (1.x)?
- Using $setValidity inside a Controller
- Partial render in HTML/JavaScript
- How can I check if a string is a valid number?
- Javascript setInterval not working
- Javascript Regular Expression Remove Spaces
- image.onload event and browser cache
- Simulate Keypress With jQuery
- How to add a keyboard listener to my onClick handler?
- SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
- HTML Entity Decode
- Why am i receiving this jsfiddle error, document.write can be a form of eval
- jquery beforeunload when closing (not leaving) the page?
- Array Mapping in AngularJs
- Get JavaScript object from array of objects by value of property [duplicate]
- Simple pagination in javascript
- Javascript swap array elements
- jQuery sort elements using data id
- Resource interpreted as Document but transferred with MIME type application/zip
- How to redirect to another page using AngularJS?
- How do I know which version of Javascript I’m using?
- getContext is not a function
- lodash multi-column sortBy
- navigator.geolocation.getCurrentPosition sometimes works sometimes doesn’t
- Expected ‘this’ to be used by class method
- Strip HTML from Text JavaScript
- Calling a JavaScript function in another js file
- JavaScript code not running in HTML5 document
- What is the cause for “angular is not defined”
- Unexpected token < in first line of HTML
- ngular is automatically adding ‘ng-invalid’ class on ‘required’ fields
- How to set “style=display:none;” using jQuery’s attr method?
- Await is a reserved word error inside async function
- Javascript WP Object Documentation?
- Show only images and videos in a wp.media window
- WordPress Rest API
- Instantiate TinyMCE via JS ( rather than PHP/wp_editor )
- Force core to use .dev JavaScript files
- TinyMCE Button to Insert Multiple Lines of Text?
- How to add extra attributes to the script tag added via wp_localize_script()
- Prevent escaping javascript in visual editor
- @wordpress/components Button variants not styled
- How to place script in footer?
- Only displaying zip files with wp media
- Adding a Javascript slideshow to the home page
- List of JS Events for Widgets?
- Authentication with the Rest API when using an External Application
- Open Featured Image Modal in WordPress Gutenberg onClick of a button
- Screen Resolution Detect
- Override do_item() Function to Add Extra Attribute to Scripts
- Is it possible to pass js variable to template file
- Automatically Add Defer or ASYNC to all JS files (no matter where they are located)
- Uncaught ReferenceError: jQuery is not defined – although jQuery is loaded [closed]
- How can I import one custom block into another?
- Gutenberg Blocks: how to determine an index of the current inner block?
- find out reason of “Updating failed” in Post-editor
- Getting a variable inside foreach from PHP to JS after localization
- Custom JS on a specific page
- How to import a JavaScript Library into WordPress
- How to change the link address from internal to external
- Uncaught TypeError: Cannot read property ‘add’ of undefined bp-plupload.min.js
- How to verify that a field has a value before adding a term to a taxonomy
- how to include js in widget?
- List unused javascript for easy removal
- Include Facebook Javascript SDK In WordPress