need to do an ascii shift cypher in c# - c#

I am trying to shift the characters in a string by 20 to match a file format I used in basic. I am using the following code in c#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace test_controls
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string text3;
private void button1_Click(object sender, EventArgs e)
{
// This is a test conversion of my pdw save routine from basic to c#
int pos = 0;
string text = label1.Text;
int t = text.Length; // get the length of the text
while (pos < t + 1) ;
string s = text.Substring(pos, 1); // get subsstring 1 character at a time
byte[] ASCIIValues = Encoding.ASCII.GetBytes(text); // convert that character to ascii
foreach (byte b in ASCIIValues)
{
int temp = b;
temp = temp + 20; // add 20 to the ascii value
char text2 = Convert.ToChar(temp); // convert the acii back into a char
text3 =""+ text2.ToString(); // add the char to the final string
}
label1.Text = text3; // rewrite the new string to replace the old one for the label1.text
}
}
}
The problem is it just does nothing and doesn't respond and I have to tell windows to close the unresponsive program. To be clear, I'm using winforms in c# to make a shift cypher. All of this code I'm using I found in various answers and pieced it together. in Vb or any other basic, I would just get the ascii value of each character in the string then do the math and convert it back using the chr$ command.
Any help would be greatly appreciated.

You have two problems. As pointed out in the comments, the following line is an infinite loop:
while (pos < t + 1) ;
Even without the loop, though, your shift algorithm is incorrect. The following line will also lead to incorrect results:
temp = temp + 20;
Consider as counterexamples the following cases:
G maps to [
ASCII z = 122. 122 + 20 = 144, which isn't even a valid ASCII character.
Uppercase Z would map to lowercase n
You could come up with other similar cases.
Incidentally, you could also rewrite this line as temp += 20.
Finally, this line is incorrect:
text3 =""+ text2.ToString();
You're not appending the new text to text3, you're replacing it every time you do an iteration, so text3 will always contain the last character encoded (rather than the entire string). Keep in mind, too, that building a C# string like this (especially a long one) is inefficient as strings are immutable objects in C#. If the string in question might be long you want to consider using a StringBuilder for this purpose.

Related

ASP.net when i click a button i want to add rows from a grid view to a database but its giving me an error [duplicate]

I'm new with C#, I have some basic knowledge in Java but I can't get this code to run properly.
It's just a basic calculator, but when I run the program VS2008 gives me this error:
I did almost the same program but in java using JSwing and it worked perfectly.
Here's the form of c#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace calculadorac
{
public partial class Form1 : Form
{
int a, b, c;
String resultado;
public Form1()
{
InitializeComponent();
a = Int32.Parse(textBox1.Text);
b = Int32.Parse(textBox2.Text);
}
private void button1_Click(object sender, EventArgs e)
{
add();
result();
}
private void button2_Click(object sender, EventArgs e)
{
substract();
result();
}
private void button3_Click(object sender, EventArgs e)
{
clear();
}
private void add()
{
c = a + b;
resultado = Convert.ToString(c);
}
private void substract()
{
c = a - b;
resultado = Convert.ToString(c);
}
private void result()
{
label1.Text = resultado;
}
private void clear()
{
label1.Text = "";
textBox1.Text = "";
textBox2.Text = "";
}
}
What can be the problem? Is there a way to solve it?
PS: I also tried
a = Convert.ToInt32(textBox1.text);
b = Convert.ToInt32(textBox2.text);
and it didn't work.
The error means that the string you're trying to parse an integer from doesn't actually contain a valid integer.
It's extremely unlikely that the text boxes will contain a valid integer immediately when the form is created - which is where you're getting the integer values. It would make much more sense to update a and b in the button click events (in the same way that you are in the constructor). Also, check out the Int.TryParse method - it's much easier to use if the string might not actually contain an integer - it doesn't throw an exception so it's easier to recover from.
I ran into this exact exception, except it had nothing to do with parsing numerical inputs. So this isn't an answer to the OP's question, but I think it's acceptable to share the knowledge.
I'd declared a string and was formatting it for use with JQTree which requires curly braces ({}). You have to use doubled curly braces for it to be accepted as a properly formatted string:
string measurements = string.empty;
measurements += string.Format(#"
{{label: 'Measurement Name: {0}',
children: [
{{label: 'Measured Value: {1}'}},
{{label: 'Min: {2}'}},
{{label: 'Max: {3}'}},
{{label: 'Measured String: {4}'}},
{{label: 'Expected String: {5}'}},
]
}},",
drv["MeasurementName"] == null ? "NULL" : drv["MeasurementName"],
drv["MeasuredValue"] == null ? "NULL" : drv["MeasuredValue"],
drv["Min"] == null ? "NULL" : drv["Min"],
drv["Max"] == null ? "NULL" : drv["Max"],
drv["MeasuredString"] == null ? "NULL" : drv["MeasuredString"],
drv["ExpectedString"] == null ? "NULL" : drv["ExpectedString"]);
Hopefully this will help other folks who find this question but aren't parsing numerical data.
If you are not validating explicitly for numbers in the text field, in any case its better to use
int result=0;
if(int.TryParse(textBox1.Text,out result))
Now if the result is success then you can proceed with your calculations.
Problems
There are some possible cases why the error occurs:
Because textBox1.Text contains only number, but the number is too big/too small
Because textBox1.Text contains:
a) non-number (except space in the beginning/end, - in the beginning) and/or
b) thousand separators in the applied culture for your code without specifying NumberStyles.AllowThousands or you specify NumberStyles.AllowThousands but put wrong thousand separator in the culture and/or
c) decimal separator (which should not exist in int parsing)
NOT OK Examples:
Case 1
a = Int32.Parse("5000000000"); //5 billions, too large
b = Int32.Parse("-5000000000"); //-5 billions, too small
//The limit for int (32-bit integer) is only from -2,147,483,648 to 2,147,483,647
Case 2 a)
a = Int32.Parse("a189"); //having a
a = Int32.Parse("1-89"); //having - but not in the beginning
a = Int32.Parse("18 9"); //having space, but not in the beginning or end
Case 2 b)
NumberStyles styles = NumberStyles.AllowThousands;
a = Int32.Parse("1,189"); //not OK, no NumberStyles.AllowThousands
b = Int32.Parse("1,189", styles, new CultureInfo("fr-FR")); //not OK, having NumberStyles.AllowThousands but the culture specified use different thousand separator
Case 2 c)
NumberStyles styles = NumberStyles.AllowDecimalPoint;
a = Int32.Parse("1.189", styles); //wrong, int parse cannot parse decimal point at all!
Seemingly NOT OK, but actually OK Examples:
Case 2 a) OK
a = Int32.Parse("-189"); //having - but in the beginning
b = Int32.Parse(" 189 "); //having space, but in the beginning or end
Case 2 b) OK
NumberStyles styles = NumberStyles.AllowThousands;
a = Int32.Parse("1,189", styles); //ok, having NumberStyles.AllowThousands in the correct culture
b = Int32.Parse("1 189", styles, new CultureInfo("fr-FR")); //ok, having NumberStyles.AllowThousands and correct thousand separator is used for "fr-FR" culture
Solutions
In all cases, please check the value of textBox1.Text with your Visual Studio debugger and make sure that it has purely-acceptable numerical format for int range. Something like this:
1234
Also, you may consider of
using TryParse instead of Parse to ensure that the non-parsed number does not cause you exception problem.
check the result of TryParse and handle it if not true
int val;
bool result = int.TryParse(textbox1.Text, out val);
if (!result)
return; //something has gone wrong
//OK, continue using val
In my case I forgot to put double curly brace to escape. {{myobject}}
You may encounter this exception when you use a string formatter with invalid bracket syntax.
// incorrect
string.Format("str {incorrect}", "replacement")
// correct
string.Format("str {1}", "replacement")
You have not mentioned if your textbox have values in design time or now. When form initializes text box may not hae value if you have not put it in textbox when during form design. you can put int value in form design by setting text property in desgin and this should work.
it was my problem too ..
in my case i changed the PERSIAN number to LATIN number and it worked.
AND also trime your string before converting.
PersianCalendar pc = new PersianCalendar();
char[] seperator ={'/'};
string[] date = txtSaleDate.Text.Split(seperator);
int a = Convert.ToInt32(Persia.Number.ConvertToLatin(date[0]).Trim());
I had a similar problem that I solved with the following technique:
The exception was thrown at the following line of code (see the text decorated with ** below):
static void Main(string[] args)
{
double number = 0;
string numberStr = string.Format("{0:C2}", 100);
**number = Double.Parse(numberStr);**
Console.WriteLine("The number is {0}", number);
}
After a bit of investigating, I realized that the problem was that the formatted string included a dollar sign ($) that the Parse/TryParse methods cannot resolve (i.e. - strip off). So using the Remove(...) method of the string object I changed the line to:
number = Double.Parse(numberStr.Remove(0, 1)); // Remove the "$" from the number
At that point the Parse(...) method worked as expected.

Delete a specific string from a file in C#

Title says it all, I have a file called test.txt with these contents:
Hello from th [BACK]e
This i [BACK]s line two.
Here, [BACK] is just a visible representation of the backspace. So a i [BACK]s would mean is. Because a backspace is implemented after a space and i.
So basically, at the click of a button, i should be able to access this file and remove ALL strings containing the word [BACK]-1. -1 implemented because a [BACK] means a backspace and is used to remove the last string before the word [BACK].
EDIT:
This time i replaced [BACK] with [SDRWUE49CDKAS]. Just to make it a unique string. I also tested on another file. This time a .html with following contents:
Alpha, Brav [SDRWUE49CDKAS]o, Charlie, Dr [SDRWUE49CDKAS][SDRWUE49CDKAS]elta, Echo.
// ^^Implementing "backspace" ^^Here doing it double because we made a mistake in spelling "Delta"
//These sentences should be Alpha, Bravo, Charlie, Delta, Echo
Did some experimenting and tested it out with this code:
string s = File.ReadAllText(path2html);
string line = "";
string contents = File.ReadAllText(path2html);
if (contents.Contains("[SDRWUE49CDKAS]"))
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(path2html);
s = s.Remove(s.LastIndexOf(line + "[SDRWUE49CDKAS]") - 2, 15);
sw.WriteLine(s);
sw.Close();
}
The edited code above will give me an output of actually deleting [SDRWUE49CDKAS] but not exactly how i wanted it to be:
Alpha, BraS]o, Charlie, DS]elta, Echo.
This really caused some confusion with the testing. And also not to mention that i had to run this code 3 times, because we had 3 x [SDRWUE49CDKAS]. So a loop will do good. I checked out a bunch of similar problems on the web, but couldn't find a one working. I'm tryna test out this one too. But it's using a StreamReader and a StreamWriter at the same time. Or maybe i should make a copy the original, and make a temp file?
var s = "i [BACK]s";
s = s.Remove(s.IndexOf("[BACK]")-1, 1 + "[BACK]".Length);
result is "is".
Explanation:
Find the start-index of [Back]
Go back one position to start at the char before
Remove from there that extra char + the marker chars
But there are several issues:
This assumes that that search string is not at the start of that string (there is a char to remove)
Plus it only removes the first occurrence so you will have to repeat this until all are removed
And it doesn't handle "surrogate pairs"
Use Regex. You may have multiple spaces or no space is it is at the beginning of the line
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string input =
"Hello from th [BACK]e\n" +
"This i [BACK]s line two.\n";
string pattern = #"\s*\[BACK\]";
string output = Regex.Replace(input, pattern, "");
}
}

Finding longest word in string

Ok, so I know that questions LIKE this have been asked a lot on here, but I can't seem to make solutions work.
I am trying to take a string from a file and find the longest word in that string.
Simples.
I think the issue is down to whether I am calling my methods on a string[] or char[], currently stringOfWords returns a char[].
I am trying to then order by descending length and get the first value but am getting an ArgumentNullException on the OrderByDescending method.
Any input much appreciated.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace TextExercises
{
class Program
{
static void Main(string[] args)
{
var fileText = File.ReadAllText(#"C:\Users\RichardsPC\Documents\TestText.txt");
var stringOfWords = fileText.ToArray();
Console.WriteLine("Text in file: " + fileText);
Console.WriteLine("Words in text: " + fileText.Split(' ').Length);
// This is where I am trying to solve the problem
var finalValue = stringOfWords.OrderByDescending(n => n.length).First();
Console.WriteLine("Largest word is: " + finalValue);
}
}
}
Don't split the string, use a Regex
If you care about performance you don't want to split the string. The reason in order to do the split method will have to traverse the entire string, create new strings for the items it finds to split and put them into an array, computational cost of more than N, then doing an order by you do another (at least) O(nLog(n)) steps.
You can use a Regex for this, which will be more efficient, because it will only iterate over the string once
var regex = new Regex(#"(\w+)\s",RegexOptions.Compiled);
var match = regex.Match(fileText);
var currentLargestString = "";
while(match.Success)
{
if(match.Groups[1].Value.Length>currentLargestString.Length)
{
currentLargestString = match.Groups[1].Value;
}
match = match.NextMatch();
}
The nice thing about this is that you don't need to break the string up all at once to do the analysis and if you need to load the file incrementally is a fairly easy change to just persist the word in an object and call it against multiple strings
If you're set on using an Array don't order by just iterate over
You don't need to do an order by your just looking for the largest item, computational complexity of order by is in most cases O(nLog(n)), iterating over the list has a complexity of O(n)
var largest = "";
foreach(var item in strArr)
{
if(item.Length>largest.Length)
largest = item;
}
Method ToArray() in this case returns char[] which is an array of individual characters. But instead you need an array of individual words. You can get it like this:
string[] stringOfWords = fileText.Split(' ');
And you have a typo in your lambda expression (uppercase L):
n => n.Length
Try this:
var fileText = File.ReadAllText(#"C:\Users\RichardsPC\Documents\TestText.txt");
var words = fileText.Split(' ')
var finalValue = fileText.OrderByDescending(n=> n.Length).First();
Console.WriteLine("Longest word: " + finalValue");
As suggested in the other answer, you need to split your string.
string[] stringOfWords = fileText.split(new Char [] {',' , ' ' });
//all is well, now let's loop over it and see which is the biggest
int biggest = 0;
int biggestIndex = 0;
for(int i=0; i<stringOfWords.length; i++) {
if(biggest < stringOfWords[i].length) {
biggest = stringOfWords[i].length;
biggestIndex = i;
}
}
return stringOfWords[i];
What we're doing here is splitting the string based on whitespace (' '), or commas- you can add an unlimited number of delimiters there - each word, then, gets its own space in the array.
From there, we're iterating over the array. If we encounter a word that's longer than the current longest word, we update it.

How to convert to System.Int32 when doing a select from a DataTable

I've got a DataTable that I've loaded from a CSV file using the KBCsv library. From what I can tell the columns are all numbers in the csv file and there aren't any null rows. I want to do a select on the data table with a query that I've created using String.Format. Here's the query:
String qry = String.Format("GRIDX = {0} AND GRIDY = {1} AND CONVERT([{2}], 'System.Int32') {3} {4}", xc, yc, _testDataColumn, _comparison, _threshold);
Where xc is a double, yc is a double, _testDataColumn is a string, _comparison is a string and _threshold is a double. The table.Select() method converts the string to:
GRIDX = 0 AND GRIDY = 4 AND CONVERT([ST DEVSSI(dBm)], 'System.Int32') = 5
I put that CONVERT in there because before I was getting a
Cannot perform '=' operation on System.String and System.Int32
error message. I've looked at this for help. I don't understand how that last column (ST DEVSSI) became a string. Am I doing the conversion correctly? Is there another way to take care of such errors?
If you run the code with the sample CSV File you should get the same error when it searches for coordinates (0, 4) so it looks like the problem is near the end of the CSV file, but I'm still not sure.
A full example of my code:
using Kent.Boogaart.KBCsv;
using Kent.Boogaart.KBCsv.Extensions;
using Kent.Boogaart.KBCsv.Extensions.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace TestCSVReader
{
public partial class MainWindow : Window
{
private String _csvFilePath;
private String _tempFilePath;
private StreamReader _streamReader;
private HeaderRecord _headerRecord = new HeaderRecord();
private Double _threshold = 5;
private String _testDataColumn = "ST DEVSSI(dBm)";
private String _comparison = "=";
private String[] coordinates = { "0,1", "0,2", "0,4", "1,1", "1,2", "1,3", "1,4"};
public MainWindow()
{
InitializeComponent();
_tempFilePath = System.IO.Path.GetTempPath() + #"\temp.csv";
_csvFilePath = "CSV FILE PATH";
using (var streamReader = new StreamReader(_csvFilePath))
{
HeaderRecord headerRecord = new HeaderRecord(streamReader.ReadLine().Split(','));
DataTable table = new DataTable();
using (var reader = new CsvReader(streamReader))
{
reader.HeaderRecord = headerRecord;
table.Fill(reader);
}
foreach (String coordinate in coordinates)
{
var xy = coordinate.Split(',');
double xc = Double.Parse(xy[0]);
double yc = Double.Parse(xy[1]);
String qry = String.Format("GRIDX = {0} AND GRIDY = {1} AND [{2}] {3} {4}", xc, yc, _testDataColumn, _comparison, _threshold);
var results = table.Select(qry);
if (results.Length > 0)
{
Console.WriteLine(String.Format("Found a match for {0}, {1}", xc, yc));
}
else
{
Console.WriteLine(String.Format("Found a match for {0}, {1}", xc, yc));
}
}
}
_streamReader.Close();
}
}
}
That sample code gives me the error above so I attempted to use a CONVERT in the query statement, but I can't seem to get it to work.
There's a lot going on here, but to fix the specific problem you're reporting:
When building the query string, make sure to include a decimal point for the threshold parameter value. The simplest way is {4:f} in the format string.
The reason this works is because the CSV library you're using just makes all of the values in the data table have string types. When you filter, each value is being converted from a string to the type of the value it is being compared against. So if you compare "3" with 3, the string is converted to an integer and it works. But comparing "3.5" with 3 won't just return false, it fails because "3.5" can be converted to an integer. Comparing against a value with a decimal point will convert the string to a double instead of an integer, and then it all works.
It's going to be slow as all heck though. It's parsing strings like crazy here, and the data table is loaded into memory. There are much better approaches - like loading the CSV into a real database that has indexes.
In C# there are several methods to perform string to numeric type conversion:
Sample 1:
string _str ="12.25";
// throws exception if fail, relatively slow in case of invalid input
double _dbl = Convert.ToDouble(_str);
Sample 2:
string _str ="12.25";
// throws exception if fail, relatively slow in case of invalid input
double _dbl = Double.Parse(_str);
Sample 3:
string _str ="12.25";
double _dbl;
// true if conversion OK, false is fail;
// very fast compare to other two: (1) and (2)
Double.TryParse(_str,out _dbl);
Hope this will help.

what is the purpose of Specific line in the Code

i am reading a book named "Visual C# 2012 Programming" and i came up with following code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ch05StringManupulationEx
{
class Program
{
static void Main(string[] args)
{
string myString = "String with small s";
char[] myChar = myString.ToCharArray();
foreach (char whatever in myString)
{
Console.WriteLine("{0}", whatever);
}
Console.Write("\nyou have entered {0} characters in String ",myString.Length);
Console.ReadKey();
}
}
}
i don't know what the aurthor is doing on line :
char[] myChar = myString.ToCharArray();
because he is not using the variable named myChar anywhere in the code and even though i commented the line and compiled the program the output is same, can any one explain what is the purpose of this line in this code ?
Probably they forgot to remove that line or show what does it do, It's an array of character, A string is full of characters, each letter of a string is a character, you can access any of these arrays by using a zero based numbers, for example:
string a = "Hello";
// Prints e
Console.WriteLine(a[2]);
You can change this line to myChar to understand, It's same to an array of string, Which means a string is an array of chars, here's the example:
foreach (char whatever in myChar)
{
Console.WriteLine("{0}", whatever);
}

Categories

Resources