generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Binary to Decimal and Decimal to Binary Conversion Algorithms

Previous Thread :: Next Thread 
nickmaster24 is not online. nickmaster24
Joined: 04 Oct 2008
Total Posts: 8906
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
crazypotato4 is not online. crazypotato4
Joined: 22 Feb 2010
Total Posts: 20000
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 is not online. 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
crazypotato4 is not online. crazypotato4
Joined: 22 Feb 2010
Total Posts: 20000
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
crazypotato4 is not online. crazypotato4
Joined: 22 Feb 2010
Total Posts: 20000
14 Oct 2011 08:30 PM
D:

GoldenUrg is a meanie.
Report Abuse
aboy5643a is not online. 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 is not online. 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
crazypotato4 is not online. crazypotato4
Joined: 22 Feb 2010
Total Posts: 20000
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 is not online. 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
nickmaster24 is not online. nickmaster24
Joined: 04 Oct 2008
Total Posts: 8906
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 is not online. 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
nickmaster24 is not online. nickmaster24
Joined: 04 Oct 2008
Total Posts: 8906
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 is not online. 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
nickmaster24 is not online. nickmaster24
Joined: 04 Oct 2008
Total Posts: 8906
14 Oct 2011 08:40 PM
Oh...

~¡Todos los días estoy barajando!~
Report Abuse
nickmaster24 is not online. nickmaster24
Joined: 04 Oct 2008
Total Posts: 8906
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
nickmaster24 is not online. nickmaster24
Joined: 04 Oct 2008
Total Posts: 8906
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 is not online. 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 is not online. 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
nickmaster24 is not online. nickmaster24
Joined: 04 Oct 2008
Total Posts: 8906
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 is not online. 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
nickmaster24 is not online. nickmaster24
Joined: 04 Oct 2008
Total Posts: 8906
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
nickmaster24 is not online. nickmaster24
Joined: 04 Oct 2008
Total Posts: 8906
15 Oct 2011 01:58 PM
Yes, I have:
#_include <_stdlib.h_>

(without underscores)

~¡Todos los días estoy barajando!~
Report Abuse
babyzander44 is not online. babyzander44
Joined: 14 Aug 2011
Total Posts: 756
15 Oct 2011 02:11 PM
lolwut
Report Abuse
nickmaster24 is not online. nickmaster24
Joined: 04 Oct 2008
Total Posts: 8906
15 Oct 2011 02:14 PM
^Who are you? And why do you troll?

~¡Todos los días estoy barajando!~
Report Abuse
babyzander44 is not online. babyzander44
Joined: 14 Aug 2011
Total Posts: 756
15 Oct 2011 02:15 PM
@nick

i'm trolling because i posted 'lolwut'

lolwut
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image