Okay here I have a problem, i've written a simple thing that converts numbering systems yeah? Well if I try to convert from binary to any other numbering system, it just stays binary (decimal system) or fux up. Works pretty well for any other numbering system though, and I feel as if the problem is in how I'm expressing the "binary" numbers given to the function. If someone can let me know how I can "express" the "binary" numbers I'll be much obliged.
#include "stdafx.h"
#include "iostream"
#include "math.h"
int *convert(int number,int base)
{
int power = 1;
int count = 0;
while (number > pow((double)base, power))
{
power++;
}
count = power;
int *iArray = new int[count];
int *lArray = new int[count];
for (int k = 0; k < count; k++)
{
iArray[k] = number % base;
number/= base;
}
for (int k = 0; k < count; k++)
{
lArray[k] = iArray[count-k-1];
std::cout << lArray[k] << std::endl;
}
return lArray;
}
int _tmain(int argc, _TCHAR* argv[])
{
convert(123456789,16);
return 0;
}
In case it helps, I'm using this to change from decimal and hex numbers into binary, doing xors and what not on the binary numbers, and then return back to decimal/hex. The problem is the returning from binary to decimal/hex.
Weird that google doesn't bring up a single thing about binary to decimal algos but tons of the opposite. Now that I've had time to ponder over it I think that the change won't be so difficult after all. I'll lay out my idea (which isn't new but if someone else wasn't sure how to go about it neither then this should help them too I hope!).
Okay, well here is a number in the base 2 system, and beneath it the power to the base of its location. (Like ones tens hundreds)
0001
3210
If I start at the left of the string, and multiply the 1's or 0's by the number created from 2^(number below current number) and then just add it up, I think it'll give me the number I need.
In this example it's 0 * (2 ^3) + 0 * (2 ^ 2) + 0 * (2^1) + 1 * (2^0) = 1, which is correct!
Okay so I guess I figured it out :P
//Define the current bit thingy... (you'l see further in the code)
int baseVal = base;
//64 being the maximum length of the bit range (64bit unsigned)
for(int c = 0; c != 65; c++) {
//Does c's bit value go outside that of the value to have?
if(value >= baseVal) {
power = c;
break;
}
//Update the base and value
baseVal *= base;
}
This will basically output the length to the power integer you defined.
An example scenario would be
base = 2; value = 13;
power would be set to 4.
I don't know c++ but I am working from c# code and not sure if you would add pointers in the for loop
Last edited by tappatekie on Mon Dec 05, 2011 8:45 pm, edited 1 time in total.
Kind of all of it, I don't know where your really coming from about your answer, as in I don't know if you got what I was asking for. I was having troubles with going from binary to a different number system, not about the length of anything ?
I do appreciate your serious reply though, it was the first one all day.
Rebornxeno wrote:Kind of all of it, I don't know where your really coming from about your answer, as in I don't know if you got what I was asking for. I was having troubles with going from binary to a different number system, not about the length of anything ?
I do appreciate your serious reply though, it was the first one all day.
What numbering system are you having? Hex (if it is a numbering system)?
Well the code in my first post can change the first number, into any numbering system specified. Try it! And you can specify hex values by 0x, so hex can be changed into any numbering system too. Now try to change from a binary number to something else. Whoops! Can't specify a binary number, and that was the problem. I don't mind the numbering system I can change it into, just had to figure HOW to do it.
Wow I'd love that about 2 hours ago ha ha but I think this little snippet turns it into decimal system for me, but thanks again anyways :D
Rebornxeno, I just scanned this thread so I don't know if your problem has been solved, but it sounds to me like you're trying to do something similar to base64 encoding, but a little more generic. You can look up base64 implementations (there are plenty out there) and adapt them for more than one possible base.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.