|
|
Hi all,
i have athe following Crc16 method. I want to use it in my C# application but am not sure how to go about it. Can anyone please put some light on this issue.
short int crc16(char *pBuf, short int Len, short int InitialCrc){
short unsigned int crc=0, i=0;
crc = 0;
for (i = 0; i < Len; i++ ) {
crc = (crc >> 8) | (crc << 8);
crc = crc ^ *pBuf;
crc = crc ^ ((crc & 0xff) >> 4);
crc = crc ^ (crc << 12);
crc = crc ^ ((crc & 0xff) << 5);
pBuf++;
}
return(crc);
}
Many thanks in advance
Regards,
The only programmers that are better that C programmers are those who code in 1's and 0's
Programm3r
My Blog: ^_^
|
|
|
|
|
First you have to decide how you want to handle the characters. A character in C++ is 8 bits, while a character in C# is 16 bits.
After that, the translation is pretty straight forward.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
|
Programm3r wrote: The other problem is how can I convert string to char*
string foo = "Hello World";
fixed (char* fp = foo)
{
var i = crc16(fp, (ushort) foo.Length, 0);
}
|
|
|
|
|
|
Programm3r wrote: Would you say that the Crc method look good after my C# interpretation?
I am not sure that it is correct, as the result (which should fit in a 16 bit number) overflowed. I have modified it slighty, best would be to test the output with the output of the native function.
static unsafe ushort crc16(char* pBuf, int Len, ushort InitialCrc)
{
ushort crc = InitialCrc;
for (int i = 0; i < Len; i++)
{
crc = (ushort)((crc >> 8) | (crc << 8));
crc = (ushort)(crc ^ *pBuf);
crc = (ushort)(crc ^ ((crc & 0xff) >> 4));
crc = (ushort)(crc ^ (crc << 12));
crc = (ushort)(crc ^ ((crc & 0xff) << 5));
pBuf++;
}
return crc;
}
|
|
|
|
|
leppie, thank you very much. It looks perfect according to my previous results done in C++ ... five from me :
The only programmers that are better that C programmers are those who code in 1's and 0's
Programm3r
My Blog: ^_^
|
|
|
|
|
You have changed the type from ushort to uint, I'm not sure that works properly.
Do you really want to use unsafe code and pointers? Why not just use a string?
The InitialCrc value isn't used in the method at all, so you should remove it.
ushort crc16(string buffer) {
ushort crc = 0;
foreach (char c in buffer) {
crc = (crc >> 8) | (crc << 8);
crc ^= (ushort)c;
crc ^= (crc & 0xff) >> 4;
crc ^= crc << 12;
crc ^= (crc & 0xff) << 5;
}
return crc;
}
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Hi,
kindly let me know that, how may I correct the following problem?
* this connection string works good :
string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\BT.XLS;Extended Properties=""Excel 8.0;HDR=YES;""";
* but this does not work by using textBox1.text :
string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ textBox1.Text +";Extended Properties=""Excel 8.0;HDR=YES;""";
error is : "; expected"
Thank you in advance
(Riaz)
|
|
|
|
|
Did you try debugging to see what is the actual value of ConnectionString after assignment?
|
|
|
|
|
He can't debug it as it doesn't compile.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Oh, I see. I thought he was getting error when he was trying to open excel workbook.
|
|
|
|
|
|
You have forgotten the @ before the second literal string.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
kindly fix the problem of my mentioned connection string that where should be place @
|
|
|
|
|
You don't understand your own code?
You have a @ delimited string which you have split into two strings, but you have forgitten to put the @ delimiter before the second string.
Example:
string s = @"A ""string"" with some ""quotes"".";
Split into two strings:
string s = @"A ""string"" with " + @"some ""quotes"".";
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
|
This should work:
To many qoutes according to me....
string ConnectionString = String.Concat(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=", textBox1.Text ,";Extended Properties=Excel 8.0;HDR=YES;");
The only programmers that are better that C programmers are those who code in 1's and 0's
Programm3r
My Blog: ^_^
|
|
|
|
|
|
You can build your string like this:
string ConnectionString = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;""", textBox1.Text)
By the way, give your controls more meaningful names.
|
|
|
|
|
Use it this way. @ comes before any string segment
string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ textBox1.Text + @";Extended Properties=""Excel 8.0;HDR=YES;""";
|
|
|
|
|
Just try this:
string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='"+ textBox1.Text +"';Extended Properties=""Excel 8.0;HDR=YES;""";
Be a good professional who shares programming secrets with others.
|
|
|
|
|
i want to make the fix sized text in text box(e.g 10 characters).
it means i made a textbox and also want that user can't enter the characters upto 10 characters..
user is bound at 10 character.
--------------------------------------------------------------------------------
maifs
maifs
|
|
|
|
|
Set MaxLength property of textbox.
-Dave.
------------------------------------
http://www.componentone.com
------------------------------------
|
|
|
|