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

  • SEARCH
  • Home
  • 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 7988297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:24:03+00:00 2026-06-04T12:24:03+00:00

I have a working algorithm to convert a UTF-8 string to a UTF-32 string,

  • 0

I have a working algorithm to convert a UTF-8 string to a UTF-32 string, however, I have to allocate all the space for my UTF-32 string ahead of time. Is there any way to know how many characters in UTF-32 that a UTF-8 string will take up.

For example, the UTF-8 string “¥0” is 3 chars, and once converted to UTF-32 is 2 unsigned ints. Is there any way to know the number of UTF-32 ‘chars’ I will need before doing the conversion? Or am I going to have to re-write the algorithm?

  • 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-04T12:24:06+00:00Added an answer on June 4, 2026 at 12:24 pm

    There are two basic options:

    1. You could make two passes through the UTF-8 string, the first one counting the number of UTF-32 characters you’ll need to generate, and the second one actually writing them to a buffer.

    2. Allocate the max number of 32-bit chars you could possibly need — i.e., the length of the UTF-8 string. This is wasteful of memory, but means you can transform utf8->utf32 in one pass.

    You could also use a hybrid — e.g., if the string is shorter than some threshold then use the second approach, otherwise use the first.

    For the first approach, the first pass would look something like this:

    size_t len=0;  // warning: untested code.
    for(const char *p=src; *p; ++p) {
        // characters that begin with binary 10xxxxxx... are continuations; all other
        // characters should begin a new utf32 char (assuming valid utf8 input)
        if ((*p & 0xc0) != 0x80) ++len;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working with some friends to convert a Matlab Genetic Algorithm to
So I have been working on properly implementing a quicksort algorithm for a homework
I'm working with my simple PHP captcha algorithm ( http://www.source.ofitall.com/devel/captcha.php ) and I have
I'm working on an algorithm to generate point to point linear gradients. I have
I have code I'm working on to instantiate a CRC algorithm dependent on a
I have been trying to get a working algorithm that detects intersection between a
I have implemented a working quickSort algorithm using the first element in the array
I have a red black tree algorithm which is working fine. When a node
I have been working on an algorithm to rearranging the letters of a word,
So I have a working sorting algorithm in Python. (Its exact contents are irrelevant

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.