C :: Algorithm To Convert From Any Base To Base 10 Decimal

Mar 25, 2013

I got this algorithm of conversion and now I'm stuck at how to code it.

"Algorithm to Convert From any Base to Base 10 Decimal."

Let 'n' be the number of digits in the number. For example, 104 has 3 digits, so 'n'=3.
Let 'b' be the base of the number. For example, 104 is decimal so 'b' = 10.
Let 's' be a running total, initially 0.

For each digit in the number, working left to right do:

Subtract 1 from 'n'.
Multiply the digit times b^n and add it to 's'.

When done with all the digits in the number, the decimal value should be 's' .

View 6 Replies


ADVERTISEMENT

C++ :: Convert From Decimal To Binary With Base 2

Dec 7, 2014

I had an exercise to convert from decimal to binary with base 2, it was simple simple i did the following:

Code:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
void Conversion (int n);

[Code] .....

I then had an follow up exercise which was to replicate but for any base up to 10, i thought i would just have to replace 2 with a variable obtained from the user, however this did not work as i got an error saying too few arguments function but i cannot see why i am getting this.

Code:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
float Conversion (int n, int b);

[Code] ......

View 5 Replies View Related

C/C++ :: Convert Decimal To Another Base And Put It Into String

Jun 14, 2014

I have written this function.

void intToBase(unsigned int val,char *buff, unsigned int base) {
int digit = val%base;
if(val == 0)
return;
intToBase((val / base), buff + 1, base);
if(digit >= 0 && digit <= 9)
*buff = (digit + '0');
if(digit >= 10 && digit <= 15)
*buff = (digit + 'A' - 10);
}

This function is suppose to convert a decimal number (val) to any other base number and put it into a string (buff).

But the function puts the value in a opposite way, like if the answer is suppose to be "123" i get this "321".

Note: the function must be recursive and i can't use loops.

View 1 Replies View Related

C++ :: Convert Base 10 To Base X Number Within String Of Characters

Apr 15, 2013

How would you convert say "238273615237287352536266362524382737272" base 10 to a base x number contained within a string of characters?

View 2 Replies View Related

C :: Need To Convert Base 10 Integer To Base 16

Mar 20, 2014

I need to convert an integer, for example 10, to its base 16 equivalent, 16. I found this code that converts a base 16 number to base 10, but I need the opposite. Plus, this code doesn't seem to work properly with input values under 32.

Code:

uint32_t input = 16;
uint8_t temp;
temp = ((input>>8)*100)|((input>>4)*10)|(input&0x0f);

View 13 Replies View Related

C/C++ :: Adding Base-N Through Strings (between Base-2 And Base-36)

Feb 27, 2015

I just wanted to add strings in any base form (example 1101+100 = 10001 in base-2 but it could be added using any base-form like in base-3 to base-36) and I'm having a big trouble with my code because it gave me incorrect results.

addition(char st[], char st2[], int base){
int i, j, carry = 0, ans, len, o=0, z=1, l=0;
char final[50];
if(strlen(st)>=strlen(st2))
len = strlen(st);
else
len = strlen(st2);

[Code] ....

View 1 Replies View Related

C :: Program That Converts Decimal To Any Base (mostly)

Jan 30, 2013

I had the idea to write a program that would convert a decimal integer into any base up to 36 (there are no letters after that) so I decided to give it a try.

Code:
#include <stdio.h>
#include <stdlib.h>
char letters(int r); // prototypes function for converting 10's to A's and so on, base 11+ will work
int main() {

int base;
int d; //declares decimal integer
int d_clone; // clones d for a loop

[Code] ......

View 8 Replies View Related

C++ :: Change Of Base Function (hex To Octal Or Decimal)

Oct 6, 2014

I need to create a generic function that changes from any starting base, to any final base. I have everything down, except my original function took (and takes) an int value for the number that it converts to another base. I decided to just overload the function. I am Ok with changing between every base, but am slightly off when using my new function to take in a string hex value.

The code below should output 1235 for both functions. It does for the first one, but for the second, I am currently getting 1347. Decimal to Hex works fine - It's just the overloaded function (Hex to anything else) that is slightly off.

#include <iostream>
#include <stack>
#include <string>
#include <cmath>

using namespace std;

void switchBasesFunction(stack<int> & myStack, int startBase, int finalBase, int num);
void switchBasesFunction(stack<int> & myStack, int startBase, int finalBase, string s);

[Code] .....

View 2 Replies View Related

C++ :: Enter Decimal Number Into Program And Required Base

Oct 12, 2013

You enter decimal number into the program and what base you want. The integer part of the decimal is being handled fine, but the decimal is not.

For example, I enter 15.6847 and base 10, which means I'm going from base 10 to base 10. It spits out 68469999999999 for the decimal part. (Do not worry about the first block of numbers. The second block seperated from the first by a space is where the decimal will appear in order.)

#include <iostream>
#include <string>
#include <math.h>
using namespace std;
int baseConverter(int, int, int *, int *);

[Code] ....

View 2 Replies View Related

C :: Converting Decimal Number To Any Base Then Find Ones And Twos Complement

Oct 10, 2014

I have the code working on converting a decimal number to base 1 through 16. I need getting the code to output the one's and two's complement.

Code:
#include <stdio.h>
int main(void) {
char base_digits[16] =
{'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

int converted_number[64];
long int number_to_convert;
int next_digit, base, index=0, a;

/* get the number and base */

[Code] ....

View 8 Replies View Related

C++ :: Convert Big Numbers To Other Base 64

Oct 2, 2014

I'm working on a program and I need to convert big numbers to radix 64. I would like to shorter them whit conversion that's why I choosed base 64. I have two problems:

1. The conversion works only for not so big numbers. Untill about 2^32. I would like to convert bigger numbers. Is there any solution? (I thought on GMP/MPIR library, but I can't managed it.)

2. The conversion back to decimal base doesn't works, because I use 'strtoul()' which doesn't support bigger bases like 36.

Is there any good method for that?

View 12 Replies View Related

C :: How To Convert Number 75 To Base 4 Representation

Mar 6, 2015

How to convert e.g. number 75 to base 4 representation? The result should be 1023. I plan to use uint32_t

x = a3.43 + a2.42 + a1.41 + a0.40 = a3.64 + a2.16 + a1.4 + a0

View 9 Replies View Related

C :: How To Convert A String To MIME Base 64

Dec 1, 2013

I've been trying to write some code to do what I mentioned in the title, but I haven't had much luck. I get very confused when I deal with bitwise operators, so it's hard for me to write this kind of encoding on my own. I need this encodement so I can login to an email account using smtp.

Here is my code so far :

Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
const char base64_table[] =

[Code]......

Gives the output :

Code:
11C
byte offset 0, new_text_sz = 5
11C
byte offset 1, new_text_sz = 5
11C
byte offset 2, new_text_sz = 5
11C
byte offset 3, new_text_sz = 5
AAAA

When according to wikipedia, it should be : T W F u

View 9 Replies View Related

C :: Convert Number To Binary From And Base Up To 10

Dec 7, 2014

I had an exercise that required me to convert a number to binary (base 2) which as simple enough.

Code:
#include <iostream>#include <iomanip>
#include <cmath>

using namespace std;
void Conversion (int n);
int main () {

[Code] .....

I now have a follow on exercise that requires me to convert to binary from ant base up to 10, i thought this would just be replacing the 2 with a variable obtained form the user, but i am having problems as within the function i am getting an error that i haven't passed enough arguments and i cant see why i get this. I did the following:

Code:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
float Conversion (int n, int b);

[Code] ....

View 2 Replies View Related

C++ :: Converting From Base 10 To Base 4

May 7, 2013

i am converting a base 10 number to base 4.

View 1 Replies View Related

C++ :: Converting Base N To Base M

Feb 8, 2013

I was wondering if there is a standard for converting something like this:

HELLO 27 to ?3

View 5 Replies View Related

C :: Base Number Representation

Jul 23, 2014

I am using MCC18 for use with MPLAB.

0bnnnnnnnn means a binary representation.
0x00 means a hex number representation.

What does sd001 means? signed decimal 1? if so how do I represent a -1 using this sdnnnn format?

View 3 Replies View Related

C++ :: Base Class As Parameter

Oct 14, 2014

I have run into a problem which is mostly just an annoyance. I need to know if i can have pass a derived class to a function which has the base class as its parameter. For example i have been creating a program but i have a function which needs to work for multiple classes all derived from the BaseObject class

Code :

class folder : public BaseObject
{}
class BaseObject
{void function(BaseObject B)}

how would i get the following to work:

function(folder)

View 3 Replies View Related

C++ :: A Pointer To Base Class

May 18, 2013

A pointer to base class, if assigned to a derived class, only points to the base part right? So you can only use the base part of the derived class with that pointer and no methods from the derived class?

View 8 Replies View Related

C++ ::  Inheritance Using Base Method With Other Name?

Feb 18, 2015

I have 2 classes with a Function with the same definition (both inherited from the same base class) and in my derived class I derive from both of those 2. Is it possible to use the Methods of both classes? for example with an other name?

class A {
protected:
int print(int a) { std::cout << "int A: " << a << std::endl; }
};
class B : A

[Code] ....

is there something like using C::print as printc;?

The Problem, I have a Sprite class that derives from a Rectangle with properties Position, Size, Origin and Angle and a Text class that derives from Rectangle. Now i have a Button class deriving from both Sprite and Text.

- The Position, when moving the Button i have to change the position of both so i Implemented a new Method which calls SetPosition from the Sprite and the Text.
- The SetSize just affects the Button so i just did using Sprite::SetSize;
- The angle affects both so i just implemented a new Method and hide the other two

The problem is here:
- The Origin: writing button.SetOrigin(-1,0) should set the Origin of the Button and writing button.SetTextOrigin should set the Origin of the text.

Should i just reimplement a Mehtod named SetTextOrigin and call Text::SetOrigin from there and hide the button.Text::SetOrigin or is there something like using Text::SetOrigin as SetTextOrigin?

View 6 Replies View Related

C++ :: Calculate Logarithm With Own Base

Sep 10, 2013

How can i calculate a logarithm with my own base for example log on base 0.52 ?

View 2 Replies View Related

C/C++ :: Function Within A Base Class

Mar 1, 2012

I am working on a program for school and I have managed to get everything to work, except the last portion of the program. It seems that the program is not calling the last function EmployeeSummary. The program will compile, and will accept data and compute overtime pay, etc. Just won't go on to the last function.
 
#include <iostream>
#include <string>
#include <iomanip>  
using namespace std;  
class CPayroll {  

[Code] ...

View 3 Replies View Related

C++ :: Base Class Pointer Destructor

Aug 29, 2014

I have a query regarding virtual destructor functionality. So below is sample code:

Code:
#include<iostream>
#include<stdlib.h>
using namespace std;
class Base

Code: Base *b = d;
Here b and d now pointing to same memory location.

Now below statement:
Code: delete (b);
Here since my destructor is virtual it will call derived class and base class destructor.

Now my question is, if I use this:
Code: delete (d); // And without virtual keyword in ~Base() {} This call both derived and base class destructor.

So which one is correct form to call and why? Is delete(b) is standard in virtual function mechanism.

Output is:

D1 :: function1()
Base :: function2()
INSIDE D1 DES
INSIDE BASE DES

View 2 Replies View Related

C++ :: Check If Number Belongs To Particular Base Or Not

May 2, 2013

I am making a number base conversion program in c++, suppose that user enters an base 2 number and want to convert that number in base 4. when he enter the number (which he wants to be converted), how can i detect that number is belongs to base 2 number system or not.

For example: if user enter 1001011001 , program should continue and convert that number to base 4. and if user enter 1200101 or 1001012 , program should not convert that number and output that this number is not belongs to base 2 number system.

View 6 Replies View Related

C++ :: How To Access Virtual Base Class

May 4, 2013

How can I access the virtual base class? This is a practice exercise from c++ primer plus 6.

The problem is that the name becomes No Name instead of the name specified when creating the gunslinger, I don't know how I can call the virtual base class explicitly

Output,

#ifndef PERSON_H_
#define PERSON_H_
#include <string>
#include <iostream>
#include <cstdlib>
using std::string;
class person

[code]....

View 7 Replies View Related

C++ :: Abstract Base Class With Templates?

Oct 3, 2014

I want to create an abstract base class having a member function that can accept a templatized structure as its parameter, something that according to C++'s rules can't be done for a good reason.

That good reason it is because an abstract base class is intended to provide interface rules to the classes that will derive from it and should not deal with data.

But how would you go about doing something like the following which is probably a reasonable design decision?

template<typename T>
class Matrix { /* ... */ };
class iFile {
public:
virtual ~iFile() {} = 0;
virtual void Process(const Matrix<T>&) = 0;

[Code] .....

View 5 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved