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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:47:27+00:00 2026-05-30T23:47:27+00:00

An integer measures 4 bytes. In my example I have numbers measuring 1 MB.

  • 0

An integer measures 4 bytes. In my example I have numbers measuring 1 MB. How can I convert them to a human readable decimal number fast?

The number is present in uint[] Array containing Size items.

  • 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-30T23:47:29+00:00Added an answer on May 30, 2026 at 11:47 pm

    I don’t know if this is any faster, but here is an example in delphi I wrote a long time ago to handle big ints as strings (VERY quick and dirty) – this was for 128bit uint but you could extend it indefinitely

    Function HexToBinShort(hex:integer):string;
    begin
      case hex of
        0:  result:='0000';  //convert each hex digit to binary string
        1:  result:='0001';  //could do this with high-nybble and low nybble
        2:  result:='0010';  //of each sequential byte in the array (mask and bit shift)
        3:  result:='0011';  //ie: binstring:=binstring + HexToBinShort(nybble[i])
        4:  result:='0100';  //but must count DOWN for i (start with MSB!)
        5:  result:='0101';
        6:  result:='0110';
        7:  result:='0111';
        8:  result:='1000';
        9:  result:='1001';
        10: result:='1010';
        11: result:='1011';
        12: result:='1100';
        13: result:='1101';
        14: result:='1110';
        15: result:='1111';
      end;
    end;
    

    Then take the concatenated binary string and add powers of two each time you see a ‘1’

    Function BinToIntString(binstring:string):string;
    var i, j : integer;
    var calcHold, calc2 :string;
    begin
      calc2:=binstring[Length(binstring)];   // first bit is easy 0 or 1
      for i := (Length(binstring) - 1) downto 1 do begin       
        if binstring[i] = '1' then begin   
           calcHold:=generateCard(Length(binstring)-i);
           calc2 := AddDecimalStrings(calcHold, calc2);
        end;
      end;
      result:=calc2;
    end;
    

    generateCard is used to create a decimal string representation of 2^i (for i>0)

    Function generateCard(i:integer):string;
    var j : integer;
    var outVal : string;
    begin
      outVal := '2';
      if i > 1 then begin
        for j := 2 to i do begin
          outVal:= MulByTwo(outVal);
        end;
      end;
      result := outVal;
    end;
    

    and MulByTwo multiplies a decimal string by two

    Function MulByTwo(val:string):string;
    var i : integer;
    var carry, hold : integer;
    var outHold : string;
    var outString :string;
    var outString2 : string;
    begin
      outString:= StringOfChar('0', Length(val) + 1);
      outString2:= StringOfChar('0', Length(val));
      carry :=0;
      for i := Length(val) downto 1 do begin
        hold := StrToInt(val[i]) * 2 + carry;
        if hold >= 10 then begin
          carry := 1;
          hold := hold - 10;
        end else begin
          carry := 0;
        end;
        outHold := IntToStr(hold);
        outString[i+1] := outHold[1];
      end;
      if carry = 1 then begin
        outString[1] := '1';
        result := outString;
      end else begin
        for i := 1 to length(outString2) do outString2[i]:=outString[i+1];
        result := outString2;
      end;
    end; 
    

    And finally – AddDecimalStrings…well, it adds two decimal strings :

    Function AddDecimalStrings(val1, val2:string):string;
    var i,j :integer;
    var carry, hold, largest: integer;
    var outString, outString2, bigVal, smVal, outHold:string;
    begin
      if Length(val1) > Length(val2) then begin
        largest:= Length(val1);
        bigVal := val1;
        smVal := StringOfChar('0', largest);
        j:=1;
        for i := (largest - length(val2) +1) to largest do begin
          smVal[i] := val2[j];
          j:=j+1;
        end;
      end else begin
        if length(val2) > Length(val1) then begin
          largest:=Length(val2);
          bigVal:=val2;
          smVal := StringOfChar('0', largest);
          j:=1;
          for i := (largest - length(val1) +1) to largest do begin
            smVal[i] := val1[j];
            j:=j+1;
          end;
        end else begin
          largest:=length(val1);
          bigVal:=val1;
          smVal:=val2;
        end;
      end;
      carry:=0;
      outString:=StringOfChar('0', largest +1);
      outString2:=StringOfChar('0', largest);
    
      for i := largest downto 1 do begin
        hold := StrToInt(bigVal[i]) + StrToInt(smVal[i]) + carry;
        if hold >=10 then begin
          carry:=1;
          hold := hold - 10;
        end else begin
          carry:=0;
        end;
        outHold:= IntToStr(hold);
        outString[i+1]:=outHold[1];
      end;
    
      if carry = 1 then begin
        outString[1] := '1';
        result := outString;
      end else begin
        for i := 1 to length(outString2) do outString2[i]:=outString[i+1];
        result := outString2;
      end;  
    end;
    

    These functions allow you to perform basic arithmetic on almost arbitrarily large integers as strings. You hit another wall when the number of digits is too big to index an array with, of course.

    Here’s a divide by two, btw (useful for going the other way…). I don’t handle odd numbers here.

    Function DivByTwo(val:string):string;
    var i : integer;
    var hold : integer;
    var outHold : string;
    var outString, outString2 :string;
    begin
      outString:=StringOfChar('0',Length(val));
    
      for i := Length(val) downto 1 do begin
        if StrToInt(val[i]) mod 2 = 0 then begin
          hold:= Math.Floor(StrToInt(val[i]) / 2);
          outHold:= IntToStr(hold);
          outString[i]:=outHold[1];
        end else begin
          hold:= Math.Floor((StrToInt(val[i]) - 1) / 2);
          outHold:=IntToStr(hold);
          outString[i]:= outHold[1];
          if i <> Length(val) then begin
            hold:= StrToInt(outString[i+1]) + 5;
            outHold:= IntToStr(hold);
            outString[i+1] := outHold[1];
          end;
        end;
      end;
      outString2:=StringOfChar('0',Length(val)-1);
      if (outString[1] = '0') and (length(outString) > 1) then begin
        for i := 1 to length(outString2) do outString2[i]:=outString[i+1];
        result:=outString2;
      end else begin
        result:=outString;
      end;
    end;
    

    EDIT: I just tried this with a 9million bit long binary string and it is ludicrously slow! Not surprising, really. This is completely unoptimized code which has a lot of low hanging fruit to pick at for speeding things up. Still, I can’t help but feel that this is the kind (or scale) of problem that you would probably want to write, at least partially, in fully optimized assembly. The individual operations are small but they must be done many times – that spells begging for assembly. Multithreading could certainly be leveraged here too.

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

Sidebar

Related Questions

Dynamic integer will be any number from 0 to 150. i.e. - number returns
We have some integer arithmetic which for historical reasons has to work the same
I have an integer field in a ClientDataSet and I need to compare to
I have an integer array in my Objective-C program. I'd like to sort it
How can i round a calculated mdx measure up to the nearest integer without
I have an integer property which is updated every second with a signal-strength value
I have an integer myInt in some Objective-C code. myInt is used as a
I have class representing a unit of measure with a Decimal as the numeric.
I've got a table where I used integer on a field which needs decimal
I have a Rails Model: ruby-1.9.2-p0 > NavItem => NavItem(id: integer, item_identifier: string, description:

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.