Page 1 of 1
Halp ! T.T
Posted: Mon Dec 05, 2011 9:57 am
by Rebornxeno
YO yo wadupp peepz REBORN IN DA HIZOUSE!
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.
Code: Select all
#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.
Re: Halp ! T.T
Posted: Mon Dec 05, 2011 5:31 pm
by dandymcgee
Rebornxeno wrote:YO yo wadupp peepz REBORN IN DA HIZOUSE!
Certainly not your IQ.
Re: Halp ! T.T
Posted: Mon Dec 05, 2011 5:40 pm
by Rebornxeno
Certainly not your sense of humor.
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
Re: Halp ! T.T
Posted: Mon Dec 05, 2011 8:23 pm
by tappatekie
Okay,Im basing this as a serious question.
From my experience, I never used power functions to calculate the actual length of the bits. So instead of
Rebornxeno wrote:
Code: Select all
while (number > pow((double)base, power))
{
power++;
}
I would try
Code: Select all
//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
Re: Halp ! T.T
Posted: Mon Dec 05, 2011 8:43 pm
by Rebornxeno
Lol I have no idea wtf your talking about. And yeah I think this was a pretty serious question.
Re: Halp ! T.T
Posted: Mon Dec 05, 2011 8:46 pm
by tappatekie
Rebornxeno wrote:Lol I have no idea wtf your talking about
What part?
Re: Halp ! T.T
Posted: Mon Dec 05, 2011 8:49 pm
by Rebornxeno
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.
Re: Halp ! T.T
Posted: Mon Dec 05, 2011 8:52 pm
by tappatekie
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)?
Edit: No problem :P
Re: Halp ! T.T
Posted: Mon Dec 05, 2011 8:54 pm
by tappatekie
Oh, and if you have'nt fixed your dilema, I'd be happy to write an app that converts binary to int and hex. But at a later time
Re: Halp ! T.T
Posted: Mon Dec 05, 2011 8:59 pm
by Rebornxeno
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
Code: Select all
int count = ia.size;
int var = 0;
for (int g = 0; g < count; g++)
{
var += ia.ip[g] * (pow((double)2, (count - g)-1));
}
Re: Halp ! T.T
Posted: Mon Dec 05, 2011 9:01 pm
by tappatekie
Oh okay, and no problem.
Re: Halp ! T.T
Posted: Tue Dec 06, 2011 9:20 am
by Ginto8
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.