Just trying to use string.Format() to convert system MAC address to text format. But it's not working:
byte[] MacAddr = new byte[6];
// this works, but rather clumzy
mac = string.Format("{0:X2}-{1:X2}-{2:X2}-{3:X2}-{4:X2}-{5:X2}",
MacAddr[0], MacAddr[1], MacAddr[2], MacAddr[3], MacAddr[4], MacAddr[5]);
// give me index error
mac = string.Format("{0:X2}-{1:X2}-{2:X2}-{3:X2}-{4:X2}-{5:X2}", MacAddr);
Edit1: OK, I am wrong, but it seems string.format works for this guy's case with string[] .
I can see there is a overload method for string.format:
Format(String, array<Object>[]()[]). Is it possble to create some form of byte[], that can be taken as this array<Object>[]()[] ?
the error occurs because you want to format 6 items but there is only 1 in your parameter list
//6 parameters expected, only one "MacAddr" given
mac = string.Format("{0:X2}-{1:X2}-{2:X2}-{3:X2}-{4:X2}-{5:X2}", MacAddr);
here is a shorter version compared to your working approach
mac = string.Join("-", MacAddr.Select(x => x.ToString("X2")));
This is because you specify a format with 6 parameters but provide only one:
//expected 6 parameters, provided only one
mac = string.Format("{0:X2}-{1:X2}-{2:X2}-{3:X2}-{4:X2}-{5:X2}", MacAddr);
if you are working with C# 6.0, you can also work with string interpolation:
//string interpolation
mac = $"{MacAddr[0]}:X2-{MacAddr[1]}:X2-{MacAddr[2]}:X2-{MacAddr[3]}:X2-{MacAddr[4]}:X2-{MacAddr[5]}:X2";
There is only 1 parameter in your string.Format() function while it requires 6 parameters as per requirement.
You can use String.Join for a better readable approach -
mac = string.Join("-", MacAddr.Select(x => x.ToString(":X2")));
try BitConverter
mac = BitConverter.ToString(MacAddr);
BitConverter.ToString(byte[]) gets the exact string you want, although MAC addresses are usually separated by colons, not dashes.
Related
I have a integration with facebook that sends me special characters(smilies and so on, for example u+1f600 that is called a grinning face). It is not possible to store this in my UTF8(not UTF8mb4) database, so how can I make the string UFT8 (not UTF8mb4) friendly?
I can´t convert my database to UTF8mb4.
You can use a simple regex:
var rx = new Regex(#"[\uD800-\uDBFF][\uDC00-\uDFFF]");
string str = "abcd\U0001D11Eabcd";
str = rx.Replace(str, "?"); // abcd?abcd
If you look http://en.wikipedia.org/wiki/UTF-16 you'll see that non-BMP characters are composed by two 16 bit code units, with the ranges given in the Regex.
The following VB code works correctly and does not flag up any errors.
strLine = strLine.Replace(strLine.LastIndexOf(","), "")
However the same C# code doesn't:
strLine = strLine.Replace(strLine.LastIndexOf(","), "");
This will not compile as it says
The best overloaded method for 'string.Replace(string,string)' has some invalid arguements.
How come this works in VB but not in C#? and how do I fix this?
I thought it might be similar to C# string.Replace doesn't work but it implies that that code will infact complile.
Likewise with other string.Replace questions: string.Replace (or other string modification) not working, it appears they will infact compile, whereas mine will not.
LastIndexOf returns an integer, not a string. Replace takes string, string as parameters, you're passing it int, string, hence the exception The best overloaded method for 'string.Replace(string,string)' has some invalid arguements.
If you're just looking to remove all , use this:
strLine = strLine.Replace(",", "");
Based on your code, you may be only wanting to replace the last instance of , so try this if that's what you want:
StringBuilder sb = new StringBuilder(strLine);
sb[strLine.LastIndexOf(",")] = "";
strLine = sb.ToString();
Reading the documentation, I'm amazed that the first example works at all. string.Replace should receive either a couple of chars or a couple of strings, not an integer and then a string. Pheraps the VB version is converting the integer to its char representation automatically?
In c# the String.Replace's first parameter must be either char or string, but you are using a integer(string.LastIndexOf returns a integer).
Why not just use:
strLine = strLine.Replace(',', '');
Try the following
string.Replace("," , "");
In C# LastIndexOf returns an int, but Replace expects a string or a char as the first argument. I don't know VB to explain why your code works, but I can explain that in C# you can't pass an integer where a string or a char is expected.
In C#, this will do what you want:
strLine = strLine.Replace(',', '');
Hope that helps.
you can use String.Remove
strLine = strLine.Remove(strLine.LastIndexOf(','), 1);
Using C#, Framework 4.0, I'm facing a tricky problem with the german language.
Considering this snippet :
string l_stest = "ZÄHLWERKE";
Console.WriteLine(l_stest.Length); // 9
Console.WriteLine(toto.LengthInTextElements); // 9
Console.ReadLine();
The result will be 9;
Now, selecting the text withing Notepad++, it will give me a length of 10.
I'm guessing the encoding is the source of my problem but without having to scan my words and replace the Umlauts by the matching two letters (Ä -> AE), how can I proceed to calculate precisely the length of my strings ?
Edit : I consider the correct length is 10.
Thanks in advance !
Encoding.UTF8.GetByteCount(l_stest) looks like it'll get the length you want.
I'm not sure if "key value" is correct word for it as there are few formats I believe, what im talking about is http://pastebin.com/XJVx1dB5 this is format where 88 is Z etc. I hope im clear.
I have tried many things, but convert from string function of keys converter class is the only one that is remotely close. The problem is, it converts "x" to 88 as I wanted, however it fails to convert " " or "[" because it expects "SPACE"(string) and not a single space as char I believe.
I used it like this, maybe there is another(correct?) version of using it:
((int)kc.ConvertFromString(s))
So what I want to do is to get that chars of mine to code ones. How can I achieve this ?
Try something like:
var stringValue = " ";
var intValue = (int)stringValue[0];
A string is an 'array of chars' - so waht you need to do is get the char [in this case the first one] - and convert that to an int - in this case simply by casting.
If you want to do it with the enum, then you're going to have to write a manual system to convert " " to Keys.Space .
If you review your enum Keys carefully, you will see there is no value for [.
In fact, that character (on my keyboard) is reported as Oem4.
This code works:
var c = kc.ConvertFromString("Oem4");
However,
var c = kc.ConvertFromString("[");
Cannot work because [ is not a valid member of the enum you are converting from.
What do you want to do once you have converted the string? That may help guide a better answer to your question.
Imagine that users are inserting strings in several computers.
On one computer, the pattern in the configuration will extract some characters of that string, lets say position 4 to 5.
On another computer, the extract pattern will return other characters, for instance, last 3 positions of the string.
These configurations (the Regex patterns) are different for each computer, and should be available for change by the administrator, without having to change the source code.
Some examples:
Original_String Return_Value
User1 - abcd78defg123 78
User2 - abcd78defg123 78g1
User3 - mm127788abcd 12
User4 - 123456pp12asd ppsd
Can it be done with Regex?
Thanks.
Why do you want to use regex for this? What is wrong with:
string foo = s.Substring(4,2);
string bar = s.Substring(s.Length-3,3);
(you can wrap those up to do a bit of bounds-checking on the length easily enough)
If you really want, you could wrap it up in a Func<string,string> to put somewhere - not sure I'd bother, though:
Func<string, string> get4and5 = s => s.Substring(4, 2);
Func<string,string> getLast3 = s => s.Substring(s.Length - 3, 3);
string value = "abcd78defg123";
string foo = getLast3(value);
string bar = get4and5(value);
If you really want to use regex:
^...(..)
And:
.*(...)$
To have a regex capture values for further use you typically use (), depending on the regex compiler it might be () or for microsoft MSVC I think it's []
Example
User4 - 123456pp12asd ppsd
is most interesting in that you have here 2 seperate capture areas. Is there some default rule on how to join them together, or would you then want to be able to specify how to make the result?
Perhaps something like
r/......(..)...(..)/\1\2/ for ppsd
r/......(..)...(..)/\2-\1/ for sd-pp
do you want to run a regex to get the captures and handle them yourself, or do you want to run more advanced manipulation commands?
I'm not sure what you are hoping to get by using RegEx. RegEx is used for pattern matching. If you want to extract based on position, just use substring.
It seems to me that Regex really isn't the solution here. To return a section of a string beginning at position pos (starting at 0) and of length length, you simply call the Substring function as such:
string section = str.Substring(pos, length)
Grouping. You could match on /^.{3}(.{2})/ and then look at group $1 for example.
The question is why? Normal string handling i.e. actual substring methods are going to be faster and clearer in intent.