I have an enum
public enum BookType
{
Old = 'O',
New = 'N',
All = 'B'
}
What I need to do is get the value of the char in the enum. For example if the enum is set to:
BookType bt = BookType.New
I need to get the value of new "N"
string val = (???)bt;
I need val = N
What is the best way to do this? If it was an int easy, just cast to int.
Thanks.
The values associated with your enum are still ints, you've just set using a character literal. If you want to recover this value as a string, you can cast the enum value to a char and then convert that to a string:
string val = ((char)bt).ToString();
You can just cast to char.
After casting to char, you'll need to call ToString() to convert the char to a string:
string val = ((char)bt).ToString();
Related
I want to convert the following string to an array of an integral number type.
string str = "-9007199254740985";
I tried str.ToArray().ToString(); but did not get the right conversion.
Below code will convert the string to character array.
string input = "welcome";
char[] chars = input.ToCharArray();
If you want to convert the string to an array, you can do it like this.
var array = new string[] { str };
However, if you want an array of long, you have to convert your string first.
var number = long.Parse(str);
Then you can add it to an array.
var array = new long[] { number };
Note that you cannot use an int here, because your number exceeds its range.
I am using ASP.NET Web Pages to create a project in which I am assigned to place each character of the user's (customer's) name in each seperate input block.
For that I am getting the value from the Sql Server CE Database and then I am trying to convert it into an array of one character for each input. I have tried the following code for that
var form_data = db.QuerySingle("SELECT * FROM Form_Data WHERE Form_Id =#0", form_id);
var name_emp = form_data.Name_Emig;
if(name_emp != null) {
name_emp = name_emp.ToString().Split('');
}
It generates the following Compiler error:
http://msdn.microsoft.com/en-us/library/8eb78ww7(v=vs.90).aspx (Compiler error: CS1011)
Which means that the character cannot be initialized by an empty value. I want to convert that value in one character each array.
The name is as: Afzaal Ahmad Zeeshan. So, in each input it would be placed inside the value of it. But the code I am using isn't working. Any suggestion to split the string at each character, so the result would be
{'A', 'F', 'Z', 'A', 'A', 'L' ...}
It doesn't matter whether result is Capital case or lower case. I will convert it to the desired one later.
You can try using ToCharArray().
So your code would be this:
var form_data = db.QuerySingle("SELECT * FROM Form_Data WHERE Form_Id =#0", form_id);
var name_emp = form_data.Name_Emig;
if(name_emp != null) {
name_emp = name_emp.ToCharArray();
}
You can use this to iterate with each item :
string yourString = "test";
foreach(var character in yourString)
{
// do something with each character.
}
Or this to get all characters in a char[]
char[] characters = yourstring.ToCharArray();
Try name_emp.ToArray(). A string implements IEnumerable<char>, so ToArray will return an array of the char.
Edit:
Actually I suppose ToCharArray is better in this case...
I need to write a hexadecimal value into a string.
I'm using this:
int value;
long item = Convert.ToInt64(value); // this convert value to a hexadecimal
So I need to write this hexadecimal value into a string...
so, when I use ToString(), it's convert again into integer, I need to write hexadecimal value.
Not sure if I get your question, but you may want to try this:
int value;
string s = value.ToString("X"); // int to hexadecimal string
If you want to reparse it back to a long:
ulong ul = ulong.Parse(s, System.Globalization.NumberStyles.HexNumber); // hexadecimal string to unsigned int
int myInt = 1243;
string myHex = myInt.ToString("X"); // gives you hex
int myNewInt = Convert.ToInt32(myHex, 16); // back to int again.
How do you convert an integer to a string? It works the other way around but not this way.
string message
Int64 message2;
message2 = (Int64)message[0];
If the message is "hello", the output is 104 as a number;
If I do
string message3 = (string)message2;
I get an error saying that you cant convert a long to a string. Why is this. The method .ToString() does not work because it converts just the number to a string so it will still show as "104". Same with Convert.ToString(). How do I make it say "hello" again from 104? In C++ it allows you to cast such methods but not in C#
message[0] gives first letter from string as char, so you're casting char to long, not string to long.
Try casting it back to char again and then concatenate all chars to get entire string.
ToString() is working exactly correctly. Your error is in the conversion to integer.
How exactly do you expect to store a string composed of non-numeric digits in a long? You might be interested in BitConverter, if you want to treat numbers as byte arrays.
If you want to convert a numeric ASCII code to a string, try
((char)value).ToString()
Another alternative approach is using ASCII.GetBytes method as below
string msg1 ="hello";
byte[] ba = System.Text.Encoding.ASCII.GetBytes(msg1);
//ba[0] = 104
//ba[1] = 101
//ba[2] = 108
//ba[3] = 108
//ba[4] = 111
string msg2 =System.Text.Encoding.ASCII.GetString(ba);
//msg2 = "hello"
Try this method:
string message3 = char.ConvertFromUtf32(message2);
104 is the value of "h" not "hello".
There is no integer representation of a string, only of a char. Therefore, as stated by others, 104 is not the value of "hello" (a string) but of 'h' (a char) (see the ASCII chart here).
I can't entirely think of why you'd want to convert a string to an int-array and then back into a string, but the way to do it would be to run through the string and get the int-value of each character and then reconvert the int-values into char-values and concatenate each of them. So something like
string str = "hello"
List<int> N = new List<int>();
//this creates the list of int-values
for(int i=0;i<str.Count;i++)
N.Add((int)str[i]);
//and this joins it all back into a string
string newString = "";
for(int i=0;i<str.Count;i++)
newString += (char)N[i];
Is there a function to append padding digits to the left of a digit.I want to form a 3 digit number, so when the value is '3' i need to make it '003'
e.g in php we have
$m_ccode=str_pad($m_casecode,3,"0",str_pad_left);
same i want to convert in asp.net c#. How can i do it, i have numeric value stored in string 's'
String s = DropDownList3.SelectedItem.Value;
string variant1 = "3".PadLeft(3, '0'); // if you have a string
string variant2 = 3.ToString("000"); // if you have a number
In your case you need to unbox your int if the numeric value is an int
String s = ((int)DropDownList3.SelectedItem.Value).ToString("000");
Try the following:
String s = DropDownList3.SelectedItem.Value.PadLeft(3, '0');