Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Digits to numeric value?

Digits to numeric value?

Scheduled Pinned Locked Moved Clever Code
testingbeta-testingquestion
8 Posts 3 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    John R Shaw
    wrote on last edited by
    #1

    This is a sample only, so ignore the obvious problems (if you see any). Note: Assume the next character from the input is a Unicode digit.

    unsigned Bogus(size n_digits)
    {
    string_type digits;
    // Gather digits.
    while( 0 < n_digits && std::isdigit(input_.peek(), std::locale() )
    {
    digits += input_.pop();
    --n_digits;
    }

    // Convert digits string to numeric value.
    unsigned n = 0;
    if( !digits.empty()g )
    {
        std::basic\_istringstream iss(digits);
        iss >> n;
    }
    // return result
    return( n );
    

    }

    Definitions: 1. input_.peek() : Look at next character in input. 2. input_.pop(): Retrieve next character from input. 3. char_type: Is a 16-bit Unicode character. The purpose of the function is to gather a given number of character digits from the input source and convert them to their equivalent numeric value. Regardless of the character set specified by the current locale (key point). Keep in mind that this is related to internationalization and that the character type is Unicode. Also there is one rule that must be followed: No system dependent code may be used. Hint: What constitutes a digit character in Unicode and why would it be possible for this to fail? P.S. I will check back in a couple of days to see who knew the answer, and give it if nobody figured it out.

    INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

    P D 2 Replies Last reply
    0
    • J John R Shaw

      This is a sample only, so ignore the obvious problems (if you see any). Note: Assume the next character from the input is a Unicode digit.

      unsigned Bogus(size n_digits)
      {
      string_type digits;
      // Gather digits.
      while( 0 < n_digits && std::isdigit(input_.peek(), std::locale() )
      {
      digits += input_.pop();
      --n_digits;
      }

      // Convert digits string to numeric value.
      unsigned n = 0;
      if( !digits.empty()g )
      {
          std::basic\_istringstream iss(digits);
          iss >> n;
      }
      // return result
      return( n );
      

      }

      Definitions: 1. input_.peek() : Look at next character in input. 2. input_.pop(): Retrieve next character from input. 3. char_type: Is a 16-bit Unicode character. The purpose of the function is to gather a given number of character digits from the input source and convert them to their equivalent numeric value. Regardless of the character set specified by the current locale (key point). Keep in mind that this is related to internationalization and that the character type is Unicode. Also there is one rule that must be followed: No system dependent code may be used. Hint: What constitutes a digit character in Unicode and why would it be possible for this to fail? P.S. I will check back in a couple of days to see who knew the answer, and give it if nobody figured it out.

      INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      You should post the answer, this isn't a quiz forum.

      J 1 Reply Last reply
      0
      • P PIEBALDconsult

        You should post the answer, this isn't a quiz forum.

        J Offline
        J Offline
        John R Shaw
        wrote on last edited by
        #3

        Then someone will be saying that I should have waited to post the answer, so they have a chance to see it. Normally if it is related to the standard I see it before reading the answer, but this one is completely hidden. This is a subtle bug related to the STL and I expect no one to know what it is. Of course whether it is a bug or not is still up for grabs. Hint: It is not a coding error. P.S. If you do not want to learn to fish, then go starve.

        INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

        P 1 Reply Last reply
        0
        • J John R Shaw

          This is a sample only, so ignore the obvious problems (if you see any). Note: Assume the next character from the input is a Unicode digit.

          unsigned Bogus(size n_digits)
          {
          string_type digits;
          // Gather digits.
          while( 0 < n_digits && std::isdigit(input_.peek(), std::locale() )
          {
          digits += input_.pop();
          --n_digits;
          }

          // Convert digits string to numeric value.
          unsigned n = 0;
          if( !digits.empty()g )
          {
              std::basic\_istringstream iss(digits);
              iss >> n;
          }
          // return result
          return( n );
          

          }

          Definitions: 1. input_.peek() : Look at next character in input. 2. input_.pop(): Retrieve next character from input. 3. char_type: Is a 16-bit Unicode character. The purpose of the function is to gather a given number of character digits from the input source and convert them to their equivalent numeric value. Regardless of the character set specified by the current locale (key point). Keep in mind that this is related to internationalization and that the character type is Unicode. Also there is one rule that must be followed: No system dependent code may be used. Hint: What constitutes a digit character in Unicode and why would it be possible for this to fail? P.S. I will check back in a couple of days to see who knew the answer, and give it if nobody figured it out.

          INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

          D Offline
          D Offline
          Doc Lobster
          wrote on last edited by
          #4

          Given the chance that someone is able to and enters a fullwidth digit U+FF10 to U+FF19 (don't ask me why one would do that), std::isdigit(...) returns true - at least the VS2003 implementation does. However the conversion does not work for fullwidth digits. Well, I didn't even know that the STL is Unicode-aware in some ways ...

          J 1 Reply Last reply
          0
          • J John R Shaw

            Then someone will be saying that I should have waited to post the answer, so they have a chance to see it. Normally if it is related to the standard I see it before reading the answer, but this one is completely hidden. This is a subtle bug related to the STL and I expect no one to know what it is. Of course whether it is a bug or not is still up for grabs. Hint: It is not a coding error. P.S. If you do not want to learn to fish, then go starve.

            INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            You can post it as a reply to the original post so readers have a choice.

            J 1 Reply Last reply
            0
            • D Doc Lobster

              Given the chance that someone is able to and enters a fullwidth digit U+FF10 to U+FF19 (don't ask me why one would do that), std::isdigit(...) returns true - at least the VS2003 implementation does. However the conversion does not work for fullwidth digits. Well, I didn't even know that the STL is Unicode-aware in some ways ...

              J Offline
              J Offline
              John R Shaw
              wrote on last edited by
              #6

              You just won the gold medal! :cool::cool::cool::cool::cool: That does not only apply to fullwidth (Chinese) digits, it applies to all digits from various languages and eras. If you want to know the starting digit (digit) 0 for various countries that MS knows about, just step into “_wtoi” to see how they solve the problem. I just opted to only accept ASCII digits, because that satisfied my requirements. Check out other countries, how a squiggly line is equivalent to a digit is beyond me but it is.

              INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

              D 1 Reply Last reply
              0
              • P PIEBALDconsult

                You can post it as a reply to the original post so readers have a choice.

                J Offline
                J Offline
                John R Shaw
                wrote on last edited by
                #7

                Good point! But "Doc Lobster" has already posted what is wrong, and he did not need to know the cheat codes. Too bad, I was hopping to surprise everyone. :laugh:

                INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

                1 Reply Last reply
                0
                • J John R Shaw

                  You just won the gold medal! :cool::cool::cool::cool::cool: That does not only apply to fullwidth (Chinese) digits, it applies to all digits from various languages and eras. If you want to know the starting digit (digit) 0 for various countries that MS knows about, just step into “_wtoi” to see how they solve the problem. I just opted to only accept ASCII digits, because that satisfied my requirements. Check out other countries, how a squiggly line is equivalent to a digit is beyond me but it is.

                  INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

                  D Offline
                  D Offline
                  Doc Lobster
                  wrote on last edited by
                  #8

                  Thanks! :) I've also tried other unicode numerics, as circled numbers, super- and subscript numbers, but isdigit(...) returned false for them. It seems that the Nd (Numeric-decimal) type from the UCD is checked.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups