Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using fprintf to output hex values

592 views
Skip to first unread message

Steve Heidorn

unread,
May 16, 1995, 3:00:00 AM5/16/95
to
Subject: Using fprintf to print hex values

I'm having trouble using fprintf with the %x format specifier when
printing negative numbers. For instance,

fprintf('%x\n',11) = b

but

fprintf('%x\n',-11) = -1.1000000e+00

In other words, the input argument of '-11' was passed through unchanged.
We tried this in C, and the output was as expected so long as the input
argument was of type integer. However, C also messed up when the input
argument was of type float. Either way, I seem to be stuck since Matlab
has no notion of integer typing. Matlab support says fprintf is working
as designed. Is there a way around this?

Steve Heidorn Phone: 703-367-5377
Combat Systems Architecture Fax: 703-367-5067
Loral Federal Systems - Manassas Internet: hei...@lfs.loral.com

Tom L. Davis

unread,
May 16, 1995, 3:00:00 AM5/16/95
to
In article <19950516....@lfs.loral.com>, hei...@lfs.loral.com
(Steve Heidorn) wrote:

> Subject: Using fprintf to print hex values
>
> I'm having trouble using fprintf with the %x format specifier when
> printing negative numbers. For instance,
>
> fprintf('%x\n',11) = b
>
> but
>
> fprintf('%x\n',-11) = -1.1000000e+00
>
> In other words, the input argument of '-11' was passed through unchanged.
> We tried this in C, and the output was as expected so long as the input
> argument was of type integer. However, C also messed up when the input
> argument was of type float. Either way, I seem to be stuck since Matlab
> has no notion of integer typing. Matlab support says fprintf is working
> as designed. Is there a way around this?

The problem is double precision floating point can represent integers upto
2^52 or so. The x format assumes unsigned 32 bit integers. The following
code converts negative integers to 32 bit integers:

x = -11;
y = mod( floor(x), pow2(32) );
fprintf( 1, '%08x\n', y )

Let me know if you find a better, faster way. Here's TMW's suggestion for
the mod function:

function z = mod(x, y)
%MOD Remainder after division.
% If X and Y are matrices of the same size, or one of them is a scalar,
% then both MOD(X,Y) and the built-in function REM(X,Y) compute the
% remainder after division of the elements of X by the elements of Y.
% The two functions agree where X and Y have the same sign, but
% differ by Y where X and Y have opposite signs.
% Specificially,
% MOD(X, Y) = X - Y .* FLOOR(X ./ Y)
% REM(X, Y) = X - Y .* FIX(X ./ Y)
% This implies that
% SIGN(MOD(X,Y)) = SIGN(Y)
% SIGN(REM(X,Y)) = SIGN(X)
% Note that MOD(X,Y) is a periodic function of X with period Y,
% but that REM(X,Y) is not periodic when X changes sign.
% And, there is one special case: Y = 0, MOD(X, 0) = X, REM(X, 0) = NaN.
%
% Examples:
% mod(0.1000,pi) = 0.1000
% mod(-0.1000,pi) = 3.0416
% rem(0.1000,pi) = 0.1000
% rem(-0.1000,pi) = -0.1000
%
% See also REM, FLOOR, FIX.

% Copyright (c) 1984-94 by The MathWorks, Inc.

z = x - y .* floor(x ./ (y + (y == 0)));

0 new messages