Cannot apply bit mask - c#

I am making a horse programme. I have the horse face and wish to apply a bit mask. Only the horses eyes should be visible when it is wearing the bit mask. First I must convert the horses face to digital. For this I have a set of bits which include 0, 0, 0, and 1 for the face of the horse.
I am using C# and have broken the problem into parts:
Convert the horse's head to digital
Build a bit mask for it to wear
Put the bit mask on the horse
Convert the digital masked horse back
into graphics
At step 4 I expect only to see the horses eyes but I only see "0" which IS NOT EVEN A HORSE FACE.
Here is all of my code, please don't question my ASCII art it is not relevant to the question, besides it is a prototype the real program will have superior graphics.
//the head of the horse
string head = "# #" +
"########" +
"#O O#" +
"# #" +
"# #" +
"#= =#" +
" #====# " +
" #### ";
//digitize the horse into bits of binary
string binaryHead = head.Replace('#', '0').Replace('=', '0').Replace(' ', '0').Replace('O', '1');
long face = Convert.ToInt64(binaryHead, 2);
//make a bit mask with holes for the eyes
string mask = "11111111" +
"11111111" +
"10111101" +
"11111111" +
"11111111" +
"11111111" +
"11111111" +
"11111111";
//apply the bit mask using C#
long maskBits = Convert.ToInt64(mask, 2);
string eyesOnly = Convert.ToString(face & maskBits, 2);
//eyesOnly is "0"....WHAT??? It should be more than that. WHERE IS THE HORSE??
//It should look like this:
// "00000000" +
// "00000000" +
// "01000010" +
// "00000000" +
// "00000000" +
// "00000000" +
// "00000000" +
// "00000000";
I suspect something is wrong with the conversion, I have tried all kinds of things like converting to a byte array and formatting the string with spaces but with no luck. I am wondering if this problem might be NP-hard.

face and eyesOnly have no common 1-bits. maskBits leaves everything except for the eyes. Either swap 0 and 1, or use the ~ operator to flip maskBits. And give it a better name so that it is clear what it is a mask for: bitmaskForNotEyes.

I think the problem is -
string binaryHead = head.Replace('#', '0').Replace('=', '0').Replace(' ', '0').Replace('O', '1');
First, all '#' are changed to '0'.
Then all '=' are changed to '0'
All ' ' are changed to '0'.
Finally the eyes to '1'
So, after the conversion the head looks like this -
string head = "00000000" +
"00000000" +
"01000010" +
"00000000" +
"00000000" +
"00000000" +
" 000000 " +
" 0000 ";
Now you are doing & with it -
string mask = "11111111" +
"11111111" +
"10111101" +
"11111111" +
"11111111" +
"11111111" +
"11111111" +
"11111111";
so the output is obviously 0.

Related

Items Padding showing Different,also after same padding size in Windows Application c#

listBox1.Items.Add(item.PadRight(60) + "Quantity=" + txtteesqty.Text.PadRight(60) + "RS=" + tea_total);
listBox1.Items.Add(sb.ToString().PadRight(60) + "Quantity=" + txtomelteqty.Text.PadRight(60) + "RS=" + omelte_total);
both 'item' and 'sb.tostring()' character lenght is same which is '23' but on the listbox both listitem not at the same position.
I really Dont know whats going on why it is not at same padding Help me!

How does c# string evaluates its own value?

Why does this snippet of code
string str = 30 + 20 + 10 + "ddd";
Console.WriteLine(str);
produces 60ddd,
and this one
string str = "ddd" + 30 + 20 + 10;
Console.WriteLine(str);
produces ddd302010?
Seems like it's very simple, but I can't get my head around it.
Please, show me direction in which I can go in order to find a detailed answer.
Thanks!
The + operators in the expression you show have equal precedence because they're the same operator, hence are evaluated left to right:
30 + 20 + 10 + "ddd"
-- + (int, int) returns (int)50
------- + (int, int) returns (int)60
------------ + (object, string) returns (string)"60ddd"
Then for the other case:
"ddd" + 30 + 20 + 10
----- + (string, object) returns (string)"ddd30"
---------- + (string, object) returns (string)"ddd3020"
--------------- + (string, object) returns (string)"ddd302010"
It's because an expression is evaluated from left side to right side. In the first example 30 + 20 + 10 gives you int + string (30 + 20 + 10) - int, "ddd" - string. In the second example "ddd" + 30 is a string "ddd30" that appends "20" and "10" to it. It's all about the order (unless you have paranthesis).
It's evaluated from left to right. The first example has the numbers first, so it starts by evaluating as numbers. Then it finds out it has to evaluate as string. The second example is the other way around. It starts with string and continues with string.
Operator + has different overloads:
int + int = int
int + string = string
string + int = string
In Following Expression:
string str = 30 + 20 + 10 + "ddd";
Console.WriteLine(str);
First 30 + 20 got evaluates both are integers so output of operator will be integer which is 50.
Then 50 + 10 will be evaluated which both are again integers so integer will be output which is 60.
Then 60 + "ddd" which is integer + string operation the operator in this case output string so 60 + "ddd" will output 60ddd.
In Following Expression:
string str = "ddd" + 30 + 20 + 10;
Console.WriteLine(str);
First "ddd" + 30 got evaluates in which string + integer operation takes place so output will be ddd30.
Then ddd30 + 20 will get evaluated in which again string + integer operation takes place so output will be ddd3020.
Then ddd3020 + 10 will get evaluated in which again string + integer operation takes place so output will be ddd302010.
It happens because, the order of operations if from left to right. But assigment is last operation.
To assing value first expression must be calculated.

Creating SQL Statements from a Text File - C#

Good morning,
I´m trying to create a program to create statements in a .Sql document but i´m having some troubles.
this is my code so far:
string[] filas = File.ReadAllLines("c:\\temp\\Statements.txt");
StreamWriter sw = new StreamWriter("c:\\temp\\Statements.sql");
foreach (string fila in filas)
{
string sql = "INSERT ";
string[] campos = fila.Split(' ');
if (campos[0]== "1A")
{
sql += " INTO TABLE1 (field1) VALUES (" + campos[1] + ");";
}
else
{
sql += " INTO TABLE2 (field1,field2,field3) VALUES (" + campos[1] + "," + campos[2] + "," + campos[3] + ");";
}
sw.WriteLine(sql);
}
sw.Close();
{
the thing is:
I need to read a txt document (the lenght will change), and then transform it to a sql document with all the statements, there are only two tipes of lines starting in "1A" or "2B", example:
1A123456 456,67
2B123456 mr awesome great strt germany
1A123456 456,67
2B123456 mr awesome great strt germany
2B123456 mr awesome great strt germany
1A123456 456,67
1A123456 456,67
then im trying to "transform" that information on "inserts":
INSERT INTO TABLE1 (REF,MONEY) VALUES (A123456,456,67);
INSERT INTO TABLE2 (REF,NAME,ADR) VALUES (B123456,mr awesome,great strt);
INSERT INTO TABLE1 (REF,MONEY) VALUES (A123456,456,67);
INSERT INTO TABLE2 (REF,NAME,ADR) VALUES (B123456,mr awesome,great strt);
INSERT INTO TABLE2 (REF,NAME,ADR) VALUES (B123456,mr awesome,great strt);
INSERT INTO TABLE1 (REF,MONEY) VALUES (A123456,456,67);
INSERT INTO TABLE1 (REF,MONEY) VALUES (A123456,456,67);
my code is not working so well... i hope someone can help me a litte :).
regards.
Firstly I could not see space between 1A and 123456 . So if (campos[0]== "1A") will not work. Use contains method to do this check - if (campos[0].contains("1A"). you can alternately evaluate using startswith
Secondly you need to split 1A123456 to get A123456 .. you can use substring or similar functions for same. (Same for 2B)
Thirdly, you are splitting the string with ' ' - this could result in many more string than your anticipated strings. 2B123456 mr awesome great strt germany - in this case mr awesome great strt are all different. You need to put in logic to concatenate campos[1] & campos[2] and campos[3] & campos[1=4] in the case of 2B ....
Fourthly for the 1A case you need to split campos[1] using , as delimiter to get the two values you want
Hope this provides you enough guidance to solve your issue.
After some research and with the help from anil and Pikoh i found a good solution:
string mydate = DateTime.Now.ToString("yyyyMMdd");
string AÑO = DateTime.Now.ToString("yyyy");
string MES = DateTime.Now.ToString("MM");
string DIA = DateTime.Now.ToString("dd");
string sql = "INSERT ";
string[] campos = fila.Split(' ');
if (campos[0].StartsWith("1H"))
{
sql += "INTO TABLE (VALUES,VALUES,VALUES) VALUES (" + "'" + mydate + "'" + "," + "'" + campos[0].Substring(1, 8) + "'" + "," + "'" + campos[0].Substring(9, 7) + "'" + "," + "'" + campos[8] + "'" + ");";
Inserting data and manipulating strings was good, but now i have the last problem,
what happen if i need to make a "backspace" to an specific string because my logic cant pick the correct information? regards.

how to evenly space out information?

I have some vehicle information that I want to send in an email.
I have all code working but spacing out the information is a problem. Each vehicle has a checklist and that checklist then gets emailed. So I loop through the list and get the defect and the comment.
foreach (var item in chkList.CheckItems)
{
if (item.Defect == true)
{
defect += item.ItemTitle + " " + item.Comment + "\n";
}
}
if (hasDefect == true)
{
Utils.ChecklistSendMail("Checklist", ToAddresses.Split(';'),
"Vehicle Reg: " + reg + "\n" +
"Checklist No: " + chkList.CheckListNo + "\n"+
"Date: " + ChecklistDate.ToShortDateString() + "\n" +
"Defects: Comments: " + "\n" +
defect);
}
Email then looks like this:
Vehicle Reg: XLZ 8194
Checklist No: 0
Date: 22/03/2016
Defects: Comments:
Vehicle Secure comment1
Brakes comment2
I want the defects and the comments to be displayed like this:
Defects: Comments:
Vehicle Secure comment1
Brakes comment2
How do I evenly space out the defects and the comments?
You can use string.Format which supports automated padding:
string s = string.Format("{0,-20}{1}", "hello", "world");
Which outputs:
hello world
If you do that for every line, and you find a good distance (20 in my sample code), you will be fine. This all assumes the use of a mono-spaced font.
Use String.Format with the width specifier:
if (item.Defect == true)
{
defect += string.Format("{0,-20} {1,-10}\n", item.ItemTitle, item.Comment);
}
You could also use StringBuilder.AppendFormat if performance becomes an issue.
If you want to guarantee that the columns are aligned, even with a client that uses proportional fonts, then consider rendering as HTML and using a <table> instead.

Line up Characters

I have a combobox made up of two numbers; inches and millimetres. At the moment it is looking hideous. I am wondering if some of the gurus here have anyway of lining the character '|' or at least make it nicer?
A bit of background info, the number inches and millimetres are separate strings which I append together like so:
Size(in) + " (In) | " + Size(mm) + " (mm)"
Possibly the cleanest way would be to format every number to have 3 decimal places for at least inches. This still won't be perfect however since the letter font width won't be perfect, to fix that you'd need to use a monospaced font.
To format to 3dp you can use the following
String.Format("{0:f3}", Size(in)) + " (In) | " + Size(mm) + " (mm)"
Since you have some values that are 2 digits before the decimal you can always use PadLeft to align these, but again this doesn't always work well without a monospaced font..
String.Format("{0:f3}", Size(in)).PadLeft(5, ' ') // or (5, '0')
Use String.PadRight(i); and String.PadLeft(i); where i is a nr. of spaces to "fill":
Example:
// Just to simplify a little, create vars:
var inches = Size(in) + " (In) ";
var mm = " + Size(mm) + " (mm)";
var formatted = inches.PadRight(15) + "|" + mm.PadLeft(15);
Example of output using 15 for the padding value (obviously, you can adjust this as needed):
43 inches | 123 cm
445554 inches | 12345 cm

Categories

Resources