|
Hello,
i was trying to simplify my code but i have a problem with the code here:
public void Send(byte a, byte b, byte c)
{
byte[] ab = new byte[3];
ab[0] = a;
ab[1] = b;
ab[2] = c;
ab[3] = a ^ b ^ c;
serialPort1.Open();
serialPort1.Write(ab, 0, 3);
serialPort1.Close();
}
When is use this code there is an error in this line:
ab[3] = a ^ b ^ c;
the error is:
Error 1 Cannot implicitly convert type 'int' to 'byte'. An explicit conversion exists (are you missing a cast?) C:\Users\***\AppData\Local\Temporary Projects\WindowsFormsApplication1\Form1.cs 27 21 WindowsFormsApplication1
Is there some one that can help me?
|
|
|
|
|
Whenever you use the xor operator (^) (and any of the other operators to) it treats everything as an int .
Change it to
ab[3] = (byte)(a ^ b ^ c);
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Thank you for your fast reply it solved my problem! thank you very much!
|
|
|
|
|
sounds like the resultant value produced is out side byte range and hence overflow that's why its asking for explicit conversion. Make sure the values you passing for a,b and c produce a value within byte range.
|
|
|
|
|
it does, because its sending the result to my serialport, so the value is never bigger then 255.
Thanks for your help!;)
|
|
|
|
|
How exactly would a xor overflow?
|
|
|
|
|
byte[] ab = new byte[3];
ab[0] = a;
ab[1] = b;
ab[2] = c;
ab[3] = (byte)(a ^ b ^ c);
Warning: "index out of bounds" is imminent
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
I know that but thank you for the anouncement;) im very happy that you guys helped me out!
|
|
|
|
|
Following on to musefan's point, you have a subtle error in your declaration of the byte array. In this line:
byte[] ab = new byte[3]
You declare ab as a byte array which can only hold three elements. However, in the lines below it, you fill it with four elements; as has been said, this will cause an IndexOutOfRangeException (or something like that) when you assign a value to ab[3]. To fix this problem, change the declaration of ab to
byte[] ab = new byte[4]
This will allow ab to hold four elements, fixing the error
Between the idea
And the reality
Between the motion
And the act
Falls the Shadow
|
|
|
|
|
Hi,
this is what I would write:
List<byte> list=new List<byte>();
list.Add(a);
list.Add(b);
list.Add(c);
writeWithXor(port, list);
...
public void writeWithXor(SerialPort port, List<byte>list) {
int xor=0;
foreach(byte b in list) xor^=b;
list.Add((byte)xor);
port.Write(list.ToArray());
list.Clear();
}
This way you don't have the hassle of counting bytes and allocating the right array; and you can reuse
writeWithXor() for any amount of data that needs to be followed by a 'checksum'; and of course I would keep the serial port open, probably as long as my app is running.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
I want 2 create a component like msn status lable.
that look like a label & a textbox & a dropDownList.
it can drop down show the menu,and it can be edited when I click the label.
how to do that?
|
|
|
|
|
You make a user control with a Label, TextBox and ComboBox
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Most probably you will make a user control.
|
|
|
|
|
|
this question is a mixture of sql and c# windows services...
i have a windows service which connects to COM1 port(serial port) and writes the data into a sql db. i have an installer which installs the service and run it immidiatly after setup is completed , but stops very soon because of this error which appears in event log :
provide : named pipes provider . error 40 - could not connect to sql server
i have installed sql server 2005 (not express ed) and the connection string
in my service is :
string _connectionstring =
"server=" + Environment.MachineName +";Initial Catalog=myDatabase;"+
"user id=myLogin;password=myPassword;trusted_connection = true;";
i have tested this connection string in a simple windows form and it works!
please notice that this sql server and windows service are working on local computer , not a network.
|
|
|
|
|
check that Environment.MachineName is returning the correct value you need, i.e. the SQL Server location. The other information in your connection string should be irrelevant at this time, as with the error you have the issue is usually because is pointing to wrong place
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
I think you may have a conflict in your connection string. You are providing a user name and password but also specifying trusted_connection=true. My assumption is that the service is trying to logon to the SQL server as the user the service is running under. Try changing trusted_connection = true to trusted_connection = false.
|
|
|
|
|
Would that not be an authentication error instead thou?
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
Quite possibly but I couldn't count the number of error messages I've seen that don't really relate to what the problem is.
|
|
|
|
|
very true
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
hi..
Can anyone have idea about "datafile" in ACOS3-24 smart card,
i have to write and data in it using C# SDK.
i analysed that each datafile can contain upto 255 data records,but i m confused that how much "datafile" can we make. and what is max capacity (no of bytes)each "datafile" can contain.
Plz reply anyone who have some idea,
plz provide any help regarding this whatever anyone know about this.
Thanx.
|
|
|
|
|
I'm afraid, unless you are VERY lucky, that the knowledge you want is not available here. You would be better off going to the manufacturers of the device or SDK.
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|
|
|
Hi.
Could you please guide me, how we can convert an IntPtr object to an Image object ?
thanks.
|
|
|
|
|