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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:27:51+00:00 2026-06-18T12:27:51+00:00

How do I convert extremely large (>1MB) decimal numbers to bytes/hex/binary? For example, the

  • 0

How do I convert extremely large (>1MB) decimal numbers to bytes/hex/binary?

For example, the number “300” should be converted to {0x01, 0x2C}. The byte order doesn’t matter and {0x2C, 0x01} is also OK.

The source numbers are stored in a prepped file (Without puntuation, whitespace or linebreaks). The largest is just over 17MB, although I can’t rule out that I’ll have a 100MB number in the future. The destination is also a file.

Is there a way that doesn’t take ages, or is fail-safe in case it does take ages?

I fear using BigInteger will take ages and is not fail-save (ie. i can’t resume half-way if something goes wrong)

I’m not against implementing my own algorithm, although I’m looking for something more efficient than ‘check if odd, divide by 2’. I’ve seen a very efficient implementation of binary to BCD, the Shift and Add-3 Algorithm, and am looking for a similarly efficient implementation in reverse.

An extra kudos for an implementation that also supports fixed-point numbers (with 1 digit and the rest decimals, eg. Pi).

  • 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-18T12:27:52+00:00Added an answer on June 18, 2026 at 12:27 pm

    For me BigInteger converts 1M digits into byte [] in ~39 seconds. Is it too much for you?

    Random r = new Random ();
    
    StringBuilder sb = new StringBuilder();
    
    for (int i = 0; i < 1000000; i++)
        sb.append ("0123456789".charAt(r.nextInt(10)));
    
    long t = System.currentTimeMillis();
    BigInteger bi = new BigInteger (sb.toString());
    byte [] bytes = bi.toByteArray();
    System.out.println(System.currentTimeMillis() - t);
    

    About decimals. Lets assume you have a big decimal in form <n digits>.<m digits>. You want to convert it to binary with k bits after dot. You need to solve an equation: D/(10^m) = X/(2^k), where X is integer. Here D is your decimal without dot (mantissa of your decimal), X is your binary without dot (mantissa of your binary). The equation is easy to solve: X ~ round(D*(2^k)/(10^m)). X has to be integer, so we added round().

    For example you need to convert 12.34 into binary with 3 bits after dot.

    n = 2
    m = 2
    D = 1234
    k = 3
    X ~ round(D*(2^k)/(10^m)) = round(1234 * 8 / 100) = round(98.72) = 99 = 1100011b
    

    Remember that we desired 3 bits after dot, so our answer is 12.34 ~ 1100.011b

    All these calculations can be done using BigInteger.

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

Sidebar

Related Questions

I came across something very basic but extremely bewildering today. I needed to convert
convert this: $300 to this : 300 can't do it with intval() or (int)
I convert all links from example.com/action to example.com/index.html#action which is then parsed by my
I have an extremely simple IMultiValueConverter that simply OR's two values. In the example
I am trying to convert a decimal integer into hexadecimal. I've done a lot
I want to count the bits that are set in an extremely large bit-vector
I have to convert some C/C++ code to Java. My C++ is extremely rusty.
I am trying to recursively convert int to binary string but I don't really
Is there a way to get python to print extremely large longs in scientific
I need to read in a couple of extremely large strings which are comprised

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.