Code covered by the BSD License  

Highlights from
Base conversion of very large integers

3.33333

3.3 | 3 ratings Rate this file 4 Downloads (last 30 days) File Size: 14.19 KB File ID: #12003

Base conversion of very large integers

by Alecsander Eitan

 

18 Aug 2006 (Updated 18 Aug 2006)

Base conversion of very large integers

| Watch this File

File Information
Description

This set of functions perform base conversion of very large integers, well beyond the standard Matlab functions that are limited to 52bits. The input and output numbers are represented by strings (and not numbers) to avoid the Matlab limitations.
The main functions are: dec2hex_long, dec2oct_long, dec2bin_long, hex2dec_long, oct2dec_long and bin2dec_long

MATLAB release MATLAB 7.1.0 (R14SP3)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (6)
13 Sep 2006 Kok-Wah Lee

Help to fulfill many demands.

15 Jul 2010 Christopher Hazel  
15 Jul 2010 Christopher Hazel

The bin2dec_long file did not account for the case where the string input contained all zeroes, so I added the following code just before the "conversion setup" lines:

if str2num(str_in)==0;
    str_out=0;
else
%conversion setup
...(file code)
end

31 Jan 2012 Michael

This seems really useful but the answers are the same as de2bi for powers of two larger than 53. Please notice below:

>> uint64(2^60-1)

ans =

  1152921504606846976

>> dec2bin_long('1152921504606846976',60)

ans =

1000000000000000000000000000000000000000000000000000000000000

>> dec2bin_long('1152921504606846976')

ans =

1000000000000000000000000000000000000000000000000000000000000

>> uint64(2^53-1)

ans =

     9007199254740991

>> dec2bin_long('9007199254740991')

ans =

11111111111111111111111111111111111111111111111111111

>> uint64(2^55-1)

ans =

    36028797018963968

>> dec2bin_long('36028797018963968')

ans =

10000000000000000000000000000000000000000000000000000000

31 Jan 2012 John D'Errico

Of course there is no need to be limited by this code, when vpi exists on the file exchange. For example, here is the result of applying it to a 60 decimal digit number, something that is rather difficult to do with other codes.

vpi2bin(vpi('123456789012345678901234567890123456789012345678901234567890'))

ans =
10011101010101111010100000100111001001011110000011110011000100001011100111111100001111010010000110111100011000011011110110100100111001000110011001111111100011001011011001110001111110000101011010010

And if you want to convert to any other base, vpi2base works nicely there too.

vpi2base(vpi('123456789012345678901234567890123456789012345678901234567890'),256)
ans =
  19 170 245 4 228 188 30 98 23 63 135 164 55 140 55 180 156 140 207 241 150 206 63 10 210

A good point is, for a tool that does as little as this does, it is rather complicated, requiring over 20 m-files to accomplish that task.

31 Jan 2012 Michael

OK just saw VPI - never heard of that before... thank you

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
base conversion Cristina McIntire 06 Feb 2009 10:07:07
large integer Cristina McIntire 06 Feb 2009 10:07:07
large integer Krystian 16 Apr 2010 09:42:06

Contact us at files@mathworks.com