Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Symbolic representation of hexadecimal numbers

Subject: Symbolic representation of hexadecimal numbers

From: Luca Parodi

Date: 12 Mar, 2002 21:51:31

Message: 1 of 4

I have to do with big numbers, with more than 16 digits.
How can I execute operations when numbers are in hexadecimal format?
The symbolic representation (with 'sym') seem to work only with integers,
isn't it? No problem with integers, using SYM and the VPA function, but what
about hex?

Thanks. Luca Parodi

Subject: Symbolic representation of hexadecimal numbers

From: Tom Davis

Date: 12 Mar, 2002 20:24:48

Message: 2 of 4

digits(50)
astr = 'AB54A98CEB1F0AD2';
bstr = 'AB54A98CDC6770B1';
ahex = sym(astr);
bhex = sym(bstr);

adec = maple('convert',ahex,'decimal','hex') % = 12345678901234567890
bdec = maple('convert',bhex,'decimal','hex') % = 12345678900987654321

cdec = char(vpa(adec+bdec)) % = 24691357802222222211.
cdec = cdec(1:end-1) % remove decimal point
chex = maple('convert',cdec,'hex','decimal') % = `156A95319C7867B83`


Luca Parodi wrote:
>
> I have to do with big numbers, with more than 16 digits.
> How can I execute operations when numbers are in hexadecimal format?
> The symbolic representation (with 'sym') seem to work only with integers,
> isn't it? No problem with integers, using SYM and the VPA function, but what
> about hex?
>
> Thanks. Luca Parodi

Subject: Symbolic representation of hexadecimal numbers

From: Nabeel

Date: 12 Mar, 2002 21:20:24

Message: 3 of 4

Hi,

> I have to do with big numbers, with more than 16 digits.
> How can I execute operations when numbers are in hexadecimal format?
> The symbolic representation (with 'sym') seem to work only with integers,
> isn't it? No problem with integers, using SYM and the VPA function, but what
> about hex?

SYM and VPA work with fractions, too. I don't believe they can handle
hex numbers, though, but Tom Davis posted a solution that you might find
useful.

-- Nabeel

Subject: Symbolic representation of hexadecimal numbers

From: Tom Davis

Date: 18 Mar, 2002 21:19:22

Message: 4 of 4

% Variable Precision Hexadecimal Arithmetic

digits(50)
astr = '1A';
bstr = '2B';

% prefix and suffix strings with backquotes
ahex = sym(['`',astr,'`']);
bhex = sym(['`',bstr,'`']);

adec = svmaple('convert',ahex,'decimal','hex') % = 26
bdec = svmaple('convert',bhex,'decimal','hex') % = 43

cdec = char(vpa(adec+bdec)) % = 69.
cdec = cdec(1:end-1) % remove decimal point
chex = svmaple('convert',cdec,'hex','decimal') % = `45`

% check
cdec = svmaple('convert',chex,'decimal','hex') % = 69


Luca Parodi wrote:
>
> Hello Tom,
> excuse me but I'd need your help another time, if it's possible.
> The following piece of program in Matlab calculates the sum between two big
> hexadecimal numbers, thanks above all to your last help. But it seems to
> refuse numbers whose first character is a number and not a letter. Why,
> according to you? How can I solve the problem?
> Many many thanks. (Luca Parodi)
>
> fid1 = fopen('ristest.txt','rt');
> fid2 = fopen('risult.txt','wt');
> digits(999);
> while 1
> s = fgetL(fid1)
> if ~ischar(s), break, end
> astr = s;
> ahex = sym(astr);
> t = fgetL(fid1);
> if ~ischar(s), break, end
> bstr = t;
> bhex = sym(bstr);
> adec = maple('convert',ahex,'decimal','hex')
> bdec = maple('convert',bhex,'decimal','hex')
> cdec = vpa(adec+bdec)
> d = char(cdec)
> d = d(1:end-1)
> chex = maple('convert',d,'hex','decimal')
> fprintf(fid2,'%s',chex);
> fprintf(fid2,'\r');
> fprintf(fid2,'\r');
> fprintf(fid2,'\r');
> v = fgetL(fid1);
> end
> fclose(fid1)
> fclose(fid2)

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us