Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8578009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:17:24+00:00 2026-06-11T20:17:24+00:00

Bytes to string and backward Functions written there work properly that is pack(unpack(string)) yields

  • 0

Bytes to string and backward

Functions written there work properly that is pack(unpack("string")) yields to "string". But I would like to have the same result as "string".getBytes("UTF8") gives in Java.

The question is how to make a function giving the same functionality as Java getBytes(“UTF8”) in JavaScript?

For Latin strings unpack(str) from the article mentioned above provides the same result as getBytes("UTF8") except it adds 0 for odd positions. But with non-Latin strings it works completely different as it seems to me. Is there a way to work with string data in JavaScript like Java does?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:17:25+00:00Added an answer on June 11, 2026 at 8:17 pm

    You can use this function (gist):

    function toUTF8Array(str) {
        var utf8 = [];
        for (var i=0; i < str.length; i++) {
            var charcode = str.charCodeAt(i);
            if (charcode < 0x80) utf8.push(charcode);
            else if (charcode < 0x800) {
                utf8.push(0xc0 | (charcode >> 6), 
                          0x80 | (charcode & 0x3f));
            }
            else if (charcode < 0xd800 || charcode >= 0xe000) {
                utf8.push(0xe0 | (charcode >> 12), 
                          0x80 | ((charcode>>6) & 0x3f), 
                          0x80 | (charcode & 0x3f));
            }
            else {
                // let's keep things simple and only handle chars up to U+FFFF...
                utf8.push(0xef, 0xbf, 0xbd); // U+FFFE "replacement character"
            }
        }
        return utf8;
    }
    

    Example of use:

    >>> toUTF8Array("中€")
    [228, 184, 173, 226, 130, 172]
    

    If you want negative numbers for values over 127, like Java’s byte-to-int conversion does, you have to tweak the constants and use

                utf8.push(0xffffffc0 | (charcode >> 6), 
                          0xffffff80 | (charcode & 0x3f));
    

    and

                utf8.push(0xffffffe0 | (charcode >> 12), 
                          0xffffff80 | ((charcode>>6) & 0x3f), 
                          0xffffff80 | (charcode & 0x3f));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to regex match a sequence of bytes when the string '02
Is this code valid C: fputc(*(Checking 7 bytes before this string-7), stdout); Is there
There is a String variable containing ascii characters and double bytes characters(for example, the
How do I convert 6 bytes representing a MAC address into a string that
There are multibyte string functions in PHP to handle multibyte string (e.g:CJK script). For
Having an utf-8 encoded string like this: bar = hello 。◕‿‿◕。 and a bytes
byte[] bytes = new byte[] { 1, -1 }; System.out.println(Arrays.toString(new String(bytes, UTF-8).getBytes(UTF-8))); System.out.println(Arrays.toString(new String(bytes,
String class has a constructor: new String(byte[] bytes, Charset charset) and a method: byte[]
Four consecutive bytes in a byte string together specify some value. However, only 7
How to get the string length in bytes in nodejs? If I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.