This question already has answers here:
Understanding slicing
(38 answers)
Closed 4 years ago.
Hi I'm trying to convert som Python code to C# I'm struck understanding this line of code
n = int(e[2:10], 16)
e is a string looking like this:
0100000180a6fa85de8dd3381cc277b046d7e3856307519d03da4e3ff5dca52de833c56951ab3e539a161df98454be311fd242407b25bf7b8e84c322f06f913d712393922bd1477d2cf3a9d2ba14bb00f8b2d7a203376afed0e1782e49ea55d43cee8e3bb8331f3f8aa81955bae8fcd118f640b4cd49d787bd8a12d57f424b371d07f08de67ab8f40bf5894288920adfe9480cfbec7deef073c3f137d71dff9d4ab967d9178648961cd2def00d376cf01dca6a4c6428243cef23eeab9791f5cd7d66f5293879b7ed83abf600f78426491c57c8a61e
n = int(e[2:10], 16) takes characters 2..10 from e and interprets them as hexadecimal characters to interpret as an integer.
That is, for your input,
>>> e = '0100000180a6fa85de8dd3...'
>>> f = e[2:10]
>>> f
'00000180'
>>> int(f, 16)
384
so you should be able to do the same with something like Convert.ToInt32(e.Substring(2, 8), 16) in C#.
At first, you're using string slicing (from 2nd to 9th character) using [2:10]. Then you're converting them to (decimal) int from hexadecimal.
Which will result n = 384.
Related
This question already has answers here:
Convert string "0x32" into a single byte
(3 answers)
Closed 5 months ago.
Sorry for the rudimentary question.
I am writing code to calculate a checksum as follows
public void GetCheckSum()
{
//test data1
//The answer is 61
//var hexInput = "43-33-30-30-20-20-20-20-20-20-20-32-03";
//test data2
//The answer is 42
var hexInput = "54-33-30-30-20-20-20-20-38-2E-30-03";
var splitByte=hexInput.Split("-").Select(hex => Convert.ToByte(hex));
byte[] byteData = splitByte.ToArray();
byte checksumTest = new byte();
for(int i = 0; i < byteData.Length; i++)
{
checkSumTest = (byte)(checkSumTest ^ byteData[i]);
}
Console.WriteLine(checkSumTest);
For test data1, I get the desired value of 61.
However, test data2 gives an error with the byte conversion of "2E".
ERROR : Input string was not in a correct format
How should I handle hexadecimal numbers like "2E"?
You should be using Convert.ToByte(hex, 16) to indicate that the input represents a number in base 16.
This question already has answers here:
How to format floating point value with fix number of digits?
(3 answers)
How do I format a Decimal to a programatically controlled number of decimals in c#?
(5 answers)
Closed 3 years ago.
I would like to specify that I want 6 digits when I convert a value from a decimal to a string. I have the following code.
decimal bound = (decimal) field.MaxVal; //trait.MaxVal is a Nullable decimal...
// my debugger shows that bound contains the value 20.0000000000
int numDigits = 6;
string num = numDigits.ToString();
string output = bound.ToString("G" + num);
// output is '20' i expected '20.0000'
return output;
Any ideas on how I would do this.
I am focused on getting a total of 6 numbers in my string as opposed to 4 decimal places!
NOTE: bound will may have more or less than 2 digits in the whole number part.
string output = bound.ToString("F" + num);
https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings?redirectedfrom=MSDN#FFormatString
F stands for fixed-point.
This question already has answers here:
How to remove leading zeros using C#
(9 answers)
Closed 5 years ago.
In my string start can be all sorts of numbers (1 until 99), but when the numbers get generated, 1 always get shown like 01. Is there an way to remove that 0, without if the number is 20 that the number get changed to 2?
You can use the String.TrimStart method:
string num = "0001";
num = num.TrimStart('0');
Another solution:
if (start.ElementAt(0) == '0')
start = start.Remove(0, 1);
or shorter:
start = start.ElementAt(0) == '0' ? start.Substring(1) : start;
This question already has answers here:
C# convert int to string with padding zeros?
(15 answers)
Closed 6 years ago.
I have phone numbers like this :
452341
12
45789632
Is there a way i can format them like this way :
00452341
00000012
45789632
You cam use Padding:
Number.PadLeft(length, '0');
For example:
string num = "1234";
num.PadLeft(10, '0');
The result will be 0000001234
ToString("D8")
Here - MSDN - Decimal ("D") Format Specifier
NOTE:
PadLeft(length, '0'); does not work for negative numbers
Ex: (-1).Padleft(5, '0') --> 000-1
Use format with leading zeroes (Example for 8 symbols in number):
452341.ToString("D8");
if you have already strign use solution of Ashkan:
"452341".PadLeft(8, '0');
This question already has answers here:
C# convert int to string with padding zeros?
(15 answers)
Closed 8 years ago.
i've been kicking my self for a while now with regex, and unable to find a proper solution. I've been trying to transform a string using Regex.Replace() method in C# which should prepend 0 to existing string if length is less than 5, the conversion might be as follow
Input String ----------- Output String
12345 ----------- 12345
123 ----------- 00123
123456 ----------- 123456
any help will be appreciated
You can use String.PadLeft: "1234".PadLeft(5, '0')
It can be like
var outputString = Regex.Replace(inputString, #"\d+", n => n.Value.PadLeft(5, '0'));
but you don't really need regex in this case.
You don't need any regex
if(str.Length < 5){
for(var i = 0; i < 5 - str.length; i++)
str = "0" + str;
}
The above is all you need. What you're doing is if the input string's Length is less than 5, you're just subtracting and looping, adding them.