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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:52:24+00:00 2026-06-07T16:52:24+00:00

Suppose I have a char buf[12]; which I know will always have a right

  • 0

Suppose I have a char buf[12]; which I know will always have a right justified unsigned number padded on the left with spaces. So, for example: _________329 (where the _ stands for space). The fastest way I can think of parsing it is with something like this:

while (*buf == ' ') buf++;
atoi(buf);

but I was wondering if there was a faster way, particularly with atoi given that we know it is unsigned which atoi does not assume..

  • 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-07T16:52:27+00:00Added an answer on June 7, 2026 at 4:52 pm

    I assume the first char is reserved for a “potential sign” and is always “space”? Because otherwise, you would only need a char[11] instead of a char[12]. Anyway, fixed size allows manual loop unrolling:

    unsigned parse(const char(&b)[12])
    {
        return ((((((((((b[1] & 15))
                 * 10 + (b[2] & 15))
                 * 10 + (b[3] & 15))
                 * 10 + (b[4] & 15))
                 * 10 + (b[5] & 15))
                 * 10 + (b[6] & 15))
                 * 10 + (b[7] & 15))
                 * 10 + (b[8] & 15))
                 * 10 + (b[9] & 15))
                 * 10 + (b[10]& 15);
    }
    

    Note that the & 15 trick treats spaces and zeros the same and will work with both ASCII (space = 32, zero = 48) and EBCDIC (space = 48, zero = 240). I haven’t checked out other character encodings yet 🙂

    Will this actually be faster or slower than atoi? The only way to find out is to measure. But I would probably stay with atoi in any case, because using a standard function always improves readability.

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

Sidebar

Related Questions

Suppose we have a loop that runs for 100 times. Does using unsigned char
Suppose I have an array unsigned char arr[]= {0,1,2,3,4,5,6,7,8,9}; Is there a way to
Suppose I have following template template<unsigned char I, unsigned char F> class FOO {
Suppose I have following template class: template<unsigned char I, unsigned char F> class FOO
Suppose that I have a unsigned char*, let's call it: some_data unsigned char* some_data;
Suppose I have: typedef unsigned long long uint64; unsigned char data[BUF_SIZE]; uint64 MyPacket::GetCRC() {
suppose I have x.dll in C++ which looks like this MYDLLEXPORT const char* f1()
Suppose i have a pointer of type char* to unicode string, and i know
Suppose you have the following C code. unsigned char a = 1; printf(%d\n, ~a);
Suppose I have a string char* str . I print it to the buffer

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.