I am just beginning to learn C#. I am reading a book and one of the examples is this:
using System;
public class Example
{
public static void Main()
{
string myInput;
int myInt;
Console.Write("Please enter a number: ");
myInput = Console.ReadLine();
myInt = Int32.Parse(myInput);
Console.WriteLine(myInt);
Console.ReadLine();
}
}
When i run that and enter say 'five' and hit return, i get 'input string not in correct format' error. The thing i don't understand is, i converted the string myInput to a number didn't i? Microsoft says that In32.Parse 'Converts the string representation of a number to its 32-bit signed integer equivalent.' So how come it doesn't work when i type the word five? It should be converted to an integer shouldn't it... confused. Thanks for advice.
'five' is not a number. It's a 4-character string with no digits in it. What parse32 is looking for is a STRING that contains numeric digit characters. You have to feed it "5" instead.
The string representation that Int32.Parse expects is a sequence of decimal digits (base 10), such as "2011". It doesn't accept natural language.
What is does is essentially this:
return 1000 * ('2' - '0')
+ 100 * ('0' - '0')
+ 10 * ('1' - '0')
+ 1 * ('1' - '0');
You can customize Int32.Parse slightly by passing different NumberStyles. For example, NumberStyles.AllowLeadingWhite allows leading white-space in the input string: " 2011".
The words representing a number aren't converted; it converts the characters that represent numbers into actual numbers.
"5" in a string is stored in memory as the ASCII (or unicode) character representation of a 5. The ASCII for a 5 is 0x35 (hex) or 53 (decimal). An integer with the value '5' is stored in memory as an actual 5, i.e. 0101 binary.
Related
i had a function containing code like this:
Random x = new Random();
int key = x.Next(0x21, 0x7B);
string nxt = Convert.ToString(key.ToString("X")) +
Convert.ToString(key.ToString("X"))
+ Convert.ToString(key.ToString("X"))
+ Convert.ToString(key.ToString("X"));
Im very new to C#, help me, thank a lot
That code is choosing a random number between 0x21 and 0x7B, converting it to a string in hex and repeating it four times in nxt.
There are better ways to create a string with a single character multiple times (for example, the string constructor that accepts a character and a count), and the Convert.ToString call is useless since int.ToString already returns a string.
The first two lines pick a random integer between 33 and 122 inclusive (the 0xs in .Next() mean those numbers are expressed in hexadecimal notation).
The key.ToString("X") part takes that random integer, converts it to hexadecimal notation, and returns it as a string.
As Blindy pointed out, the Convert.ToString() is redundant and not necessary since it is converting a string to a string.
The final "nxt" variable would consist of that new hexadecimal number (as a string) repeated four times.
Here's a couple of other ways to get that string repeated four times:
string nxt = new StringBuilder().Insert(0, key.ToString("X"), 4).ToString();
string nxt = String.Concat(Enumerable.Repeat(key, 4));
string nxt = $"{key}{key}{key}{key}";
How can I get the unicode values (from the code column) if I have the string?
For example, for passing the empty space " " I would like to get the value U+0020.
I found this approach:
byte[] asciiBytes1 = Encoding.ASCII.GetBytes(" ");
But this returns me the value from the decimal column.
If value is your decimal value:
string code = $"U+{value.ToString ("X4")}";
will give you what you want.
(X means hex, 4 means pad to 4 digits)
I've tried converting the double value into a string and using the Replace() method
to replace the ',' to '.'.
This works well but only when the trailing digits are not zero, I need zeros in my string, even if the value is 1234.0. This worked well for the decimal values. I have tried to convert the double to decimal but I lose the decimal digits if there are zeros.
I know I'm missing something. I would be grateful for some suggestions.
This would depend on the language. An example in C#
d.ToString("0.00");
Would produce a double with 2 decimal places nomatter the values (zero or otherwise).
If this is in Java, check out the NumberFormat class's setMinimumFractionDigits() method.
Example:
double d1 = 2.5;
double d2 = 5.0;
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(2);
String d1s = nf.format(d1);
String d2s = nf.format(d2);
System.out.println("d1s: " + d1s + " and d2s: " + d2s);
produces
d1s: 2.50 and d2s: 5.00
...and in Fortran, you could do something like: :-)
write(*,110) x
110 format (F5.3)
(guess we really have to know what language is being used...)
I'm working with RFID Reader, and it became with a software demo that has some different types of reading a rfid tag, like:
Hexadecimal,Decimal,Ascii, Abatrack etc...
The Abatrack documentation says:
Shows the CardID converted to decimal with 14 digits.
I have a CardID = 01048CABFB then with this protocol it shows me 00004371295227
where the first four zeroes were added by the software
It converts a string with letters and numbers to decimal with only numbers. how may I do that ?
I've found THIS , but it's in VB.
To convert from hexadecimal to decimal, you can do this:
string hexString = "01048CABFB";
long intVal = Int64.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
// intVal = 4371295227
You can also use Convert.ToInt64() which allows you to specify base 16 (hexadecimal):
string hexFromRFID = "01048CABFB";
Int64 decFromRFID = Convert.ToInt64(hexFromRFID, 16);
Console.WriteLine("Hex: " + hexFromRFID + " = Dec: " + decFromRFID);
I have a situation where I need to prefix a zero to an integer.
Initially I have string which has 12 chars, first 7 are alphabets and 5 are numeric values.
The generated string some times have a zero at starting position of numeric values. for example ABCDEF*0*1234, and my scenario is to generate a range of strings from the generated string. Suppose I want to generate a range (assume 3 in number), so it would be ABCDEF01235, ABCDEF01236, ABCDEF01237.
When I try to convert a string which has a 0 (as shown above) to int, it returns only 1234.
Is there any way to do this, without truncating zero?
You can use PadLeft to expand a given string to a given total length:
int num = 1234;
Console.WriteLine(num.ToString().PadLeft(5, '0')); // "01234"
int num = 1234;
Console.WriteLine(num.ToString("D5")); // "01234"
No with int.
You have to use string to concatenate the parsed number and the 0
int num = 1234;
Console.WriteLine($"{num:d5}");
I think you can use string.Format
int num = 1234;
Console.WriteLine(string.Format("0{0}", num));