LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-20-2004, 09:58 AM   #1
Linh
Member
 
Registered: Apr 2003
Posts: 178

Rep: Reputation: 30
How do I add two octal number ?


How do I add two octal number ?


1) The number 27 and 156 are both octal, how do I add the two to where the result will be octal and not decimal which is 183.

a = 27 + 156

2) What function do I use to convert a number to binary, octal, and hexadecimal.

3) The function below convert an octal to string, but what is the function that convert a number to octal ?
single_char = strtoul (three_oct_char, NULL, 8);
 
Old 05-20-2004, 10:52 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Re: How do I add two octal number ?

Assuming you're talking about C code...

Quote:
1) The number 27 and 156 are both octal, how do I add the two to where the result will be octal and not decimal which is 183.

a = 27 + 156
By specifying them as octal numbers by prefixing the numbers with a zero ('0'). Like this:

a = 027 + 0156;

if you also want to print the result as an octal number use %o in the printf:

printf("%o\n", a);

Quote:
2) What function do I use to convert a number to binary, octal, and hexadecimal.
To specify a integer constant inside the C-code, prefix with '0' for octal, '0x' or '0X' for hexadecimal, and binary is not possible (in C).

To convert an integer to a string as octal or hexadecimal:
Code:
#include <stdio.h>

#define LEN 10

int a;
char str[LEN+1];

a = 123;
snprintf(str, LEN, "%o", a);   /* str gets octal representation of a */
snprintf(str, LEN, "%X", a);   /* str gets hexadecimal representation of a */
To get a string in binary is not possible directly (in C) as far as I know. But you can use this function:
Code:
#define NUMBITS 16

/* make sure str can store at least NUMBITS+1 char's 
    for example:   char str[NUMBITS+1];
*/

void strbinary(char *str, int x)
{
     int i;

     for (i = 0; i < NUMBITS; ++i) str[NUMBITS-i-1]=x&1<<i?'1':'0';
     str[NUMBITS] = '\0';
}
To read an integer from a string representing a dec, hex, oct or bin number:
Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
	 int a;

	 /* The strings below all represent (decimal) 123. */

	 a = (int)strtol("123", NULL, 10);  /* str is decimal (base 10) */
	 printf("%d\n", a);

	 a = (int)strtol("1111011", NULL, 2);  /* str is binary (base 2) */
	 printf("%d\n", a);

	 a = (int)strtol("173", NULL, 8);  /* str is octal (base 8) */
	 printf("%d\n", a);

	 a = (int)strtol("7B", NULL, 16);  /* str is hexadeciaml (base 16) */
	 printf("%d\n", a);

	 return 0;
}
Quote:
3) The function below convert an octal to string, but what is the function that convert a number to octal ?
single_char = strtoul (three_oct_char, NULL, 8);
Do you mean: how to print a integer (or single char for that matter) as an octal number? or how to convert an integer to string as octal?
Use printf() or snprintf() for this. See above how.

Last edited by Hko; 05-20-2004 at 11:04 AM.
 
Old 05-20-2004, 11:33 AM   #3
Linh
Member
 
Registered: Apr 2003
Posts: 178

Original Poster
Rep: Reputation: 30
reply

Thank you Hko for your help.
 
Old 05-20-2004, 03:08 PM   #4
vasudevadas
Member
 
Registered: Jul 2003
Location: Bedford, UK
Distribution: Slackware 11.0, LFS 6.1
Posts: 519

Rep: Reputation: 30
Glad you answered that first hko, as otherwise I would have blundereded in and said something like, add the digits and carry the eights of course, and looked a right fool...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Read in an Octal number from a text file using C++ pjordan Programming 2 11-18-2004 03:03 PM
Have problem converting a decimal number to octal Linh Programming 4 05-20-2004 03:21 PM
How do I add character+number to get char in C++? needforspeed Programming 10 02-10-2004 12:16 AM
Octal format kawaii Linux - Newbie 2 11-02-2003 08:27 AM
--- --- --- octal 000 permissions Fascistchicken Linux - General 4 09-05-2003 05:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration