This question already has answers here:
how to Convert string MAC Address to Hex [closed]
(2 answers)
Hex increment / loop till FFF
(1 answer)
Convert int to hex and then into a format of 00 00 00 00
(3 answers)
Closed 2 years ago.
I have a seemingly simple request that I have no idea how to do in C#. I have searched and only found answers using other languages.
I have a string that includes a MAC address. For example, string sMyMACAddress = "00:1D:FE:12:37:1A";
How do I take this MAC address, increment it by 1, and pop out the resultant new next-in-sequence MAC Address in a new string such as sMyNextMACAddress?
Can someone help me with this or at least point me in the right direction?
I've tried the following after the latest suggestions. UPDATE: I've provided the answer below as my question was closed prior to an answer being accepted.
string sMACAddressIn = "00:1D:FE:12:37:1A";
string sMACAddressOut = "";
Console.WriteLine("MAC Address going in: " + sMACAddressIn);
sMACAddressIn = sMACAddressIn.Replace(":", ""); // Format this otherwise you get an incorrect format error on numberstyles.hexnumber
long iLastMACAddressHex = long.Parse(sMACAddressIn, NumberStyles.HexNumber);
iLastMACAddressHex++;
sMACAddressOut = string.Join(":", BitConverter.GetBytes(iLastMACAddressHex).Reverse() .Select(b => b.ToString("X2"))).Substring(6);
Console.WriteLine("MAC Address going out: " + sMACAddressOut);
Console.Read();
I get the following result.
MAC Address going in: 00:1D:FE:12:37:1A
MAC Address going out: 00:1D:FE:12:37:1B
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 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:
How do you get a variable's name as it was physically typed in its declaration? [duplicate]
(11 answers)
Closed 7 years ago.
I have an array
int[] arr1 = new int[] { 1, 2, 3 };
Console.WriteLine("How to print the text arr1 ?? ");
I need the array name i.e. 'arr1' in console output/Writeline, seems simple but need help.
Edit :
Output should be
arr1
NOT
1 2 3
Good to know, this is achieved in PHP too.
If you declare/initialize variables in a far more complicated way you could...
echo "<pre>";
$var_name="Foo"; // naming the var "Foo"
$$var_name="Bar"; // declaring the var "Foo"
echo "value: ". $Foo . "<br><br>";
foreach(compact($var_name) as $key=>$value){ // refering to var name
echo "name: " . $key . "<br><br>";
}
echo "pair: ";
print_r(compact($var_name));
echo "<pre>" ;
But we'd still need to get it through compact($var_name) instead of compact($Foo)
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.