LinuxQuestions.org How do I add two octal number ?
 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.

 05-20-2004, 09:58 AM #1 Linh Member   Registered: Apr 2003 Posts: 178 Rep: 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);
05-20-2004, 10:52 AM   #2
Hko
Senior Member

Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep:
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.

 05-20-2004, 11:33 AM #3 Linh Member   Registered: Apr 2003 Posts: 178 Original Poster Rep: reply Thank you Hko for your help.
 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: 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...

 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 Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post pjordan Programming 2 11-18-2004 03:03 PM Linh Programming 4 05-20-2004 03:21 PM needforspeed Programming 10 02-10-2004 12:16 AM kawaii Linux - Newbie 2 11-02-2003 08:27 AM Fascistchicken Linux - General 4 09-05-2003 05:44 PM

LinuxQuestions.org

All times are GMT -5. The time now is 09:18 AM.

 Contact Us - Advertising Info - Rules - Privacy - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -