|
| 14 Oct 2011 08:19 PM |
Does anyone know an algorithm I could use? Lua is ok but C++ would be better ;P
~¡Todos los días estoy barajando!~ |
|
|
| Report Abuse |
|
|
|
| 14 Oct 2011 08:23 PM |
lua:
function dectobin(x) -- only works with integers local r = "" for i = 0, 7 do r = (x % 2^i) .. r x = x - (x % 2^i) -- dunno if this is necessary, but i think it is end return tonumber(r) end
easy to change to c++
that is, if i got it right |
|
|
| Report Abuse |
|
|
GoldenUrg
|
  |
| Joined: 23 Aug 2009 |
| Total Posts: 6428 |
|
|
| 14 Oct 2011 08:27 PM |
C has builtin functions for this:
Assuming the "decimal" is a string:
bin = atoi( dec ) dec = itoa( bin ) |
|
|
| Report Abuse |
|
|
|
| 14 Oct 2011 08:29 PM |
function bintodec(x) x = tostring(x) local r for i = #x, 1, -1 do r = r + (1+tonumber(x+sub(i,i))^(#x - i) -- i think end return r end |
|
|
| Report Abuse |
|
|
| |
|
aboy5643a
|
  |
| Joined: 20 Nov 2010 |
| Total Posts: 2785 |
|
|
| 14 Oct 2011 08:31 PM |
@crazy
I'd throw in an automatic rounding equation (math.floor(x+.5) or similar) to make sure it's always an integer? That way if you receive a non-int numeric value it won't break.
"Locked" - Sorcus |
|
|
| Report Abuse |
|
|
GoldenUrg
|
  |
| Joined: 23 Aug 2009 |
| Total Posts: 6428 |
|
|
| 14 Oct 2011 08:32 PM |
In fact, so does Lua:
bin = tonumber( dec ) dec = printf( "%d", bin )
Or if you want string for binary too:
int = tonumber( bin, 2 ) bin = printf( "%b", int ) |
|
|
| Report Abuse |
|
|
|
| 14 Oct 2011 08:32 PM |
| in c++, you can define the type of the argument (string dectobin(int x)), so thats not really necessary if it's just made to be changed to c++ |
|
|
| Report Abuse |
|
|
aboy5643a
|
  |
| Joined: 20 Nov 2010 |
| Total Posts: 2785 |
|
|
| 14 Oct 2011 08:32 PM |
I never kNEW that! :o
+2 cookies for GoldenUrg
"Locked" - Sorcus |
|
|
| Report Abuse |
|
|
|
| 14 Oct 2011 08:33 PM |
@aboy
Why would this get locked? This algorithm isn't very simple and I asked for Lua OR C++...
~¡Todos los días estoy barajando!~ |
|
|
| Report Abuse |
|
|
aboy5643a
|
  |
| Joined: 20 Nov 2010 |
| Total Posts: 2785 |
|
|
| 14 Oct 2011 08:33 PM |
@nickmaster
It's called a siggy, and it's a joke at Sorcus. lolololol
"Locked" - Sorcus |
|
|
| Report Abuse |
|
|
|
| 14 Oct 2011 08:38 PM |
@aboy
Lol, my bad :P
@GoldenUrg
Do I have to import any special libraries?
~¡Todos los días estoy barajando!~ |
|
|
| Report Abuse |
|
|
aboy5643a
|
  |
| Joined: 20 Nov 2010 |
| Total Posts: 2785 |
|
|
| 14 Oct 2011 08:39 PM |
"C has builtin functions for this"
I took that to mean its builtin to all C libraries...
"Locked" - Sorcus |
|
|
| Report Abuse |
|
|
|
| 14 Oct 2011 08:40 PM |
Oh...
~¡Todos los días estoy barajando!~ |
|
|
| Report Abuse |
|
|
|
| 14 Oct 2011 08:44 PM |
C:\Users\Fifty7 Designs\Desktop\Coding\For My First Thing\My First Code\main.cpp|24|error: 'itoa' was not declared in this scope|
Um...
~¡Todos los días estoy barajando!~ |
|
|
| Report Abuse |
|
|
|
| 14 Oct 2011 08:55 PM |
How I fix this? --> "error: 'itoa' was not declared in this scope"
~¡Todos los días estoy barajando!~ |
|
|
| Report Abuse |
|
|
XlegoX
|
  |
| Joined: 16 Jun 2008 |
| Total Posts: 14955 |
|
|
| 14 Oct 2011 08:58 PM |
include stdlib.h
That's where those sort of global C functions are usually located. |
|
|
| Report Abuse |
|
|
GoldenUrg
|
  |
| Joined: 23 Aug 2009 |
| Total Posts: 6428 |
|
|
| 14 Oct 2011 09:00 PM |
Try: #include < stdlib.h >
If you're on a Linux OS, "man atoi" should tell you. (And Google is pretty good for that, too).
|
|
|
| Report Abuse |
|
|
|
| 14 Oct 2011 09:00 PM |
What the heck?
"error: too few arguments to function 'char* itoa(int, char*, int)'|"
Part of the code:
int fromBianary() { int Total; Total = 0; int Convert; cout << "Please enter a bianary number." << endl << ">"; cin >> Convert; Total = itoa(Convert); return Total; };
~¡Todos los días estoy barajando!~ |
|
|
| Report Abuse |
|
|
XlegoX
|
  |
| Joined: 16 Jun 2008 |
| Total Posts: 14955 |
|
|
| 14 Oct 2011 09:07 PM |
What? itoa is for converting Integers(i) to Characters(a)
You want atoi. |
|
|
| Report Abuse |
|
|
|
| 15 Oct 2011 01:56 PM |
Part of the code:
int fromBianary() { int Total; Total = 0; int Convert; cout << "Please enter a bianary number." << endl << ">"; cin >> Convert; Total = atoi(Convert); return Total; };
int toBianary() { int Total; Total = 0; int Convert; cout << "Please enter a decimal number." << endl << ">"; cin >> Convert; Total = atoi(Convert); return Total; };
The build messages:
C:\Users\Fifty7 Designs\Desktop\Coding\For My First Thing\My First Code\main.cpp||In function 'int fromBianary()':| C:\Users\Fifty7 Designs\Desktop\Coding\For My First Thing\My First Code\main.cpp|25|error: invalid conversion from 'int' to 'const char*'| C:\Users\Fifty7 Designs\Desktop\Coding\For My First Thing\My First Code\main.cpp|25|error: initializing argument 1 of 'int atoi(const char*)'| C:\Users\Fifty7 Designs\Desktop\Coding\For My First Thing\My First Code\main.cpp||In function 'int toBianary()':| C:\Users\Fifty7 Designs\Desktop\Coding\For My First Thing\My First Code\main.cpp|36|error: invalid conversion from 'int' to 'const char*'| C:\Users\Fifty7 Designs\Desktop\Coding\For My First Thing\My First Code\main.cpp|36|error: initializing argument 1 of 'int atoi(const char*)'| ||=== Build finished: 4 errors, 0 warnings ===|
~¡Todos los días estoy barajando!~ |
|
|
| Report Abuse |
|
|
|
| 15 Oct 2011 01:58 PM |
Yes, I have: #_include <_stdlib.h_>
(without underscores)
~¡Todos los días estoy barajando!~ |
|
|
| Report Abuse |
|
|
| |
|
|
| 15 Oct 2011 02:14 PM |
^Who are you? And why do you troll?
~¡Todos los días estoy barajando!~ |
|
|
| Report Abuse |
|
|
|
| 15 Oct 2011 02:15 PM |
@nick
i'm trolling because i posted 'lolwut'
lolwut |
|
|
| Report Abuse |
|
|