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 5952731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:43:23+00:00 2026-05-22T17:43:23+00:00

how can I convert a wchar_t ( ‘9’ ) to a digit in the

  • 0

how can I convert a wchar_t ('9') to a digit in the form of an int (9)?

I have the following code where I check whether or not peek is a digit:

if (iswdigit(peek)) {
    // store peek as numeric
}

Can I just subtract '0' or is there some Unicode specifics I should worry about?

  • 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-05-22T17:43:24+00:00Added an answer on May 22, 2026 at 5:43 pm

    If the question concerns just '9' (or one of the Roman
    digits), just subtracting '0' is the correct solution. If
    you’re concerned with anything for which iswdigit returns
    non-zero, however, the issue may be far more complex. The
    standard says that iswdigit returns a non-zero value if its
    argument is “a decimal digit wide-character code [in the current
    local]”. Which is vague, and leaves it up to the locale to
    define exactly what is meant. In the “C” locale or the “Posix”
    locale, the “Posix” standard, at least, guarantees that only the
    Roman digits zero through nine are considered decimal digits (if
    I understand it correctly), so if you’re in the “C” or “Posix”
    locale, just subtracting ‘0’ should work.

    Presumably, in a Unicode locale, this would be any character
    which has the general category Nd. There are a number of
    these. The safest solution would be simply to create something
    like (variables here with static lifetime):

    wchar_t const* const digitTables[] =
    {
        L"0123456789",
        L"\u0660\u0661\u0662\u0663\u0664\u0665\u0666\u0667\u0668\u0669",
        // ...
    };
    
    //!     \return
    //!         wch as a numeric digit, or -1 if it is not a digit
    int asNumeric( wchar_t wch )
    {
        int result = -1;
        for ( wchar_t const* const* p = std::begin( digitTables );
                p != std::end( digitTables ) && result == -1;
                ++ p ) {
            wchar_t const* q = std::find( *p, *p + 10, wch );
            if ( q != *p + 10 ) {
                result = q - *p;
        }
        return result;
    }
    

    If you go this way:

    1. you’ll definitely want to download the
      UnicodeData.txt file from the Unicode consortium
      (“Uncode Character
      Database
      “—this page has a links to both the Unicode data
      file and an explination of the encodings used in it), and
    2. possibly write a simple parser of this file to extract the
      information automatically (e.g. when there is a new version of
      Unicode)—the file is designed for simple programmatic
      parsing.

    Finally, note that solutions based on ostringstream and
    istringstream (this includes boost::lexical_cast) will not
    work, since the conversions used in streams are defined to only
    use the Roman digits. (On the other hand, it might be
    reasonable to restrict your code to just the Roman digits. In
    which case, the test becomes if ( wch >= L'0' && wch <= L'9' ),
    and the conversion is done by simply subtracting L'0'—
    always supposing the the native encoding of wide character
    constants in your compiler is Unicode (the case, I’m pretty
    sure, of both VC++ and g++). Or just ensure that the locale is
    “C” (or “Posix”, on a Unix machine).

    EDIT: I forgot to mention: if you’re doing any serious Unicode programming, you
    should look into ICU. Handling Unicode
    correctly is extremely non-trivial, and they’ve a lot of functionality already
    implemented.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How I can convert this code to LINQ? var TcData = new List<double>(); for(int
I can convert RGB values to HSV with the following code... $r = $r/255;
You can convert a negative number to positive like this: int myInt = System.Math.Abs(-5);
How i can convert to c# code files to html files?Is there any generation
How can I convert wchar_t* type to a managed UTF-8 string in C++/CLI?
How can i convert a narrow string to a wide string ? I have
Can someone explain why the following code won't compile (formatted oddly to make it
How can I convert a string from wchar_t to LPSTR .
How can I convert from ANSI character (char) to Unicode character (wchar_t) and vice
I need to convert a QChar to a wchar_t I've tried the following: #include

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.