[comp.sys.mac.programmer] conversion between hex and dec

pgn@osupyr.mps.ohio-state.edu (Paul G. Nevai) (06/25/89)

Is there a way to convert from HEX to decimal and vice versa?
Is anything built into Pascal or the Toolbox?
Please respond to my edu address.
Thanks.
Paul


Paul Nevai                                pgn@osupyr.mast.ohio-state.edu
Department of Mathematics                 TS1171@ohstvma.BITNET
The Ohio State University                 73057,172.Compu$erve
231 West Eighteenth Avenue                1-(614)-292-5310.office
Columbus, OH 43210, U.S.A.                1-(614)-292-4975.department

minow@mountn.dec.com (Martin Minow) (06/25/89)

Paul Nevai asks for conversion routines between decimal and hex.  Here's
a subroutine to convert from hex to ascii.  Conversion from strings to
hex is about as simple.

	void
	hex_to_string(value, result)
	unsigned short	value;
	Str255		result;
	{
		int	i, v;
		result[0] = (sizeof value) * 2;	/* 8-bit bytes!		*/
		for (i = result[0]; i >= 1; --i) {
		    v = value & 0xF;
		    value >>= 4;
		    result[i] = (v <= 9) ? v + '0' : v + ('A' - 10);
		}
	}

Martin Minow
minow%thundr.dec@decwrl.dec.com

tim@hoptoad.uucp (Tim Maroney) (06/26/89)

In article <354@mountn.dec.com> minow%thundr.dec@decwrl.dec.com (Martin Minow)
writes:
>
>Paul Nevai asks for conversion routines between decimal and hex.  Here's
>a subroutine to convert from hex to ascii.  Conversion from strings to
>hex is about as simple.

I appreciate your providing this subroutine to the net, but for the
Mac, you should have stuck on a disclaimer warning about its
non-international nature.  Unfortunately, there's no international
hexadecimal trap built in to the Mac, so developer utilities that need
hex are intrinsically less internationalizable than most other
software.  The moral is to only use hex where you can't avoid it (e.g.,
ResEdit, Fedit).  If you do have to use hex, it might be worth your
while to put the hex routines in a separable code resource so people
using some other languages won't have to recompile the whole thing.
-- 
Tim Maroney, Mac Software Consultant, sun!hoptoad!tim, tim@toad.com
Postal: 424 Tehama, SF CA 94103; Phone: (415) 495-2934

"Everything that gives us pleasure gives us pain to measure it by."
    -- The Residents, GOD IN THREE PERSONS

minow@mountn.dec.com (Martin Minow) (06/26/89)

In article <7765@hoptoad.uucp> tim@hoptoad.UUCP (Tim Maroney) notes that
the hex->ascii conversion program I posted was not internationalized.

Touche.  Umm, Touch<option-E>e. Err, Touch<compose>-e-", Hmm, never mind.

The simplest way to internationalize this would be to define a STR resource
containing the text representation of the hex equivalents.  Then, after
pulling off four bits, you need only store the character at
	(*hex_string)[v].
Note that, to do the job right, you'll have to know the number of bytes
in a character.

Note that the same problem occurs in decimal->ascii conversion: Hebrew,
Arabic, and Japanese are among those languages that have "native" digit
representations that are used alongside, and sometimes in place of, 0..9.
(It is especially important to realize that the choice of digit representation
may be dependent on the meaning of the text: dates and prices may use one
format, while scientific data uses another.

Martin Minow
minow%thundr.dec@decwrl.dec.com

tom@iconsys.UUCP (Tom Kimpton) (06/27/89)

In article <354@mountn.dec.com> minow%thundr.dec@decwrl.dec.com (Martin Minow) writes:
>
>Paul Nevai asks for conversion routines between decimal and hex.  Here's
>a subroutine to convert from hex to ascii.  Conversion from strings to
>hex is about as simple.
>
>	void
>	hex_to_string(value, result)
>	unsigned short	value;
>	Str255		result;
>	{
>		int	i, v;
>		result[0] = (sizeof value) * 2;	/* 8-bit bytes!		*/
>		for (i = result[0]; i >= 1; --i) {
>		    v = value & 0xF;
>		    value >>= 4;
>		    result[i] = (v <= 9) ? v + '0' : v + ('A' - 10);
		    result[i] = "0123456789ABCDEF"[v];
>		}
>	}
>
>Martin Minow
>minow%thundr.dec@decwrl.dec.com

I saw this in a compiler routine to output hex constants.  I
thought it was kind of cute.

Or ...

	result[0] = sprintf(&result[1],format_string,value) - 1;
		/* subtract 1 for the NULL added to the string */
	
Look in your manual for the numerous formats.

If you want to convert incoming strings, in various formats,
look at the man section for "sscanf()", remembering the
difference between short (int) and long pointers (byte you every time).

f
o
d
d
e
r

f
o
d
d
e
r

f
o
d
d
e
r

-- 
Tom Kimpton                    UUCP: {uunet,caeco,nrc-ut}!iconsys!tom
Software Development Engineer  ARPANET: icon%byuadam.bitnet@cunyvm.cuny.edu
Icon International, Inc.       BITNET: icon%byuadam.bitnet (multi-user acct)
Orem, Utah 84058               PHONE: (801) 225-6888