C
C#13mo ago
Ahmed

❔ ✅ denary -> binary

using System.Data;

static void Main(string[] args)
{
string ans = AddBinary(1, 2);
Console.WriteLine(ans);
Console.ReadLine();
}
static string AddBinary(int a, int b)
{
string string_combiner = "";
int number = a + b;

for (int i = 1; i <= 32; i++)
{

int powered_number = (int)Math.Pow(number, i);
if (number % powered_number == 0)
{
string_combiner.Insert(0, "1");
}


else
{
string_combiner.Insert(0, "0");
}

}

return string_combiner;

}
using System.Data;

static void Main(string[] args)
{
string ans = AddBinary(1, 2);
Console.WriteLine(ans);
Console.ReadLine();
}
static string AddBinary(int a, int b)
{
string string_combiner = "";
int number = a + b;

for (int i = 1; i <= 32; i++)
{

int powered_number = (int)Math.Pow(number, i);
if (number % powered_number == 0)
{
string_combiner.Insert(0, "1");
}


else
{
string_combiner.Insert(0, "0");
}

}

return string_combiner;

}
35 Replies
Ahmed
AhmedOP13mo ago
what am i doing wrong im tired
Jimmacle
Jimmacle13mo ago
why do you have one method responsible for both adding numbers and converting them to binary? and is this specifically something you have to write yourself or do you just need it formatted in binary?
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
Ahmed
AhmedOP13mo ago
no just a personal project idk how to convert to binary that's why
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
Ahmed
AhmedOP13mo ago
i don't think so
Jimmacle
Jimmacle13mo ago
as of .NET 8 it looks like there's a string format specifier for binary 1234.ToString("B")
Ahmed
AhmedOP13mo ago
meaning?
Jimmacle
Jimmacle13mo ago
meaning if you use .NET 8 you can just do that
Ahmed
AhmedOP13mo ago
ToString meaning add B to the beginning of the string
Jimmacle
Jimmacle13mo ago
no meaning "convert 1234 to binary"
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX13mo ago
TeBeCo
REPL Result: Success
int value = 12353429;
Convert.ToString(value, 2)
int value = 12353429;
Convert.ToString(value, 2)
Result: string
101111000111111110010101
101111000111111110010101
Compile: 492.113ms | Execution: 23.424ms | React with ❌ to remove this embed.
MODiX
MODiX13mo ago
TeBeCo
isn't there method in dotnet to change a number fom a base to another ?
Quoted by
<@689473681302224947> from #denary -> binary (click here)
React with ❌ to remove this embed.
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
Ahmed
AhmedOP13mo ago
WTH 💀
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
Jimmacle
Jimmacle13mo ago
i try to forget Convert exists
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
Ahmed
AhmedOP13mo ago
int value = 12353429; b_value = Convert.ToString(value, 2);
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
Ahmed
AhmedOP13mo ago
? mind showing the hex thing
Jimmacle
Jimmacle13mo ago
you mean the base64 conversions?
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
Ahmed
AhmedOP13mo ago
mind correcting as in 16 = 10 F0
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
Ahmed
AhmedOP13mo ago
what did i do wrong here tho what's 2?
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
Ahmed
AhmedOP13mo ago
ah
MODiX
MODiX13mo ago
TeBeCo
REPL Result: Success
byte[] array = { 0x64, 0x6f, 0x74, 0x63, 0x65, 0x74 };

string hexValue = Convert.ToHexString(array);
Console.WriteLine(hexValue);

/*Output:
646F74636574
*/
byte[] array = { 0x64, 0x6f, 0x74, 0x63, 0x65, 0x74 };

string hexValue = Convert.ToHexString(array);
Console.WriteLine(hexValue);

/*Output:
646F74636574
*/
Console Output
646F74636574
646F74636574
Compile: 627.605ms | Execution: 94.631ms | React with ❌ to remove this embed.
MODiX
MODiX13mo ago
TeBeCo
REPL Result: Success
string hexString = "8E2";
int num = Int32.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
Console.WriteLine(num);
//Output: 2274
string hexString = "8E2";
int num = Int32.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
Console.WriteLine(num);
//Output: 2274
Console Output
2274
2274
Compile: 563.441ms | Execution: 32.679ms | React with ❌ to remove this embed.
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
Ahmed
AhmedOP13mo ago
thank you 🙂 !close
Accord
Accord13mo ago
Closed! Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server