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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:57:09+00:00 2026-05-24T18:57:09+00:00

Assume one has an vector or array of N elements (N can be very

  • 0

Assume one has an vector or array of N elements (N can be very large) containing the octal representation of a non negative integer. How do I get the decimal representation of the number from this array? The code has to be really fast.

EDIT: array A of N elements contains octal representation of a non-negative integer K, i.e. each element of A belongs to the interval [0; 7] (both ends included)

Example: A[0] = 2; A[1] = 6; A[2] = 3

Now a naive calculation would be 2*8pow0 + 6*8pow1 + 3*8pow2 = 2+ 48+ 192 = 242

I tried this but it does not seem to work for large inputs > 6K

//vector<int> A is the input
using namespace std;
vector<int>::iterator it = A.begin();

unsigned int k = 0;
unsigned int x = 0;
while(it < A.end()){
   x = x | (*it<<3*k);
   k++;
   it++;
}

I am also having problems converting a hexadecimal string to its decimal representation? Is this the correct way to do this in C++:
//Assume S to be your input string containing a hex representation
//like F23

std::stringstream ss;
ss << std::hex << S;
ss >> x;
  • 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-24T18:57:10+00:00Added an answer on May 24, 2026 at 6:57 pm

    This is what I came up with:

    template<typename Iter>
    int read_octal(Iter begin, Iter end)
    {
        int x = 0;
        int f = 1;
        for (; begin != end; ++begin)
        {
            x += *begin * f;
            f *= 8;
        }
        return x;
    }
    
    int main()
    {
        int test[] = {2, 6, 3};
        int result = read_octal(test + 0, test + 3);
        std::cout << result << '\n';
    }
    

    I tried this but it does not seem to work for large inputs > 6K

    What exactly do you mean by 6k? An int usually has 32 bits, and an octal digit has 3 bits. Thus, you cannot have more than 10 elements in your range, otherwise x will overflow.

    I am also having problems converting a hexadecimal string to its decimal representation?

    Well, you could always write a function to parse a string in hex format yourself:

    int parse_hex(const char* p)
    {
        int x = 0;
        for (; *p; ++p)
        {
            x = x * 16 + digit_value(*p);
        }
        return x;
    }
    

    With the most portable version of digit_value being:

    int digit_value(char c)
    {
        switch (c)
        {
            case '0': return 0;
            case '1': return 1;
            case '2': return 2;
            case '3': return 3;
            case '4': return 4;
            case '5': return 5;
            case '6': return 6;
            case '7': return 7;
            case '8': return 8;
            case '9': return 9;
            case 'A': 
            case 'a': return 10;
            case 'B': 
            case 'b': return 11;
            case 'C': 
            case 'c': return 12;
            case 'D': 
            case 'd': return 13;
            case 'E': 
            case 'e': return 14;
            case 'F': 
            case 'f': return 15;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume we have three sets of strings in Scala. One has elements A,B,C. Two
How can one detect being in a chroot jail without root privileges? Assume a
Let's assume one joins a project near the end of its development cycle. The
(For the purposes of this question, let us assume that one is intentionally not
Assume you have five products, and all of them use one or more of
Lets assume I have two hashes. One of them contains a set of data
This is one of those little detail (and possibly religious) questions. Let's assume we're
Let's assume I'm developing a AJAX, PHP chess game. During the game, one movement
I noticed the WinForms RichTextBox has a ZoomFactor property that I assume is exactly
If I have two table of data. One has a clustered index CINDEX, the

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.