Integer to hexadecimal conversion - c#

I have n XML file which has a hexadecimal variable
<settings>
<add key "var1" value "0x0FFFFFFF">
</settings>
And I need to pull the String value from the config and put it into an integer variable
uint Store_var;
Store_var=Integer.parseInt(settings["var1"]);
But it shows an error as:
The name Integer does not exist in the current context.
I tried other methods also. But it isn't working.
Can you please help me how to proceed with it. Or any other method how to store the string value in an integer variable.
It is C#.

C#:
uint Store_var = UInt32.Parse(settings["var1"], System.Globalization.NumberStyles.HexNumber)
Java:
int value = Integer.parseInt(settings["var1"], 16);
It also won't parse the 0x so:
string hexString = settings["var1"].ToUpper().Trim();
if (hexString.StartsWith("0X"))
{
hexString = hexString.Substring(2, hexString.Length - 2);
}
uint Store_var = UInt32.Parse(hexString, System.Globalization.NumberStyles.HexNumber);

Related

Use a Decimal Value as a Hexadecimal Value

I have the int 15 (or the string "15", that's just as easy), and I need to use it to create the value:
"\u0015"
Is there some conversion which would accomplish this? I can't do this:
"\u00" + myInt.ToString()
Because the first literal is invalid. Is there a simple way to get this result?
(If you're curious, this is for integrating with a hardware device where the vendor sometimes expresses integer values as hexadecimal. For example, I'd need to send today's date to the device as "\u0015\u0010\u0002".)
Given that you want a Unicode code point of 21, not 15, you should definitely start with the string "15". If you try to start with 15 as an int, you'll find you can't express anything with a hex representation involving A-F...
So, given "15" the simplest way of parsing that as hex is probably:
string text = "15";
int codePoint = Convert.ToInt32(text, 16);
After that, you just need to cast to char:
string text = "15";
int codePoint = Convert.ToInt32(text, 16);
char character = (char) codePoint;
Note that this will only work for code points in the Basic Multilingual Plane (BMP) - i.e. U+0000 to U+FFFF. If you need to handle values beyond that (e.g. U+1F601) then you should use char.ConvertFromUtf32 instead:
string text = "15";
int codePoint = Convert.ToInt32(text, 16);
string character = char.ConvertFromUtf32(codePoint);
Unicode literals in strings are resolved at compile-time, that's why "\u00" + myInt.ToString() doesn't work (the ToString() and concatenation are evaluated at runtime).
You could cast the int to char:
int unicodeCodePoint = 15; // or 21
char c = (char)unicodeCodePoint;

Create a custom string c#

I have a numeric id of integer type. I want to add that integer type value to 00000 and concatenate it with string type variable.Assume my integer variable value is 1. After adding the variable it should look like 00001. And I want to concatenate with a string like "Added" and then convert whole thing to a string variable. like this "Added00001". As another example if my integer value is 111 it should look like "Added00111" at the end. Another example integer variable = 1234. Final string should be "Added01234". How to do it...? An example of how to do it.. or Any tutorial how to do such a thing would be really great. Thanks in advance.
You should use the short form format specifier of the ToString method. In the example below, D5 indicates that the integer 111 should be converted to a string with padded zeros to a length of 5 digits, e.g. 00111.
int i = 111;
string s = "Added";
var s = s + i.ToString("D5");
//s = "Added00111"

Trying to write hexadecimal value into a string in C#

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.

padding in asp.net

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');

Convert string "0x32" into a single byte

I'm working with C# trying to convert a string value into a byte. Seems to be harder then I expected. Basically I have a string called hex = "0x32" and need byte block to equal this value.
string hex = "0x32";
byte block = Convert.ToByte(hex);
The above doesn't work, does anybody know how I can successfully assign the hex value to the byte. I need to append this byte to a byte array later in the code.
Try the following
byte block = Byte.Parse(hex.SubString(2), NumberStyles.HexNumber);
The reason for the SubString call is to remove the preceeding "0x" from the string. The Parse function does not expect the "0x" prefix even when NumberStyles.HexNumber is specified and will error if encountered
Convert.ToByte(hex, 16)
string hex = "0x32";
int value = Convert.ToInt32(hex, 16);
byte byteVal = Convert.ToByte(value);
Will work...
Edit
A little code to demonstrate that 0x32 (hex) and 50 (int) are the same.
string hex = "0x32";
byte[] byteVal = new byte[1];
byteVal[0] = Convert.Byte(hex, 16);
Console.WriteLine(byteVal[0] + " - Integer value");
Console.WriteLine(BitConverter.ToString(byteVal) + " - BitArray representation");;

Categories

Resources