Trying to write hexadecimal value into a string in C# - 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.

Related

Integer to hexadecimal conversion

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

Hex string to int string and vice versa

I would like to convert some huge hex string to int string.
my hex string can be somthing like that:
666E7A427931676468633533394553764B38483240384A4B615241333455455A3369386F366048745A333932367A6A664142462F57574273
this number is too long to be store in an int, and I need to get it in a string as integer
and how to convert my new int string back in hex ?
have you some idea to do that?
You can use BigInteger (.NET 4.0+) to obtain the base 10 representation.
var hexString = "666E7A427931676468633533394553764B38483240384A4B615241333455455A3369386F366048745A333932367A6A664142462F57574273"
var bigNumber = BigInteger.Parse(hexString, NumberStyles.AllowHexSpecifier)
The result is
290825075527865440850840162776336047300722068695844686969687688283360481878315042200208855384521898951434440464937388090234036230242931
You can store it as a string from there if it's more convenient for you.

Overflow Exception. StringBuilder

I am trying to convert a StringBuilder into an Int64 but I get Overflow Exception and I cannot figure out why. The length of the string is not greater than the int. My current code is:
+ $exception {"Value was either too large or too small for an Int64."} System.Exception {System.OverflowException}
private static Int64 EightBit(string Data)
{
StringBuilder Pile = new StringBuilder();
char[] values = Data.ToCharArray();
foreach (char letter in values)
{
// Get the integral value of the character.
int value = Convert.ToInt32(letter);
// Convert the decimal value to a hexadecimal value in string form.
string hexOutput = String.Format("{0:X}", value);
Pile.Append(Convert.ToString(Convert.ToInt32(hexOutput, 16), 2));
}
return Convert.ToInt64(Pile.ToString()); //Error here
}
You are converting the ASCII values of a string to their binary values (Convert.ToString(string, 2) converts to base 2, right?)
character -> ASCII value -> Hex string representation -> Int Value -> Binary string representation of int value -> try to parse that string in base 10.
For instance, if "Data" is "147483647", then "Pile" is "110001110100110111110100111000110011110110110100110111" which is clearly larger than 'long.MaxValue' in base 10. By converting each character in a string to the binary representation of their ASCII values, you are creating a number string with far, far more digits than originally when you finally to to parse it base 10.
Just a quick step-through in the debugger should confirm the problem.

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