|
|
The convert class can also be used for this:
Convert.ToString(number, base)
where base can be 2 (binary), 10 (decimal) or 16 (hexadecimal).
|
|
|
|
|
Thanks for your reply.
If you just convert it as you have shown it Eliminates the "0".
Say for example you have to convert 0000111111111000 into Hex.So it will show FF8 instead 0FF8
Well I was able to over come that by doing following:
string MyBinary = "0000111111111000";
string MyHex = String.Format("{0:X4}", Convert.ToInt32(MyBinary, 2));
|
|
|
|
|
When I need padding I use the string.PadLeft method. A couple of functions like this are all that's needed.
public static string ToHexString(int value)
{
return ToHexString(value, 0);
}
public static string ToHexString(int value, int bytes)
{
string result = Convert.ToString(value, 16);
if (bytes > 0)
result = result.PadLeft(bytes * 2, '0');
return result;
}
|
|
|
|
|
int b = 15;
string s=b.ToString("X8");
I advice to always specify the width you want; padding zeroes will be prefixed when necessary.
|
|
|
|
|
Hi!!
Its good to be back...
I'm working on a simple client server program... server waits for connection and client sends request for connection sort of... simple...
On the same m/c
but when i start my program... it hangs... it displays all msgs and then hangs.. doesnt respond to anything... then i start my client it connects to the server but server aint responding... i'm using windows forms in place of console app as per tutorials... plz help me...
should i attach my code here???
|
|
|
|
|
Have you got a firewall enabled?
Attach snippets of your client and server code so we can see what you're doing.
|
|
|
|
|
check your server call back function is correct or not..,
i think that is the problem, that's way you are able the receive the data for very first time of each new client.
Rajesh B --> A Poor Workman Blames His Tools <--
|
|
|
|
|
Whats hanging? The server or client application?
Are your listeners and tcp stuff in seperate threads or just running on the UI thread?
Are you using blocking or not blocking methods?
You really need to give us some code to look at here...
|
|
|
|
|
you should not execute blocking calls on the main thread; either use asynchronous methods, or better yet apply proper threading. There are some CP articles on threaded client-server systems.
|
|
|
|
|
Is the listener running in its own thread? It should be.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
This is the complete source code for my server side program... on clicking the button it calls func conn_man.. on clicking it this program hangs...
b4 clicking it.. the program is responding n i can move it resize it n stuff.. but after the button is clicked its not responding.. plz.. tell me how to get it working..
on receiving the connection from client it searches for a free port on the server it self.. when found it sends the client the free port to connect to[ function route does it]..
master_slave is another form which is called on accepting a connection from client
<pre>
namespace master
{
public partial class main_form : Form
{
public int port;
public main_form()
{
InitializeComponent();
activity_win.AppendText("Master On...");
}
public void conn_man()
{
try
{
//activity_win.AppendText("In conn_man...");
IPAddress ipAd = IPAddress.Parse("192.168.1.2");
// use local m/c IP address, and
// use the same in the client
/* Initializes the Listener */
TcpListener listener = new TcpListener(ipAd, 5000);
/* Start Listeneting at the specified port */
listener.Start();
activity_win.AppendText("\r\nThe server is running at port 5000...");
activity_win.AppendText("\r\nThe local End point is :" + listener.LocalEndpoint);
activity_win.AppendText("\r\nWaiting for a connection.....");
Socket s = listener.AcceptSocket();
activity_win.AppendText("\r\nConnection accepted from " + s.RemoteEndPoint);
byte[] b = new byte[100];
int k = s.Receive(b);
activity_win.AppendText("Recieved...");
activity_win.AppendText(ASCIIEncoding.ASCII.GetString(b));
if (route(5000) == 0)
{
activity_win.AppendText("No port free");
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes("Busy"));
}
else
{
port = route(5000);
new master_slave(port);
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes("Port " + port.ToString()));
}
/* clean up */
s.Close();
}
catch (Exception e)
{
activity_win.AppendText("Error..... " + e.StackTrace);
}
}
public int route(int port)
{
try
{
TcpClient scnr = new TcpClient("localhost", port);
return port;
}
catch (Exception e)
{
activity_win.AppendText(e.ToString());
port += 10;
route(port);
return 0;
}
}
private void Begin_Click(object sender, EventArgs e)
{
conn_man();
}
}
}
</pre>
|
|
|
|
|
The same code is working fine as a console app... i changed a lil bit in route func.. due to an logical problem... rest is same.. its working in console app... Y???
Wats the error or problematic statement in this code...
|
|
|
|
|
Why are you doing everything on the primary thread?
|
|
|
|
|
can u plz tell me... the number of threads n what shud it do?? for my program...
|
|
|
|
|
Dear All
ConfigurationManager.GetSection("Section1") only retrieve the default app.config's section information and it returns a object that can be casted. Now i have a specified configuration that used by my application. I have loaded the specified configuration file into the memory, please see the below code:
this is the specified config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="Section1" type="CortexShell.DisplayModeSectionHandler,CortexShell" />
</configSections>
<Section1 DisplayMode="MonitorDisplay" Timeout="300" />
</configuration>
this is the c# code:
Configuration config = ConfigurationManager.OpenExeConfiguration(@"c:\AppState.config");
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = path;
config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
DisplayModeSection _displayMode = (DisplayModeSection)config.GetSection("Section1");
Now the config.GetSection("Section1") only returns the specified configuration.ConfigurationSection object, hence when i compile it, this is the exception: Cannot convert type 'System.Configuration.ConfigurationSection' to 'CortexShell.DisplayModeSection', my question is how can i convert System.Configuration.ConfigurationSection to CortexShell.DisplayModeSection (this is my customised config section in the specified config file)
Thanks very much
|
|
|
|
|
|
Smart Thread Pool[^] supports cancelation of queued items.
Harvey Saayman - South Africa
Software Developer
.Net, C#, SQL
you.suck = (you.Occupation == jobTitles.Programmer && you.Passion != Programming)
1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111
|
|
|
|
|
|
Glad to be of service
|
|
|
|
|
The standard ThreadPool holds threads for you to use; you never get full control over those threads, so there are a lot of managerial operations you can't perform on them, such as changing their priority, or aborting them. What you can do is implement a co-operational abort, say a while loop where the outside changes the expression or variable that is going to be tested.
public void myThreadAction(...) {
while(!threadStop) {
... go on
}
}
The net result most often is the "abort" is not instantaneous, the while loop may (and should) call some blocking method (some input/output operation, or a Thread.Sleep).
BTW: that is also the way you should do it on normal threads, a hard abort is to be avoided as one never knows in what state objects (such as locks, unmanaged resources) will be at the moment of the abort.
|
|
|
|
|
Luc Pattyn wrote: a hard abort is to be avoided as one never knows in what state objects (such as locks, unmanaged resources) will be at the moment of the abort.
Sorry any elaboration on this...? I can't see my calculation threads has any "State" to it... and my guess is for all practical reason I can throw an exception in the thread just to kill it. Am I missing something, Abort vs Interrupt?
dev
|
|
|
|
|
you'll have state whenever some I/O is included, or something gets locked (e.g. a shared results queue), etc. Here is an excellent article on threading: http://www.albahari.com/threading/[^]
My conclusion is: choose the co-operative way whenever you can.
|
|
|
|
|
Hello everyone,
I am designing a simple convertor, that will take an ammount and convert it to a different unit.
the uits that one can convert between are:
moles
millimoles
grams
milligrams
mirolitres
millilitres
The user enters an amout, selects its unit, and then selects a unit to convert to, and the new value is output onto the winform.
Now, should I write individual conversion functions, and maybe use a case to decide what unit we have, and then call the required converter.
Or, should I have an overloaded function and pass something in to let it decide how to convert?
Any other advice would be greatly appreciated.
Thank you
Steve
|
|
|
|
|
Hmmm...
You should probably have an enumeration that contains the supported units and have one method that takes the magnitude and two units.
But you could also provide separate methods and the above method could just be a switching station -- but with Dictionaries, not switch es.
private static Dictionary<FromUnit,Dictionary<ToUnit,delegate>> dic ... ;
dic [ Unit.Grams ] = ... ;
dic [ Unit.Grams ] [ MilliGrams ] = ConvertGramsToMilliGrams ;
...
return ( dic [ FromUnit ] [ ToUnit ] ( Magnitude ) ) ;
Actually, the populating of the Dictionary can be automated -- perhaps create an Attribute to decorate your methods:
[UnitConvertorAttribute ( From , To )]
public double ConvertGramsToMilliGrams ( double Magnitude , Unit From Unit To ) ...
|
|
|
|