Complete Guide to Unicode String to Hexadecimal Conversion in JavaScript

Nov 23, 2025 · Programming · 8 views · 7.8

Keywords: JavaScript | Unicode | Hexadecimal Conversion | UTF-16 | Character Encoding

Abstract: This article provides an in-depth exploration of converting between Unicode strings and hexadecimal representations in JavaScript. By analyzing why original code fails with Chinese characters, it explains JavaScript's character encoding mechanisms, particularly UTF-16 encoding and code unit concepts. The article offers comprehensive solutions including string-to-hex encoding and hex-to-string decoding methods, with practical code examples demonstrating proper handling of Unicode strings containing Chinese characters.

Problem Background and Challenges

In JavaScript development, conversion between strings and hexadecimal representations is a common requirement. However, when dealing with Unicode characters, particularly non-ASCII characters like Chinese, simple conversion methods often fail. The original code produced incorrect output "ªo"[W" when processing the string "漢字", revealing the limitations of basic conversion approaches when handling multi-byte Unicode characters.

JavaScript Character Encoding Fundamentals

The key to understanding string handling in JavaScript lies in recognizing that JavaScript uses UTF-16 encoding to represent strings. In UTF-16 encoding, each character consists of one or more 16-bit code units. For characters in the Basic Multilingual Plane (BMP), each character corresponds to one code unit; for characters in supplementary planes, surrogate pairs—two code units—are required to represent a single character.

The fundamental reason the original conversion code fails is its assumption that each character can be represented by a single byte, which clearly doesn't apply to Unicode characters requiring multiple bytes. When str.charCodeAt(i) is called for characters requiring surrogate pairs, only part of the surrogate pair is obtained, leading to conversion errors.

Complete Solution

To solve this problem, we need to ensure that each code unit is correctly represented as a 4-digit hexadecimal number. Here's the improved complete solution:

String to Hexadecimal Encoding

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;
}

The clever aspect of this method is using ("000"+hex).slice(-4) to ensure each code unit is represented as a 4-digit hexadecimal number. By padding with leading zeros and then taking the last 4 digits, we get a uniform 4-digit representation regardless of whether the original hexadecimal representation was 1, 2, 3, or 4 digits.

Hexadecimal to String Decoding

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;
}

The decoding process first uses the regular expression /.{1,4}/g to split the hexadecimal string into groups of 4 digits, then parses and converts each group to the corresponding character. This approach correctly handles Unicode characters containing surrogate pairs.

Practical Application Examples

Let's verify the effectiveness of this solution with concrete examples:

var str = "\u6f22\u5b57"; // equivalent to "漢字"
var encoded = str.hexEncode();
var decoded = encoded.hexDecode();
console.log("Original string:", str);
console.log("Encoded result:", encoded);
console.log("Decoded result:", decoded);
console.log("Verification result:", str === decoded);

In this example, the Unicode code points for the string "漢字" are U+6F22 and U+5B57 respectively. After encoding, each character is correctly represented as 4-digit hexadecimal numbers "6f22" and "5b57", and the original string is perfectly restored after decoding.

Comparison with Other Methods

The method proposed in Answer 2, while concise, has significant drawbacks:

function toHex(str) {
    var result = '';
    for (var i=0; i<str.length; i++) {
        result += str.charCodeAt(i).toString(16);
    }
    return result;
}

This method doesn't pad the hexadecimal representation, resulting in different-length hexadecimal representations for code units of different sizes. This creates difficulties during decoding. For example, code unit values less than 256 might produce 1-digit or 2-digit hexadecimal representations, while larger values might produce 3-digit or 4-digit representations. This inconsistency makes decoding complex and error-prone.

Performance Considerations and Optimization

When processing large amounts of strings, performance becomes an important consideration. The current implementation uses string concatenation, which may not be optimal in some JavaScript engines. For performance-sensitive applications, consider using arrays to collect results:

String.prototype.hexEncodeOptimized = function(){
    var result = [];
    for (var i=0; i<this.length; i++) {
        var hex = this.charCodeAt(i).toString(16);
        result.push(("000"+hex).slice(-4));
    }
    return result.join('');
}

This approach avoids frequent string concatenation through array operations, potentially offering better performance in certain scenarios.

Extended Application Scenarios

This Unicode string to hexadecimal conversion method has important applications in multiple domains:

Browser Compatibility

The solution presented in this article is based on standard JavaScript features and has excellent browser compatibility. All modern browsers, including Chrome, Firefox, Safari, and Edge, fully support these methods. For older browsers, appropriate polyfills can be added to ensure compatibility.

Conclusion

Properly handling Unicode string to hexadecimal conversion in JavaScript requires a deep understanding of UTF-16 encoding mechanisms. By ensuring each code unit is uniformly represented as a 4-digit hexadecimal number, we can build robust conversion methods. The solution provided in this article not only solves the original problem but also offers performance optimization suggestions and practical application guidance, providing developers with a complete reference for handling Unicode string conversions.

In practical development, it's recommended to always use this padding approach for hexadecimal conversion to ensure code robustness and maintainability. Simultaneously, understanding character encoding fundamentals is crucial for handling internationalization applications and special character scenarios.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.