?
Solved

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

Posted on 2003-02-19
3
Medium Priority
?
270 Views
Last Modified: 2010-04-07

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

thanks
0
Comment
Question by:adinkins
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 18

Expert Comment

by:deighton
ID: 7981476
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
 

Author Comment

by:adinkins
ID: 7981900
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
 
LVL 18

Accepted Solution

by:
deighton earned 150 total points
ID: 7982510
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question