C
C#2y ago
احمد

❔ ✅ 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
احمد
احمدOP2y ago
what am i doing wrong im tired
Jimmacle
Jimmacle2y 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 User2y ago
Message Not Public
Sign In & Join Server To View
احمد
احمدOP2y ago
no just a personal project idk how to convert to binary that's why
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
احمد
احمدOP2y ago
i don't think so
Jimmacle
Jimmacle2y ago
as of .NET 8 it looks like there's a string format specifier for binary 1234.ToString("B")
احمد
احمدOP2y ago
meaning?
Jimmacle
Jimmacle2y ago
meaning if you use .NET 8 you can just do that
احمد
احمدOP2y ago
ToString meaning add B to the beginning of the string
Jimmacle
Jimmacle2y ago
no meaning "convert 1234 to binary"
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2y 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
MODiX2y 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 User2y ago
Message Not Public
Sign In & Join Server To View
احمد
احمدOP2y ago
WTH 💀
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Jimmacle
Jimmacle2y ago
i try to forget Convert exists
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
احمد
احمدOP2y ago
int value = 12353429; b_value = Convert.ToString(value, 2);
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
احمد
احمدOP2y ago
? mind showing the hex thing
Jimmacle
Jimmacle2y ago
you mean the base64 conversions?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
احمد
احمدOP2y ago
mind correcting as in 16 = 10 F0
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
احمد
احمدOP2y ago
what did i do wrong here tho what's 2?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
احمد
احمدOP2y ago
ah
MODiX
MODiX2y 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
MODiX2y 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 User2y ago
Message Not Public
Sign In & Join Server To View
احمد
احمدOP2y ago
thank you 🙂 !close
Accord
Accord2y 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.

Did you find this page helpful?