|
Bitmasks in C# are pretty simple, as noted you can use ushort or ulong depending on how many bits you need. That's probably the closest to C and gives you rapid conversion. However it's a good idea to avoid magic numbers and use constants instead. Less you get run over by the proverbial train and some poor soul has to figure out the rationale for your bit manipulations.
As an alternative you can also define your Bit Mask as a class with an enum. In c# an enum is equivalent to a uint, i.e. 32 bits. Thus you can mask off the high order bytes or take advantage of the extra bits for additional states. Normally I would only use a class if the bit manipulations were esoteric or voluminous. Nevertheless here is a quick example relative to your sample above:
class MyMaskOps<br />
{<br />
public enum eMyMask<br />
{<br />
None = 0,<br />
State1 = 0x1,<br />
State2 = 0x2,<br />
State3 = 0x4,<br />
StateRed = 0x200,<br />
StateBlue = 0x400;<br />
StateOrange = 0x800,<br />
AllStates = 0xe07,<br />
Multiplier = 0x1081,<br />
AllBits = 0xffff<br />
}<br />
<br />
private eMyMask mCalc = eMyMask.None;
private eMyMask mTemp = eMyMask.None;
<br />
public eMyMask Calc<br />
{<br />
set { mCalc |= value; }<br />
get { return mCalc; }<br />
}<br />
public eMyMask Temp<br />
{<br />
set { mTemp |= value; }<br />
get { return mTemp; }<br />
}<br />
public eMyMask Xor(eMyMask msk, uint bits)<br />
{<br />
return (msk ^ bits) & 0xf;<br />
}<br />
public eMyMask And(eMyMask msk, uint bits)<br />
{<br />
return msk & bits;<br />
}<br />
public eMyMask Mul(eMyMask msk, uint bits)<br />
{<br />
uint tmp = msk * bits;<br />
return (eMyMask)tmp;<br />
}<br />
public ushort DoCalc(uint[] data)<br />
{<br />
mCalc = eMyMask.None;<br />
mTemp = eMyMask.None;<br />
<br />
for (int i = 0; i < data.Length; ++i)<br />
{<br />
mTemp = Mul(Xor(mCalc, data[i]), eMyMask.Multiplier);<br />
mCalc = Xor(mTemp, (mCalc >> 4));<br />
mTemp = Mul(Xor(mCalc, (data[i] >> 4)), eMyMask.Multiplier;<br />
mCalc = And(Xor(mTemp, (mCalc >> 4)), eMyMask.ShortBits);<br />
}<br />
<br />
ushort result = (ushort)And(mCalc, eMyMask.ShortBits);<br />
return result;<br />
}<br />
}
Mike
|
|
|
|
|
Hi Mike,
I don't play much with CRC functions, actually I took it from another project
But for sure, your sample code looks to me as the C# way I like it
Also, for doing an 8 bits CRC I don't think that a class could be done as the loop is 2 lines, but maybe the class you gave me can be extended with other CRCs. It could be interested to develop a class with most CRCs inside (maybe it already exist)
best regards.
There is no spoon.
|
|
|
|
|
How do i marshal an array of strings. I'm trying to do it as below.
I have a I C dll with a C structure I want to pass
[code]
#define MAX_KEYWORDS 50
#define KEYWORD_LENGH 20
struct KeywordListStruct
{
char ks_keywords[MAX_KEYWORDS][KEYWORD_LENGH];
};
[/code]
and a function I want to export
[code]
// ...
#define MYEXPORT_API __declspec(dllexport)
// ...
MYEXPORT_API int Tester(KeywordListStruct& testStruct)
{
int retval = 0;
// do stuff
return retval;
}
[/code]
and I want to call this from a C# app
[code]
namespace DllImports
{
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct KeywordStruct
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
Imports.KEYWORD_LENGTH)] public String keyword;
}
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct KeywordListStruct
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = Imports.MAX_KEYWORDS)] public KeywordStruct[] words;
}
public class Imports
{
DllImport("KeywordDLL.dll", EntryPoint="Tester", SetLastError=true,
CharSet=CharSet.Ansi, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern short Tester(ref KeywordListStruct rTest);
public const int MAX_KEYWORD_LENGTH = 20;
public const int MAX_KEYWORDS = 50;
}
[/code]
Which I call as follows
[code]
//...
KeywordListStruct kl = new KeywordListStruct();
//...
Imports.Tester(ref kl);
[/code]
but I get the following runtime error.
An unhandled exception of type 'System.TypeLoadException' occurred. Additional information: Can not marshal field words of type DllImports.KeywordListStruct: This type can not be marshaled as a structure field.
Can anyone help?
|
|
|
|
|
Hi friends,
Plz give me the code for calling child form in which the parent windows form dispose as the child takes the control............
Thanks,
Sandeep
+919891027854
|
|
|
|
|
You can't do that. If you dispose the parent form, all it's child die with it.
If you dispose the startup form, your entire application closes with it.
I take it you're trying to implement some kind of splash screen or login form?
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Thanx 4 ur reply.
Yes, I am trying to implement login form where the login form will disappear after the user had logged in..
Thanks,
Sandeep
+919891027854
|
|
|
|
|
Your startup form is your applications main form.
When your app starts, your main form can check to see if someone logged in. If not, show the login form. If so, continue as normal.
There's a bunch of information on this. All you have to do is search[^].
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Does someone konw how I can create a color from #ffcc00?
Something like
<br />
Color aColor = Color.FromName("#ffcc00");
Thanks
Thomas
|
|
|
|
|
Hello,
Try to explorer Color.FromArgb(int argb) method. You can convert this string to a Hexadecimal number and pass it to above mentioned method
HTH, Cheers
Maqsood Ahmed - MCAD.net
Kolachi Advanced Technologies
http://www.kolachi.net
|
|
|
|
|
FromARGB should work. Make sure to convert the hexadecimal string into the 4 color representations first.
modified 12-Sep-18 21:01pm.
|
|
|
|
|
ColorTranslator.FromHtml("#ffcc00");
|
|
|
|
|
That did the trick
Thanks
|
|
|
|
|
Hi friends I am new to windows forms applications in C#. Plz tell me the method and code to call another form with retaining values from the last form?
Thanks,
Sandeep
+919891027854
|
|
|
|
|
You may find this article[^] useful. It explains how to pass values from one form to antoher and how to create a form from a form.
My: Blog | Photos
"Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious
|
|
|
|
|
Hi,
Can anybody suggest how to write windows explorer add-ins using C# on .net frame work. e.g. writing the code for creating the virtual drive in Windows explorer. When ever windows explorer is invoked then windows explorer lists out all the drives along with should display this virtual drive. e.g XCopy, GMailDrive etc., internally these virtual drives will be some how architectured to map on some file server or some other content server.
Any suggestions or techniques, please share.
Thank You,
-Ramu.
|
|
|
|
|
I am trying to insert a record into an Access database that has a memo
field. It keeps giving me an error.
I am using C#, Access 2000 and OleDB.
what is memo equivalent type in C#?? and how to convert?
my value is in txtbox1.text now plz tell me syntax that i write to make query.
I tried to use Convert but it is not working.
Looking for ur reply..... reply asap
Day by Day Everyone is moving towards graveyard
|
|
|
|
|
What is the error message that you get?
A memo field corresponds to as string in C#, so you don't have to do any conversion at all.
---
b { font-weight: normal; }
|
|
|
|
|
Hi,
I´v got a question about the souregrid 2.
I know that I can display text in a cell vertically. But only in one way.
I want to center 180° the text. How can I do this. Please help me!!
Goldjoer
|
|
|
|
|
I am using crytal Report in C# 2005 . I want to write a progam that can allow user to add lines ,Images ,Texts and move them around at runtime . I don't know how to do it
Does anyone help me ?
Thank you in advance.
Sincerely,
|
|
|
|
|
Hello once again frends!
Well right now im stuck doing a web form...well its actually finished though the only problem i have left is how i can print my form without showing my dropdown lists and buttons.
I've come up with something though im not realy sure how to go about this. the plan is upon firing my print button (which i havnt made yet) i put the entire html into a variable (easy enough) then pass that variable into a similar page except without the dropdown lists and buttons(fairly easy)...though retreival of the html is a b*tch. quite frankly...how do i do that? is it even posible?
Sori im in such a haste...though all your post will be much appreciated.
Thanx all!
Aim small, miss small...
|
|
|
|
|
Interesting that to print your are essentially re-running the HTML through your own print logic. I'd have to agree that would be ugly but more importantly is it what you really want or do you want a screen capture of the Client Window?
The latter is pretty simple Win32 type stuff, get the application form device context: Left, Right, and size coordinates. Use a GDI calls like GetRegionData, SaveDC, etc to snatch the image, then you can either save and print or just render the image to the printer. It's probably a tad more work but at least you can design a generic screen grab and print class that you could reuse. A cool application that I have used for years is SnagIT, which does what I'm talking about but it also saves the image to your favorite file format (BMP, GIF, JPEG, etc.) Depending on how far you wished to take this you could build a mini-SnagIT for your application library.
Mike
|
|
|
|
|
My apologies for the very late reply i was on a short weekend vacation. About ur post...nice logic i thought about saving it though i havnt the sense to think it through. Then it occured to me, im not the one who will use this form its the other department (meaning computer iliterate, lazy management ). So basicaly i have to create a print function that directly prints out the form with those preferences without having to save and/or edit it. Is there posibly a way?
By the way thanks so much for reply!
Aim small, miss small...
|
|
|
|
|
hello,
i'm interested in how to get the windows color theme. i'm developing a small programm, where i need to draw my own forms. i overrided the onpaint method and the drawing works fine so far. i now want to draw some form colors depending on the actual windows colors (like the standard blue, green or silver).
does anyone know, how i can get information about this setting in c#?
best regards
pikebu
|
|
|
|
|
SystemColors and SystemBrushes may be of some use, you may also want to search for uxtheme if you want to draw themed elements.
|
|
|
|
|
i am doing here some some socket-client work in C# windows service
it is working fine for multiple clientss
now i want to limit these multiple clients to 25 for example
i want that when service starts objects for all these 25 clients
are created and when client connects it should be accepted and will not allow more than 25 clients to connect
and when client diconnects that object can be allocated to another client who requests
i am not able to make up logic how i will first create 25 objects and store them in array
and how i will be checking wether object is free or not to be allocated to another client
code:
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
listen obj;
Thread threadlisten=null;
obj=new listen();
threadlisten =new Thread(new ThreadStart(obj.startlisten));
threadlisten.Start();
}
in listen class listen.cs
public class listen
{
TcpListener server=null;
Thread tcpthread=null;
client cl=null;
public listen()
{
//
// TODO: Add constructor logic here
//
}
public void startlisten()
{
Int32 port = 3310;
IPAddress localAddr = IPAddress.Parse("192.168.0.5");
// TcpListener server = new TcpListener(port);
server = new TcpListener(localAddr, port);
// Start listening for client requests.
server.Start();
// Enter the listening loop.
while(true)
{
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
cl= new client(server.AcceptTcpClient());
tcpthread=new Thread(new ThreadStart(cl.getClient));
tcpthread.Start();
}
}
public void stoplisten()
{
server.Stop();
}
}
in client class client.cs
public class client
{
TcpClient tcpClient;
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
public client(TcpClient Client)
{
//
// TODO: Add constructor logic here
tcpClient =Client;
}
public void getClient()
{
try
{
data = null;
// Get a stream object for reading and writing
NetworkStream stream = tcpClient.GetStream();
int i;
// Loop to receive all the data sent by the client.
while((i = stream.Read(bytes, 0, bytes.Length))!=0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
// Process the data sent by the client.
string replyMsg = data;
clamdCommand x=new clamdCommand();
replyMsg=x.Command(replyMsg);
byte[] msg = System.Text.Encoding.ASCII.GetBytes(replyMsg);
// Send back a response.
stream.Write(msg, 0, msg.Length);
//Console.WriteLine(String.Format("Sent: {0}", data));
}
}
catch(Exception se)
{
}
// Shutdown and end connection
tcpClient.Close();
}
}
Ankit Aneja
"Nothing is impossible. The word itself says - I M possible"
|
|
|
|