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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:14:31+00:00 2026-05-16T11:14:31+00:00

I have always used streams, printf, string(x) or whatever the language in question offered

  • 0

I have always used streams, printf, string(x) or whatever the language in question offered to convert numeric types to a string or back. However I have never really considered how this is actually done. I searched around on Google, but all the results are just to use those varies methods, and not how the conversion is really done behind the scenes 🙁

For integers using binary, octal and hexadecimal seems fairly straight forward since each “digit” in the string represents a set group of bits (eg for the 2 hex digits I know its xxxxyyyy), so I could do it with bit shifts and taking one digit at a time, eg for the hex string 0xFA20 the value is “(15 << 12) | (10 << 8) | (2 << 4) | (0 << 0)”.

Decimal integers are more difficult since base 10 doesn’t map to base 2 like that and so one bit may effect more than one decimal digit making conversion both ways more complex…

As for floating point numbers I really have no idea. I guess the whole and fractional parts could be considered separately or something? What about as an exponential, a set number of significant figures or set number of decimal places?

  • 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-16T11:14:31+00:00Added an answer on May 16, 2026 at 11:14 am

    Decimal conversions are a bit slower, but not really a lot more complex. Let’s look at the hexadecimal conversion a bit more like we’d probably write it in real code. Just for example, in C++ you might do the conversion something like this:

    char digits[] = "0123456789abcdef";
    std::string result;
    
    int input = 0xFA20;
    
    while (input) {
        int digit = input & 0xf; // or: digit = input % 0xf;
        input >>= 4;             // or: input /= 16;
        result.push_front(digits[digit]);
    }
    

    Right now, however, that has some magic numbers. Let’s get rid of them:

    const int base = 16;
    
    while (input) { 
        int digit = input % (base - 1);
        input /= base;
        result.push_front(digits[digit]);
    }
    

    In the process of getting rid of those magic numbers, we’ve also made the routine nearly universal — if we change the value of ‘base’, the rest of the routine still works, and converts the input to the specified base. Essentially the only other change we need to make is adding more to the “digits” array if we want to support bases larger than 16.

    This also ignores a few things for simplicity. Most obviously, if the number is negative, you typically set a flag, convert to a positive number, and at the end if the flag was set, put a ‘-‘ into the string). With 2’s complement there’s a corner case for the maximally negative number, which can’t be converted to a positive number (without converting to a type with more range). Typically you deal with that by promoting most types. For your largest integer type (which you can’t promote) it’s usually easiest to just hard-code that one value.

    In principle floating point isn’t a whole lot different — you still basically do mathematical manipulations to generate one digit at a time. In fact, it gets more complex simply because you typically have to deal with a couple of different formats (at least a “basic” floating point and some sort of “Scientific” format), as well as variables for field width and precision. By the time you’ve dealt with that, you end up with a few hundred lines of code or so — not a particularly outrageous amount, but probably a bit more than makes sense to include here.

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

Sidebar

Related Questions

I always have used Spring Framework classes as-is. However I am thinking of customizing
We have used Infragistics controls in our applications for years. However, we have always
I have always used the mouseover event, but while reading the jQuery documentation I
As a hardcore WinForms programmer from a Win32 background I have always used Spy++
I have generally always used some sort of Hungarian Notation for my field names
I have a C program which has always used hard coded define statements for
I have java sdk 1.7. I've always used django/python for web development, this will
I have always used XML for returning data from my servlets and i have
I have always used a Singleton class for a registry object in PHP. As
I have always used open-uri, and open().read to get content through http. I am

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.