looking for a program
Gorman, John
John.Gorman@pegs.com
Wed, 7 Feb 2001 08:44:54 -0700
Here is one of my tools that accomplishes what you want. It will
convert to/from any base 2-16.
i.e.
$ baseconv 255 16 (decimal 255 to hex)
255 in base 16 is: ff
$ baseconv 255 2 (decimal 10 255 to binary)
255 in base 2 is: 1111 1111
$ baseconv 0x255 10 (hex to decimal)
0x255 in base 10 is: 597
$ baseconv /13 10 (octal to decimal)
/13 in base 10 is: 11
To use cut/paste, compile, enjoy.
Command line rules!
John Gorman
---[ BEGIN CUT ]----------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
/********************************************************************
*
* Function: baseconv
*
* Author: John Gorman (JHGorman@yahoo.com)
*
* Created: 7/23/1998
*
* Description: This program will accept as input 2 values
* the first value will be a number (base-10)
* the second number will be a base in which to
* display the first value
*
* Input: int number (base-10)
* int base (2-16)
*
* Output: string representing the base-10 value in the
* base requested in input value 2
*
* Updates: Oct 23, 1998, John Gorman
* -- Added the ability to accept an octal number
*
*******************************************************************
*/
char* baseconv(unsigned long num, int base)
{
static char retbuf[100];
char *p;
int iCnt=0;
if (base < 2 || base > 16)
{
return NULL;
}
p = &retbuf[sizeof(retbuf)-1];
*p = '\0';
do
{
*--p = "0123456789abcdef"[num % base];
num /= base;
if (base == 2)
{
iCnt++;
if ( (iCnt %4) == 0)
{
*--p = ' ';
}
}
}
while(num != 0);
return p;
}
void main(int argc, char *argv[])
{
int number;
int base;
unsigned long lNumber;
char *result;
char *endptr;
if (argc == 3)
{
base = atoi(argv[2]);
if (argv[1][0] == '0' && argv[1][1] == 'x')
{
number = lNumber = strtoul(argv[1], &endptr, 16);
}
else if (argv[1][0] == '/')
{
number = lNumber = strtoul(argv[1]+1, &endptr,8);
}
else
{
number = lNumber = strtoul(argv[1], &endptr,10);
/*number = atoi(argv[1]);*/
}
}
else
{
printf("\nusage: %s number base(2-16)\n", argv[0]);
printf(" where number is either a base 10 number, OR\n");
printf(" a base 16 number (i.e. 0xf1), OR\n");
printf(" a base 8 number (i.e. /32)\n\n");
exit(-1);
}
result = baseconv(lNumber, base);
printf("%s in base %d is: %s\n", argv[1], base, result);
return;
}
---[ END CUT ]----------------------------------------------------
-----Original Message-----
From: Lucas Vogel [mailto:lvogel@exponent.com]
Sent: Tuesday, February 06, 2001 7:04 PM
To: 'plug-discuss@lists.plug.phoenix.az.us'
Subject: looking for a program
I'm looking for a program that will take either a decimal, hex or octal
input and give me its decimal, hex or octal output. Can anyone tell me what
I'm looking for?
Lucas "I Spy" Vogel
________________________________________________
See http://PLUG.phoenix.az.us/navigator-mail.shtml if your mail doesn't post
to the list quickly and you use Netscape to write mail.
Plug-discuss mailing list - Plug-discuss@lists.PLUG.phoenix.az.us
http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-discuss