|
I would like to convert a integer into a byte-array:
0x1234 ->
b[0]=1, b[1]=2 ... b[3]=4
How is this to be done?
Thanks!
Ariadne
|
|
|
|
|
So, looking at your example, what you want is to take, in groups of 4 bits, the integer and place it in to a byte array in big-endian ordering? So, that for a System.Int32 you would get byte array containing 8 elements? Is this right?
"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell
Not getting the response you want from a question asked in an online forum: How to Ask Questions the Smart Way!
|
|
|
|
|
I'm seek for two solutions:
(1) How can I address the particular bytes of a int variable
(2) I need finaly an byte-Array with
int = 0x1234
byte [3] -> [0]
int 0x1 -> 0x4
Ariadne
|
|
|
|
|
I'm guessing that you're bytes are merely representational to your integer, because the integer 0x1234 is actually only 2 bytes large (2 hex digits are a byte). For that, you can use BitConverter.GetBytes .
If it is representational, you'll need to parse it yourself. You could first transform the number to a hex string and then parse each numeric value to a byte:
string val = 0x1234.ToString("x");
byte[] buf = new byte[val.Length];
for (int i=0; i<val.Length; i++)
buf[i] = (byte)((int)val[i] - 48);
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles]
|
|
|
|
|
Hi..
I am trying to write a windows service and this service have some jobs which must be done just only once at a day. My windows service read a file,which all job info stay in the file, when it starts up and create job objects and push them to a array, named Jobs.
Every job object has;
-->time
-->ok (default:false)
-->job to do.
Job[0] -----> 14:50<br />
Jobs[1] -----> 13:24 <br />
Jobs[2] -----> 13:12<br />
.<br />
.<br />
.
So I put a timer that checks if its is the exact time for every job. the interval of the timer is minute (1000ms). if any of Jobs is done the ok flag turns to true. And the job never done again. When the clock show 24 every thing starts again..
But there is smthg wrong that I could not found.
1. I could not debug the timer events. I attach the process to vs.net to see what is going on but the timer event code confused me that it smtimes turn over and over. How could I debug a timer code
2. If the two or more jobs times are same what will happen. Must I use thread to solve this problem. How could I use a timer and threads together?
karanba
|
|
|
|
|
karanba wrote:
1. I could not debug the timer events. I attach the process to vs.net to see what is going on but the timer event code confused me that it smtimes turn over and over. How could I debug a timer code
1000 ms is 1 second, not 1 minute. 60000 ms is 1 minute. There's nothing special about debugging a timer event. All you need to do is attach the debugger to your running service.
karanba wrote:
2. If the two or more jobs times are same what will happen. Must I use thread to solve this problem. How could I use a timer and threads together?
This all depends on how your checking to see what jobs need to be run and how your executing these jobs and what they are. I'm assuming your doing this inside your timer event, but it all depends on how your doing this.
Without a code sample, it's impossible to tell you what might happen or what you have to do.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
thanx the 10000 to 60000 ms solve the first problem.
on the second issue;
the timer code try to read an url and smtimes this could be long than 2 minute to get a url.
here is the job instructor
public job(string url, string ms, int tp)<br />
{<br />
this.url = url;<br />
this.type = tp;<br />
this.ok=false;<br />
if(tp==1)<br />
{<br />
timer = new System.Timers.Timer();<br />
timer.Elapsed+=new ElapsedEventHandler(OnTimedEvent); <br />
timer.Interval=System.Convert.ToInt32(ms);<br />
this.interval = System.Convert.ToInt32(ms);<br />
timer.Enabled=true;<br />
}<br />
else if(tp==0)<br />
{<br />
this.onTime = System.Convert.ToDateTime(ms); <br />
}<br />
}
and the OnTimedEvent
private void OnTimedEvent(object source, ElapsedEventArgs e)<br />
{ <br />
extras.readURLfromUri(this.url);<br />
}
I want to be sure that if two read url request come on the same time both will done as posible as the CPU and network is available.
karanba
|
|
|
|
|
Hi there. I am having trouble in C# trying to get from a mapped network drive letter to an actual network machine name. Is there a method somewhere to performn this conversion.
Cheers,
Brian.
|
|
|
|
|
There are several ways, but nothing exposed in the .NET Base Class Libraries (BCL). C#, keep in mind, is just one of many languages that target the CLR and can access the BCL.
One way is to P/Invoke QueryDosDevice . The output is perhaps not quite the format you are looking for, but is the actual mount point for a drive:
using System;
using System.Runtime.InteropServices;
class Test
{
const int MAX_PATH = 260;
static void Main(string[] args)
{
if (args.Length != 1) Environment.Exit(-1);
string path = new string('\0', MAX_PATH);
QueryDosDevice(args[0], path, MAX_PATH);
Console.WriteLine("Mount point: " + path);
}
[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
static extern int QueryDosDevice(string name, string path, int max);
} Paste this to a file, compile it with csc.exe /t:exe <Filename.cs>, and run it on the command line, passing a drive mapping in (like U:, if U: is your user profile drive, for example). Do not include a trailing backslash (i.e., don't pass "U:\").
There are many other functions documented in the Platform SDK. I encourage you to try searching.
If you need help constructing Platform Invoke (P/Invoke) methods for native functions, take a look at http://pinvoke.net[^] or as here.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles]
|
|
|
|
|
There is nothing native in the .NET BCL that'll do this for your. You'll have to call into (P/Invoke) the Win32 WNet (Windows Networking) API to get this information. Everything will come from the MPR.DLL file.
To enumerate the connected resources (mapped drives), you'll need to use the WNetOpenEnum , WNetEnumResource , and WNetCloseEnum functions. You'll also need to supply the structures used, NETRESOURCE , and the values for the constants used by WNetOpenEnum .
You can pickup the declarations for the functions, structures and constants from PInvoke.net, here[^].
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Back in my C++ days, I relied on BoundsChecker to help identify potential memory leaks and resource leaks since I write mainly server-based services that run 24x7.
With .NET, managed code and garbage collection, is there still a need for these third-party tools? What's your experience?
Also, can someone explain this...I have a console .NET application that starts up taking around 9K of memory. By the time everything gets loaded, Task Manager shows it taking around 20K. Fine. But when the console window is minimized, the memory (as reported by Task Manager goes down to like 3K! WTF?!
Barry Etter
|
|
|
|
|
Well the suite that BoundsChecker is in is changing to more of a profiling and performance tool. They actually have a free profiler (community edition) for VS.Net. Its not the best, but its not bad either. Compuware [^]
As for you memory question, if you look at any application, esp VS.Net when its up and then minimize it and there will be a difference in the memory used. I think that this is due to the OS getting rid of some of the memory pages that it does not need. I have observed this with all windows apps, not just console apps.
Steve Maier, MCSD MCAD
|
|
|
|
|
Yes there is such a use, but a CLR profiler is a better choice. Many classes in the .NET BCL (and third-party libraries) rely on native resources. These are unmanaged resources because the CLR does not manage them. File handles are a good example. FileStream (actually, any String ) implements IDisposable because there may be a file handle (there is for a FileStream ) or some other native resource that must be closed when finished. You must call the IDisposable.Dispose implementation when finished with such an object. If you are a class designer, you should follow the dispose pattern discussed in the .NET Framework SDK and other articles:
class MyObject : IDisposable
{
~MyObject()
{
Dispose(false);
}
void IDisposable.Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
}
}
} Unmanaged resources will be freed aventually. There is no gaurantee of when the GC runs, however, and you should almost never call GC.Collect (repeated calls can greatly hinder the performance of your application). This is why IDisposable exists. The using block statement is good to make sure that - even in case of exception - your objects are disposed:
using (FileStream file = File.Open("file.txt", FileMode.Open))
{
} Is the same as:
FileStream file = File.Open("file.txt", FileMode.Open));
try
{
}
finally
{
if (file != null) ((IDisposable)file).Dispose(0;
} So, exceptions will still be thrown, but at least the file is disposed and the native file handle is released.
So, in a round-about way, you should be able to see that while objects are garbage collected, resources they use (i.e., unmanaged resources) are not. Using a profile will help you find those problems, although it's a good rule of thumb to always dispose of any object that implements IDisposable anywhere up the class inheritance hierarchy.
Profilers can also help you find bottlenecks in your code; any platform is susceptible to that since it's typically human fault.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles]
|
|
|
|
|
Hi People,
I have a problem with changing the BackColor of a System.Windows.Forms.DataGrid cell. Actually I don't want to change the whole Style of the DataGrid - what I want is to change just the backcolor of an exact Cell that corresponds to certain requirements.
Does anyone knows how can this be done. I have looked in the MSDN and I'm starting to believe that this is not possible.
I would be grateful for any help.
|
|
|
|
|
hi,
I wrote an article for datagrid and its custom formattings. Please go through the entire article. Sure you will get the answer. Your requirement is mentioned in the third section of my article.
Take idea from that and try to impliement in your application.
You can view the article infomation on my profile.
http://www.codeproject.com/csharp/Apply_DataGridTableStyle.asp
**************************
S r e e j i t h N a i r
**************************
|
|
|
|
|
Hi Sreejith Nair,
really thank you for your fast answer. I downloaded your sourse and there i found everything I need. Really much obliged for your help
Kamen
|
|
|
|
|
I have a web application running on a server which will authenticate a user against a user name and password. Once authenticated this should pass a url to another server with the username and password as querystring. These two application are different. I tried using server.transfer but it does not work and i came to know it will not work between two different application in two different servers. I also donot want the query string to be visible as it is not secure. can any body help. response.redirect reveals the passwords. i also cannot encrypt the password as the second application does not know that the password is encrypt. i have no control over the second web application. It is a 3 rd party stuff.
any help appreciated
anand
|
|
|
|
|
hi,
i think you will instant answers once if you submit thsi queary on Asp.Net forum.
**************************
S r e e j i t h N a i r
**************************
|
|
|
|
|
Hi. I need to add the help to my application. I want to add a chm file. I'm trying with HelpProvider, but it only shows the help pressing F1. How can I show it pressing a main menu item?
Regards,
Diego F.
|
|
|
|
|
hi,
Eg 1.
Write this much in code in your menu event handler
System.Diagnostics.Process.Start(Hlp.HelpNamespace.ToString());
Eg 2.
You can use app.config file to store your .chm file and then try call your .chm file through starting a process.
System.Diagnostics.Process.Start(ConfigurationSettings.AppSetting[0].ToString())
Both are using Process.Start() method to call your external .chm file.
**************************
S r e e j i t h N a i r
**************************
|
|
|
|
|
Thank you, but where is the dll to use System.Process? I can't find it in Visual Studio 2003 Professional.
|
|
|
|
|
hi,
System.Diagnostics.Process.Start(Hlp.HelpNamespace.ToString());
if you are trying to show help using app.config then you need to add a namespace called
using System.Configuration;
And then you can use like this
System.Diagnostics.Process.Start(ConfigurationSettings.AppSettings["helpfile"].ToString())
**************************
S r e e j i t h N a i r
**************************
|
|
|
|
|
Thank you! It's working now 
|
|
|
|
|
You don't need the .ToString() calls. Both HelpNamespace and AppSettings[] return Strings to begin with. Why execute .ToString() on a String object?
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I would like to cretate excel files with my app written in C#, is it possible?
Does someone have an example?
thanks
|
|
|
|