• Status: Solved
• Priority: Medium
• Security: Public
• Views: 284

# C++ program add two numbers in base 8 (octal)

I need a program that adds two numbers in base 8.  Can anyone help.

thanks
0
• 2
1 Solution

Commented:

End Sub

Function Base8Add(num1 As String, num2 As String) As String

Dim x As Long
Dim y As Long
Dim z As Long
Dim c As Long
Dim m As Long

For c = Len(num1) To 1 Step -1
z = z + Val(Mid(num1, c, 1)) * 8 ^ m
m = m + 1
Next

m = 0
For c = Len(num2) To 1 Step -1
z = z + Val(Mid(num2, c, 1)) * 8 ^ (c - 1)
m = m + 1
Next

While z > 0
z = z \ 8
Wend

End Function
0

Author Commented:
This is a little beyond me.

My starting code is in binary.  I just wanted to make it octal and add the numbers.

*/

#include <stdio.h>

short decimal2binary(unsigned long decimal_value, char

binary_value[32])
{
short index,significant_digits=0;
unsigned long temp_value;
for(index=31;index>=0;index--)
{
// temp_value=decimal_value/pow(2,index)
temp_value=decimal_value/(1<<index);
if(temp_value>0)
{
binary_value[index]=(char)('0'+temp_value);
// decimal_value=decimal_value%pow(2,index)
decimal_value=decimal_value%(1<<index);
if(!significant_digits)
significant_digits=index;
}
else
{
binary_value[index]='0';
}
}
return significant_digits;
}

short decimal2hex(unsigned long decimal_value, char hex_value[8])
{
short index,significant_digits=0;
unsigned long temp_value;
for(index=7;index>=0;index--)
{
// temp_value=decimal_value/pow(16,index)
temp_value=decimal_value/(1<<(index<<2));
if(temp_value>9)
{
hex_value[index]=(char)('A'-10+temp_value);
// decimal_value=decimal_value%pow(16,index)
decimal_value=decimal_value%(1<<(index<<2));
if(!significant_digits)
significant_digits=index;
}
else if(temp_value>0)
{
hex_value[index]=(char)('0'+temp_value);
// decimal_value=decimal_value%pow(16,index)
decimal_value=decimal_value%(1<<(index<<2));
if(!significant_digits)
significant_digits=index;
}
else
{
hex_value[index]='0';
}
}
return significant_digits;
}

void main()
{
short significant_digits,index;
char hex_value[8];
char binary_value[32];
/*
* Hex conversion
*/
significant_digits=decimal2hex(0x0123FEDC,hex_value);
printf("0x0123FEDC = 0x");
/*
* for(index=8;index>=0;index--)
*/
for(index=significant_digits;index>=0;index--)
printf("%c",hex_value[index]);
/*
* Binary conversion next
*/
significant_digits=decimal2binary(0x0123FEDC,binary_value);
printf(" = 0b");
/*
* for(index=31;index>=0;index--)
*/
for(index=significant_digits;index>=0;index--)
printf("%c",binary_value[index]);
printf("\n");
}
0

Commented:
you need to send that to the c++ section, this is the vb section - or do you want a c++ to VB conversion of that code?
0

## Featured Post

• 2
Tackle projects and never again get stuck behind a technical roadblock.