How to evalute string of IP address in c# [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Now I have a series of addresses with the format "\10.4.1.2\Camera_save". They are in total 18, the only number varies is the 3rd number in the IP address (e.g: 10.4.2.2, 10.4.3.2.....10.4.18.2)
What I need to do is extract the 3rd number from an address, any idea?
P.s The addresses are in an array with 18 elements. I have another array trams[] for storing the extracted 18 numbers.
Ok what I tried is to use string.replace(), like
directory[i].Fullname.Replace('\\10.4.','').Replace('.2\Camera_save','');
The error says the arguments for Replace() are too long and null string is not OK. Since I got this method from another post where the replace() was used exactly like this. So any explanation?

String[] ip = { #"\10.4.1.2\Camera_save", #"\10.4.5.2\Camera_save" };
String[] result = ip.Select(x => x.Split('.')[2]).ToArray();

Parse the string with Uri.Parse, pick the first segment, then parse it like IP address with IPAddress.Parse and then convert them to 4-byte array using the .GetAddressBytes() and pick your third byte.

you can Split() by .:
string number= ip.Split('.')[2];

Related

Getting error in visual studio while reading from mysql database [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
In the following code I try to fill the combobox
while (myReader.Read())
{
string sName = myReader.GetString("profesor"); <--- here is the error
cb_1najprof.Items.Add(sName);
cb_2najprof.Items.Add(sName);
cb_3najprof.Items.Add(sName);
cb_1najsprof.Items.Add(sName);
cb_2najsprof.Items.Add(sName);
cb_3najsprof.Items.Add(sName);
}
When the code above is run I get the error cannot convert string to int on the indicated line.
You can try this..
string sName = myReader.GetString(myReader.GetOrdinal("profesor"));
or
string sName = myReader["profesor"].ToString();
Also see related question
How to get data by SqlDataReader.GetValue by column name
GetString takes an integer as an argument, not a string. You have called it with "profesor". Try calling it with eg 1 instead. This integer corresponds to the column you want to read from.
That's because you can't pass a string to the .GetString() method [documentation] You have to pass an int. The int (zero-based) is the column number you want to read.
Yes I know my documentation link is for a SqlDataReader, but the same concept applies for a MySqlDataReader

C# subtraction of strings [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am sorry for that bad Title, but basicaly my problem is really simple. I got 1 string which is basic alphabet and the second string which is gonna be part of the alphabet (8 characters) which user will fill up by himself. If 2 characters are the same, they will get removed and then rest of characters will be in the TextBox3. could someone pls help me ?
string alphabet = "abcdefghijklmnopqrstuvwxyz_*";
string special = TextBox2.Text;
I assume you want to check the existence of substring and remove from the parent string, Try this
string alphabet = "abcdefghijklmnopqrstuvwxyz_*";
string special = textBox2.Text;
if (alphabet.ToLowerInvariant().Contains(special))
{
textBox3.Text = alphabet.Replace(special, "");
}

reading lines with variable numbers of fields from a file in c# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
How do you read the lines one by one if their fields are uneven and you need to know when it ends.
For example:
A;B;C;D
E;F;G;H;J
'A' is a person and 'B' , 'C' and 'D' are his friends.It goes the same for the second line I wrote.I know I could just write it with an even number of fields but I think this is a neater way to do it.
Thank you.
There are two functions that make this really easy: StreamReader.ReadLine and String.Split.
You use StreamReader.ReadLine to get the entire line of text:
string lineOfInput = reader.ReadLine();
Then, you can split on the semicolons to get all your "fields":
string[] fields = lineOfInput.Split(';');
fields[0] will contain the "person" and the rest, his "friends".
See StreamReader and Split on MSDN.
CSV files are deceptively simple. See this question, Simple csv reader?, for some details.
But I'd just use Sebastien Lorion's Fast CSV Reader from CodeProject:
http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader
All you've got to do is set the correct field separator and you're good to go. It's fast, it works well. It implements IDataReader, so it acts like a normal .Net data reader implementation.

How to String Format [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I've an array of strings below and wanted to format like the followings, what is the best way of doing that? Thanks in advance.
line[0] = "This is line one two tree";
line[1] = "This is Abc Cde";
line[2] = "This is cjdj";
I want it to format to display like this
This is line one two tree
This is Abc Cde..........
This is cjdj.............
You can use the string.PadRight() method, coupled with determining which of the array of strings is the widest:
var width = line.Max(l => l.Length);
foreach (var l in line)
Console.WriteLine(l.PadRight(width, '.'));
You could use:
var output = string.Join(Environment.NewLine, line.Select(l => l.PadRight(line[0].Length, '-').ToArray());
Use string.PadRight to pad each string, up to the specified length, with instances of a specified character.
Use PadRight:
http://msdn.microsoft.com/en-us/library/system.string.padright.aspx
e.g.
int len = line[0].Length;
Console.WriteLine(line[0]);
Console.WriteLine(line[1].PadRight(len,"."));
Console.WriteLine(line[2].PadRight(len,"."));

RSACryptoServiceProvider helper [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I was looking for RSACryptoServiceProvider helper and found two different implementations
1) http://www.cnblogs.com/WYB/archive/2008/06/19/1225704.html
2) https://github.com/robvolk/Helpers.Net/blob/master/Src/Helpers.Net/EncryptionExtensions.cs
both of them working
var encryptedBytes = myBytes.RSAEncrypt(publicKey);
System.Text.Encoding.Unicode.GetString(encryptedBytes);
returns strings like "蹩巷Ӂය馧㾵봽놶徤蕺蓷課Ϝ堲泍썳⁙䃑ക늏...."
myString.EncryptStringUsingXMLFile(publicKey)
returns strings like "AnvFFT6YpoiAyIFwl+tueZq56Zcb0B7WhBEvz5uWl...."
May be some one can explain why first one producing Chinese strings and how to change that?
What approach is better?
To answer your first question. While it may look like it is producing Chinese characters what is actually happening is it is turning a byte array into unicode. In c# typically when you want to store a byte array you convert it to base64 which is what your second example appears to return.
Your first example would become this:
var encryptedBytes = myBytes.RSAEncrypt(publicKey);
Convert.ToBase64String(encryptedBytes) // this line changed
returns strings like "AnvFFT6YKpoiAy...."
As for what is recommended, the most common is to use base64. The reasons people use base64 over unicode or UTF-8 for binary data can be found in these answers:
https://stackoverflow.com/a/201491/701062
MSDN - Convert.ToBase64String(byte[])
http://msdn.microsoft.com/en-us/library/dhx0d524(v=vs.100).aspx
MSDN - Convert.FromBase64String(string) - Useful if you need to convert back into a byte array
http://msdn.microsoft.com/en-us/library/system.convert.frombase64string(v=vs.100).aspx

Categories