static void Main(string[] args)
{
string str_val = "8584348,894";
//int prefix = Convert.ToInt32(str_val[0]); //prefix = 56 O_o
//int prefix = (int)str_val[0]; //what, again 56? i need 8!
int prefix = Convert.ToInt32("8"); //at least this works -_-
}
Any idea how to convert first symbol to right numeric value?
If you use:
Convert.ToInt32(str_val[0]);
then you are actually calling the overload:
Convert.ToInt32(char val);
which gives the Unicode/Ascii number of character being passed as a parameter.
If you want to convert first character, you need to force it to be a string type:
Convert.ToInt32(str_val.Substring(0, 1));
This way you call the overload:
Convert.ToInt32(string val);
which actually do what you want (convert string value to int value that this string represents).
Your trying to parse a string but passing in a char. Convert the character to a string first.
int prefix = Convert.ToInt32(str_val[0].ToString());
So the character value of 8 is the ASCII value 56, what you want to do is inteprete the value as a string rather than an ASCII Value. By using the .ToString() method you are converting the character into a null terminated string, which can be read by the ToInt32 method.
By doing this way Convert.ToInt32(str_val[0]) you are reading the character at a index of the string that converts the char to int. the int is the ascii for it
Like Bridge mentioned, it's getting the ASCII value. When you use the indexer on the string class to get just a character, it returns it as a char. If you read the char documentation you'll see that it is internally stored as a numeric UTF-16 value. It also has implicit conversions to and from most numeric types, which extract the UTF-16 value, or convert the numeric form into the character form. That's what you're doing.
What you mean to do is parse it as an int, not get the numeric representation of the UTF-16 value. That's where the Convert answers all come in.
Int32.Parse(str_val[0])
Will give you number in string.
Related
I want to convert string number like "000023" to integer 000023 in RDLC report but when I am trying to convert it into integer it only displays 23 instead of 000023.
does anyone know how to do this ?
thanks
000023 is not an valid integer. When 000023 is converted to an integer, it will automatically be converted to the valid integer 23.
Jaq316's answer is correct.
How could you have an int as 000023? You could format it as a string with leading zeros which you have already it as a string.
But if you want to display 23 as a 000023 you can use String.PadLeft like;
public string Represent(string s)
{
int i = 0;
if (int.TryParse(s, out i))
{
return(i.ToString().PadLeft(6,'0'));
}
}
This method seems pointless of course since you have already 000023 in your database.
Integral types and Floating point types doesn't owns any formatting.
Formatting is done only when converting them to string. so you've to format it when you display it. There is no way to store an integer as 000023
i am using convert.tobyte to convert string to byte. the problem is if the data is:
string data = "5";
byte b = Convert.tobyte(data); works fine.
but, if
string data = "S"
byte b = Convert.tobyte(data); DOESN'T WORK!
ERROR : Input string was not in a correct format
What is wrong and how to solve it?
Note: i am extracting a values from textbox, so the conversion works only if the input is number digits, not characters.
how to include the characters?
Thanks.
This is exactly how Convert.ToByte method works http://msdn.microsoft.com/en-us/library/y57wwkzk.aspx
Only digits in string accepted.
Did you meant converting the string to byte array? If so, use:
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(yourString);
For strings containing only ASCII characters, the size of array will be equal to length of your string and every byte in array will be an ord value for the character. If string contains multibyte characters the size of array will be greater than length of string.
When you are not sure if a variable of string type could be correctly converted to a number you need to use the TryParse family of methods like Byte.TryParse method
string data = "S";
byte b;
if(byte.TryParse(data, out b))
Console.Writeline("Worked: " + b.ToString());
The TryParse has the advantage to not throw an exception if the string cannot be converted to a number and return just false or true while the out parameter is filled with the converted value if possible.
I want to convert a thousand separated value to integer but am getting one exception.
double d = Convert.ToDouble("100,100,100");
is working fine and getting d=100100100
int n = Convert.ToInt32("100,100,100");
is getting one format exception
Input string was not in a correct format
Why?
try this:
int i = Int32.Parse("100,100,100", NumberStyles.AllowThousands);
Note that the Parse method will throw an exception on an invalid string, so you might also want to check out the TryParse method as well:
string s = ...;
int i;
if (Int32.TryParse(s, NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out i))
{
// if you are here, you were able to parse the string
}
What Convert.ToInt32 is actually calling in your example is Int32.Parse.
The Int32.parse(string) method only allows three types of input: white space, a sign, and digits. In the following configuration [ws][sign]digits[ws] (in brackets are optional).
Since your's contained commas, it threw an exception.
Because you're supposed to specify a string containing a plain integer number (maybe preceded by +/- sign), with no thousands separator. You have to replace the separator befor passing the string to the ToInt32 routine.
You can't have separators, just numbers 0 thru 9, and an optional sign.
http://msdn.microsoft.com/en-us/library/sf1aw27b.aspx
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
c# convert char to int
This works:
int val = Convert.ToInt32("1");
But this doesn't:
int val = Convert.ToInt32('1'); // returns 49
Shouldn't this convert to actual int?
It is returning the ASCII value of character 1
The first statement treats the argument as string and converts the value into Int,
The second one treats the argument as char and returns its ascii value
As the others said, Convert returns the ASCII code.
If you want to convert '1' to 1 (int) you should use
int val = Convert.ToInt32('1'.ToString());
The code '1' is the same as (char)49 (because the Unicode code point of the character 1 is 49). And Convert.ToInt32(char) returns the code point of that character as an int.
As others have already pointed out: In your second example ('1') you are using a char literal. A char is a numeric type. There is no parsing done as in the string example ("1"), since it already is a number - just a cast to a wider number format (from 16 bits to 32 bits).
It treats '1' as char and int form of any char is its ASCII equivalent so it return its ASCII equivalent.
But in case of "1" it treats it as string and convert it to integer.
I'm new to C#.
I know in vb.net, i can do this:
Dim guid as string = System.Guid.NewGuid.ToString
In C#, I'm trying to do
String guid = System.Guid.NewGuid().ToString;
but i get an "Cannot convert method group 'ToString' to non-delegate type 'string'. Did you intend to invoke the method?" error.
According to MSDN the method Guid.ToString(string format) returns a string representation of the value of this Guid instance, according to the provided format specifier.
Examples:
guidVal.ToString() or guidVal.ToString("D") returns 32 hex digits
separated by hyphens: 00000000-0000-0000-0000-000000000000
guidVal.ToString("N") returns 32 hex digits:00000000000000000000000000000000
guidVal.ToString("B") returns 32 hex digits separated by hyphens, enclosed in braces:{00000000-0000-0000-0000-000000000000}
guidVal.ToString("P") returns 32 hex digits separated by hyphens, enclosed in parentheses: (00000000-0000-0000-0000-000000000000)
You're missing the () after ToString that marks it as a function call vs. a function reference (the kind you pass to delegates), which incidentally is why c# has no AddressOf operator, it's implied by how you type it.
Try this:
string guid = System.Guid.NewGuid().ToString();
Here are examples of output from each of the format specifiers:
N: cd26ccf675d64521884f1693c62ed303
D: cd26ccf6-75d6-4521-884f-1693c62ed303
B: {cd26ccf6-75d6-4521-884f-1693c62ed303}
P: (cd26ccf6-75d6-4521-884f-1693c62ed303)
X: {0xcd26ccf6,0x75d6,0x4521,{0x88,0x4f,0x16,0x93,0xc6,0x2e,0xd3,0x03}}
The default is D.
Run this yourself.
In Visual Basic, you can call a parameterless method without the braces (()). In C#, they're mandatory. So you should write:
String guid = System.Guid.NewGuid().ToString();
Without the braces, you're assigning the method itself (instead of its result) to the variable guid, and obviously the method cannot be converted to a String, hence the error.
Did you write
String guid = System.Guid.NewGuid().ToString;
or
String guid = System.Guid.NewGuid().ToString();
notice the parenthesis.
You need
String guid = System.Guid.NewGuid().ToString();
String guid = System.Guid.NewGuid().ToString();
Otherwise it's a delegate.
you are missing () on the end of ToString.
Guid guidId = Guid.Parse("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
string guidValue = guidId.ToString("D"); //return with hyphens
Following Sonar rules, you should whenever you can try to protect yourself, and use
System.globalisation whenever it's possible like for DateTime.ToString().
So regarding the other answers you could use:
guid.ToString("", CultureInfo.InvariantCulture)
where "" can be replaces by : N, D, B , P and X for more infos see this comment.
Example here