C
C#14mo ago
Pinapleu

❔ Converting from binary to integer

Recently I had to problem where I would convert an integer to binary and count the ones, the problem itself didn't have any issues, however, I noticed that of I converted binary 1 to an integer, it would become 49 and 0 be camera 48. This is the code: static void Main(string[] args) { int n = Convert.ToInt32(Console.ReadLine()); string str = Convert.ToString(n, 2); char[] bin = str.ToCharArray(); int sum = 0; for(int i=0;i<bin.Length;i++) { if(bin[i]-48==1) { sum++; }
} Console.WriteLine(sum); } When doing Console.WriteLine(bin[i]) it outputs 1s and 0s but when comparing with 1 it's always different, then I noticed the 48-49 thing so I removed 48 from every number, making the code work, my question is, why does it become 48-49? Is it related to it being a char array?
16 Replies
MODiX
MODiX14mo ago
Pobiega#2671
REPL Result: Success
(int)'1'
(int)'1'
Result: int
49
49
Compile: 315.382ms | Execution: 72.521ms | React with ❌ to remove this embed.
Pobiega
Pobiega14mo ago
as we can see, the numerical value of '1' is actually... 49.
Pinapleu
Pinapleu14mo ago
Confusing, but, thanks
MODiX
MODiX14mo ago
Pobiega#2671
REPL Result: Success
(char)1
(char)1
Result: char


Compile: 381.175ms | Execution: 23.567ms | React with ❌ to remove this embed.
Pobiega
Pobiega14mo ago
😄
Pinapleu
Pinapleu14mo ago
So it's because it's a char not an integer? Just like how letters have numbers assigned?
Pobiega
Pobiega14mo ago
yes
Pinapleu
Pinapleu14mo ago
Alright, thanks
Tvde1
Tvde114mo ago
A useful trick is to subtract '0'
MODiX
MODiX14mo ago
Tvde1#0587
REPL Result: Success
'5' - '0'
'5' - '0'
Result: int
5
5
Compile: 313.896ms | Execution: 23.718ms | React with ❌ to remove this embed.
ffmpeg -i me -f null -
this is not binary 😐
TheRanger
TheRanger14mo ago
ASCII Table - ASCII Character Codes, HTML, Octal, Hex, Decimal
Ascii character table - What is ascii - Complete tables including hex, octal, html, decimal conversions
TheRanger
TheRanger14mo ago
as u can see, character '1' represents 49 in ascii subtract like that to get the actual integer
MODiX
MODiX14mo ago
TheRanger#3357
REPL Result: Success
(int)Char.GetNumericValue('1')
(int)Char.GetNumericValue('1')
Result: int
1
1
Compile: 455.436ms | Execution: 25.022ms | React with ❌ to remove this embed.
TheRanger
TheRanger14mo ago
also works
Accord
Accord14mo ago
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
More Posts
❔ Better way to open a "window" to take in text input?As in, when you click a button, a small window appears which exists only to take in the for example ❔ Can someone help me use data from database tables?I have 2 $ and a dream ! Tommorow im going to the national stage of the c# olympiad and i dont even ❔ List string variablesHello, I'm really new to C# and would like to get help. How is the correct way to give variable for❔ EF successfully updated database, but unable to connect to sql server when getting data.Hey guys, getting back into .net after a while. sql server is currently on docker and I am able to c❔ ✅ Writing to response body is slow in .net core 7 web api?Hey guys, I have a stack overflow thread about the issue, but basically, I'm getting significant per❔ update-package : Failed to add reference to 'xyz'. Error HRESULT E_FAIL has been returnedSo I don't really know what happened. Turned my pc on, opened a project I created 2 days ago. My pac❔ help pleaseI’m try to make a 2d racing game in visual studio 2022 with C# but I’m have trouble figuring out how❔ Tailwindcss and . net7 proper installation using vs 2022 previewSo I got tailwind css working just not working properly. I cannot nest classes together like in vs cTrying to write to XAML file with C# (more details below)This might be too vague, and you're free to ask more questions about what I'm trying to get across, ❔ Learning C#, and curious about advanced return statements...I'm jumping right into the depts and wish to return a new object of 'Buffer' type and construct it w