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
ADVERTISEMENT
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
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
View Related
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
May 7, 2013
i am converting a base 10 number to base 4.
View 1 Replies
View Related
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Oct 19, 2014
I recall when I first started playing with C++ I was told that you should never use virtual functions unless you absolutely cannot think of a better way to do whatever you are attempting. This is something I have tried to stick to over the years - and indeed is probably why I have never used inheritance or polymorphism much in my own programmes.
However, I notice through a great deal of the code examples offered to questions here and even over on StackOverflow that commentators show no hesitation to recommend code that involves virtual functions. More so, I have even seen several instances here where - what I was taught as, but they may well have a different official name - 'pure virtual functions' (those with definitions inside a class of something like virtual int function_name(void)=0) are demonstrated and I was very clearly taught to avoid those like the plague.
I was wondering therefore has the official thinking changed since the middle nineties on when - and even whether - to use virtual functions in your programmes?
View 9 Replies
View Related
Sep 16, 2014
I have couple of objects which are using some amount of methods. Right now my application is not OOP and i am going to transfer it to OOP. So i would create each class for my each object. Almost all methods are in use for each object, the only one thing which is changing some of those objects passing not all parameters inside specific method. I can go two ways one is prepare interface for all methods i got and each of my classes could implement its own definition for it but other way almost all would implement exactly the same methods but with different parameters so maybe its better to create base class then for each object do inheritance from base class (polymorphism). inside base class i can prepare base methods and classes which will inherit from that class would override those methods for requirements they want.
So imagine: You got:
Memory
CPU
Latency
and all of them using mostly all of those same methods (only arguments for one of them could be use different way):
ExecuteQuery()
ExportToExcel()
PopulateDataTable()
PutValueToReport()
Now its better to do like this e.g:
Base class: Stuff
prop: name, id, date ...
methods to ovveride: ExecuteQuery(), ExportToExcel() ...
classes: CPU, Memory, Latency (inheriting from Stuff)
ovveride methods and align its definition for specific class use (remember mostly only passing args are used or not by specific class)
or go with inheritance
Interface SomeMethods
ExecuteQuery()
ExportToExcel()
PopulateDataTable()
PutValueToReport()
and each class would implemet its own definition.
The most important thing is that those every objects mostly using all of those methods and what is diffrence that one object can use all of available parameters inside this method and other one no. What i should do? Go with interface or inheritance and polymporfizm inside base class?
View 2 Replies
View Related
Apr 9, 2014
I have encountered following lines in base class and I do not comprehend its meaning of "= 0" at the end of the member functions;
distance_list intersect(ray & r) = 0;
appearance get_appearance(vector & pt) = 0;
where distance_list is a list of doubles and appearance is properties.
In general, what does this "equal sign and 0 " mean for the member functions in the base class?
View 3 Replies
View Related
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