Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!cs.utexas.edu!uunet!iconsys!tom From: tom@iconsys.UUCP (Tom Kimpton) Newsgroups: comp.sys.mac.programmer Subject: Re: conversion between hex and dec Keywords: conversions Message-ID: <382@iconsys.UUCP> Date: 26 Jun 89 22:51:30 GMT References: <1300@osupyr.mps.ohio-state.edu> <354@mountn.dec.com> Reply-To: tom@iconsys.UUCP (Tom Kimpton) Organization: ICON International, Inc., Orem, UT Lines: 64 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