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

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
adinkins
Asked:
adinkins
  • 2
1 Solution
 
deightonCommented:
Private Sub Form_Load()

MsgBox Base8Add("32", "7")

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
    Base8Add = CStr(z Mod 8) & Base8Add
    z = z \ 8
Wend

End Function
0
 
adinkinsAuthor 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");
 /*
  * With leading zeros ...
  * 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");
 /*
  * Leading zeros...
  * for(index=31;index>=0;index--)
  */
 for(index=significant_digits;index>=0;index--)
  printf("%c",binary_value[index]);
 printf("\n");
}
0
 
deightonCommented:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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