c# console program divides 6 by 2 and gets 27 [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
something went wrong in c# while coding a project, it divides and multiplies all different datatypes and results 27 or 26 or 108 , how to resolve ?!

Console.Read() reads a character from the keyboard, and lets you store it where you want. You typed a 6, which may look like a number to you, but to the computer is merely another character, like A, ! or &.
The character '6' has the (ASCII) code 54. Divide that by 2 and you get 27...

Problem is, code using Console.Read to read an integer, but Console.Read reads next character from input stream (Will not wait for enter). Moment when type 67, first character(6) will be converted to int and assigned to variable a immediately, so a gets 54 (ASCII value).
Which will get divided by 2 results 27
To fix your problem, use Console.ReadLine, which reads the line of characters.
int a = int.Parse(Console.ReadLine()); // or use `int.TryParse`

Related

If integer look like this `010` is first 0 consider integer? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
There was misunderstanding about this int number = 010, What I am saying is
first 0 is not integer due to c# has no leading zero so 010 will be 10
however one of stackoverflow user saying
first 0 in 010 is integer
so could anyone help to explain in details why first 0 in 010 is integer even though it has no value or it doesn't represent any mathematical integer !
thanks in advance
When you are writing integer literals, leading zeros don't mean anything.
var a = 10;
var b = 010;
a == b // true
It's not that the "first 0 is not [an] integer", it's that the leading 0 is ignored, because it doesn't contribute any information to the value of the number.
The same is with binary notation - leading zeros do not increase the information of the number (except for maybe the storage size of the value, but that's meta-information).
If you're dealing with strings, that's a whole different ballgame, as "010" has a different character array than "10", even if they parse to the same integer value.
var c = "010"
var d = "10"
c == d // false
int.Parse(c) == int.Parse(d) // true
Ok so obviously 0 is an integer and you can do the math on a 0. 0*1 = 0 for example. When looking at what your computer sees from an integer standpoint you'll notice that 010 and 10 both have the same binary representation. So that begs the question, why is 0 being ignored? Remember that computers understand instructions, one... at... a... time. Meaning that when it first reads through that integer, even with math, it starts at the first character and goes through. The only other difference is strings or other data types that may make better use of that 0.
Binary Representation of 10
1010
Binary Representation of 010
1010
Now since we recognize that computers read one instruction at a time, if it is given a 0 (Which has a binary representation of 0) what happens when that instruction has any math completed on it? Nothing is what happens. 0*100=0 or 0/2=0 the difference being addition or subtraction which takes your integer into positive or negative value. 0+100=100 now if you look at the instructions it again starts with the 0 and then it's cleaned out leaving you with the binary representation of whatever had some calculation.

How to append a string in C# dynamically? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Thank you in Advance:
I have a parameter which gives the row_count after loading the data.
i need to insert that value into a file, which i am able to do it.
But I need the format in this manner:
if the row_count is 150 then
I am getting only Param name='INPUT_NUM_RECS value=150/
But I need the output as
Param name='INPUT_NUM_RECS value=000000150/
Thank you.
Assuming that 150 is an int variable name num.
int num = 15;
string output = $"Param name='INPUT_NUM_RECS value={num:D9}"
Here using D9 we will place the int value with leading zeroes if the number of digits in num is less than 9.
Number of leading zeroes = 9 - digits in num
If number of digits in num is more than 9, num will not be truncated

How do I get two numbers between two words (C#) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a string "Building1Floor2" and it's always in that format, how do I cleanly get the building number (e.g. 1) and floor number. I'm thinking I need a regex, but not entirely sure that's the best way. I could just use the index if the format stays the same, but if I have have a high floor number e.g. 100 it will break.
P.S. I'm using C#.
Use a regex like this:
Building(\d+)Floor(\d+)
Regex would be an ok option here if "Building" and "Floor" could change. e.g.: "Floor1Room23"
You could use "[A-Za-z]+([0-9]{1,})[A-Za-z]+([0-9]{1,})"
With those groupings, $1 would now be the Building number, and $2 would be Floor.
If "Building" and "Floor" never changed, however, then regex might be overkill.. you could use a string split
Find the index of the "F" and substring on that.
int first = str.IndexOf("F") ;
String building = str.substring(1, first);

Splitting a 200 bit hexadecimal bitmask [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a bitmask of 200 bit stored as a hexadecimal value.
In order to apply the & bit operator, I have to first convert the hex to an integer but 200 bit is too big for a uint64 so my question is : how do I split my bitmask in 4 different hexadecimal value without loosing data?
So that I can also split my 200 bit data and then compare every chunk of data with the corresponding chunk of bitmask without altering the result.
You can use the BigInteger from System.Numerics (it's a separate assembly):
BigInteger bi = BigInteger.Parse("01ABC000000000000000000000000000000000", System.Globalization.NumberStyles.HexNumber);
VERY IMPORTANT: prepend a "0" before the hex number! (because BigInteger.Parse("F", NumberStyles.HexNumber) == -1, while BigInteger.Parse("0F", NumberStyles.HexNumber) == 15
BigInteger implement the "classical" logical operators (&, |, ^)
Requires .NET 4.0
The most efficient way of achieving this is writing a class that can store and do binary operations on 200bits of data, have strings as input, etc.

How do I make a string only X char and specific? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I don't know if you got me what I'm trying to explain... (English is not my main language).
Ok here, I got this string 590CBC145FA and this one F6EC5CA9A and the only part that I want, no matter how long is the string, is the last eight char like this: CBC145FA 6EC5CA9A. The 590 and F are obsolete to me, like: !S!ZV)+D_?CEFZEZAF = CEFZ!Z#F.
I tried to use serial.Substring(3); only work for the first one: 590CBC145FA = CBC145FA
If I try to use serial.Substring(1); it work for F6EC5CA9A = 6EC5CA9A
But what happen if I get a string very long and I don't know how much the obsolete part is... I can't use serial.Substring(X); because I don't, like I said, how long is the obsolete part.
I only want to get the eight last char of any serial no matter how long is.
You want to get the eight last char of any serial no matter how long is.
string test = "F6EC5CA9A";
string result = test;
if (test.Length >= 8)
result = test.Substring(test.Length-8);
You just need to discover the index of the 8th character before the end of the string. And this could be easily calculated using the Length property of every string.
Of course you need to be sure that your string is at least 8 characters. You don't say what do you want in case this length is less than 8, so I assume that you want the original input back

Categories