|
|
|
|
Why are you writing to LocalMachine?
Do you have the permissions nessessary for this?
Cant you write to CurrentUser?
Will
|
|
|
|
|
I have an array of bytes which i need to get bits from in 5-bit chunks.
e.g. lets say my byte array has 4 values
0 - 10101010
1 - 10101010
2 - 10101010
3 - 10101010
i would like an array of strings (or whatever, could be bytes with leading 0) that returns to me the values
0 - (000)10101 (first 5 bits of 0)
1 - (000)01010 (last 3 bits of 0 and first 2 bits of 1)
2 - (000)10101 (mid 5 bits of 1)
.
.
so on
How can I do this in C#?
Thanks
|
|
|
|
|
Since that's only 32 combinations, why not use a lookup table? Then all you need to do is some bit shifting and and'ing.
Marc
MyXaml
Advanced Unit Testing
YAPO
|
|
|
|
|
Thanks Marc, this seems to do the trick but if any1 has a suggestion to make this process faster and less complicated then go right ahead.
private byte[ ] EncodeArray( byte[ ] parentArray )
{
byte[ ] retArr = new byte[ parentArray.Length * 8 / 5 ];
int i = 0;
int pos = 0;
while ( i < parentArray.Length )
{
if ( i % 5 == 0 )
{
byte mask = 0xF8;
int result = parentArray[ i ] & mask ;
retArr[ pos++ ] = ( byte ) result;
mask = 0x7;
byte mask2 = 0xC0;
int result1 = parentArray[ i ] & mask ;
int result2;
if ( i + 1 < parentArray.Length )
{
result2 = parentArray[ i + 1 ] & mask2 ;
}
else
result2 = 0;
result = ( result1 << 2 ) + ( result2 >> 6);
retArr[ pos++ ] = ( byte ) result;
}
else if ( i % 5 == 1 )
{
byte mask = 0x3E;
int result = parentArray[ i ] & mask;
retArr[ pos++ ] = ( byte ) result;
mask = 0x1;
byte mask2 = 0xF0;
int result1 = parentArray[ i ] & mask;
int result2;
if ( i + 1 < parentArray.Length )
{
result2 = parentArray[ i + 1 ] & mask2 ;
}
else
result2 = 0;
result = ( result1 << 4 ) + ( result2 >> 4 );
retArr[ pos++ ] = ( byte ) result;
}
else if ( i % 5 == 2 )
{
byte mask = 0xF;
byte mask2 = 0x80;
int result1 = parentArray[ i ] & mask;
int result2;
if ( i + 1 < parentArray.Length )
{
result2 = parentArray[ i + 1] & mask2;
}
else
result2 = 0;
int result = ( result1 << 1 ) + ( result2 >> 7 );
retArr[ pos++ ] = ( byte ) result;
}
else if ( i % 5 == 3 )
{
byte mask = 0x7C;
int result = parentArray [ i ] & mask;
retArr[ pos++ ] = ( byte ) result;
mask = 0x3;
byte mask2 = 0xE0;
int result1 = parentArray[ i ] & mask;
int result2;
if ( i + 1 < parentArray.Length )
{
result2 = parentArray[ i + 1] & mask2;
}
else
result2 = 0;
result = ( result1 << 2 ) + ( result2 >> 5 );
retArr[ pos++ ] = ( byte ) result;
}
else if ( i % 5 == 3 )
{
byte mask = 0x1F;
int result = parentArray [ i ] & mask;
retArr[ pos++ ] = ( byte ) result;
}
i++;
}
return retArr;
}
|
|
|
|
|
This seems overly complex and there are some errors. It seems that the masks should go:
F8, 7C, 3E, 1F
Also, there's two tests for (i % 5) == 3
Instead of changing the mask, why not shift the byte under test and 'or' in the necessary bits from a look ahead byte (which is 0 if at the end of the array). It's a bit complicated (no pun intended) because you have to figure out when to get the next byte, but you should be able to do the whole thing in a loop.
Marc
MyXaml
Advanced Unit Testing
YAPO
|
|
|
|
|
Have a look at the BitArray class. You can feed it with a byte array and afterwards pick the bits one after the other:
BitArray ba = new BitArray(b);
int counter = 0;
string temp = "";
while (counter < ba.Length)
{
int mod = counter % 8;
temp += ba[counter - 2 * mod + 7] ? "1" : "0";
if (counter % 5 == 4)
{
Console.WriteLine(temp);
temp = "";
}
counter++;
}
if (temp.Length > 0)
Console.WriteLine("Left: " + temp);
The formula when accessing the BitArray indexer seems a bit complicated and results from the fact that the bits from each byte put into the constructor seem to be reversed.
|
|
|
|
|
Is it just me or does everyone feel cheated when you don't get a feedback wether a solution worked or not? Maybe I am just small...
|
|
|
|
|
Whats even worse is that you sometimes dont even get to know if your answer was read or if you have wasted your time because the answer wasnt even read by the questioner.
|
|
|
|
|
Exactly!!
|
|
|
|
|
niansah wrote:
Is it just me or does everyone feel cheated when you don't get a feedback wether a solution worked or not?
It is good to know. Especially given that some people's command of the language isn't so great and I wonder whether I interpreted their original question correctly in the first place.
niansah wrote:
Maybe I am just small...
No. Feedback is important in all areas. You cannot improve without feedback. Imagine a baby learning to walk. Without the feedback of the sensation of hitting the floor when they stumble they won't know what moves work and what don't.
Answering questions is great as a learning tool and to keep information going. It is amazing how much more information I can retain by helping others. Mainly because the problems they have may not be in the same area I am currently working on, so by thinking about the area they are asking about keeps the information fresh in my mind, so that if I move back to that area I can recall it much quicker.
Sometimes I feel that I can't be bothered answering questions here. If I feel really bad about not being appreciated or that I'm being used I just leave it a couple of days then come back.
So, don't feel discouraged about people not leaving feedback on your answers. There are many people here who genuinely appreciate the efforts made by those who share knowledge.
My: Blog | Photos | Next SQL Presentation
WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More
|
|
|
|
|
Hi,
I am trying to get all the farm name attrbute from Xml files thorugh loop
I have to verify conditoion that when farm name is equal to dev1
It should exit from the loop
my code retrieves only one value from farm name dev2 my code does not search for farm name dev2
any body can help me giving me code
XmlDocument doc = new XmlDocument();
doc.Load("c:/farm-config.xml");
XmlNode farm=doc.SelectSingleNode("/root/farm-config/farm/@name");
MessageBox.Show(farm.InnerText);
xml file strucutyue is as given below
Hi,
I am trying to get all the farm name from Xml files.
This is my xml file structure
<?xml version="1.0" encoding="UTF-8"?>
<root>
<farm-config>
<farm name="dev2">
<connection address="localhost1" user="root1" selectedConnection="1" password="daask6">
<lastJob>Copy-All</lastJob>
<lastConnection startdate="2005-05-10" starttime="12:43:23" enddate="2005-05-10" endtime="12:58:32" />
<state>1</state>
</connection>
</farm>
<farm name="dev1">
<connection address="localhost1" user="root1" selectedConnection="1" password="daask2">
<lastJob>Copy-All</lastJob>
<lastConnection startdate="2005-05-10" starttime="12:43:23" enddate="2005-05-10" endtime="12:58:32" />
<state>1</state>
</connection>
</farm>
</farm-config>
</root>
regards
sanjeev
|
|
|
|
|
Well, given your query, InnerText would only return a result if:
<farm name="dev2">Some text</farm>
You may want InnerXml instead.
Marc
<a href="http://www.myxaml.com">MyXaml</a>
<a href="http://aut.tigris.org">Advanced Unit Testing</a>
<a href="http://wdevs.com/Default.aspx?tabid=80">YAPO</a>
|
|
|
|
|
hi!
Perhaps I've misunderstood you, but XmlNode.SelectSingleNode will always return a single node.
If you want several nodes you should use XmlNode.SelectNodes()...
mav
|
|
|
|
|
XmlDocument doc = new XmlDocument();<br />
doc.Load("c:/farm-config.xml");<br />
XmlNodeList farm=doc.SelectNodes("//farm");<br />
foreach(XmlNode n in farm)<br />
{<br />
if (n.Attributes.GetNamedItem("name").Value == "dev1")<br />
MessageBox.Show(n.Attributes.GetNamedItem("name").Value + " is a match!" );<br />
else<br />
MessageBox.Show(n.Attributes.GetNamedItem("name").Value + " is not a match!" );<br />
}<br />
|
|
|
|
|
I have the following situation:
Person A performs an application action that affects Person B. Management wants the application to automatically generate an e-mail to Person B, notifying them of the action. I can think of two solutions. 1) Query the Active Directory LDAP server for the e-mail address tied to Person B. 2) Get a dump of the e-mail addresses from the Exchange administrator and tie it into the application like another table.
I don't like #2 because it doesn't account for change very well and I don't want to implement ANOTHER import/reconcile process if I don't have to. However, I've never done LDAP work and don't know how to exactly go about "eating this elephant". The proverbial answer is "one bite at a time", but where to start?
BTW: The application is ASP. NET. The domain is Win Server 2000, Exchange 2000, and SQL Server 2000.
Suggestions, pointers, hints, and help are welcome.
Thanks in advance,
Darien
"I don't know. I haven't tried today." - Trumpet icon Maynard Ferguson's response when asked how high he could play.
|
|
|
|
|
Look at the System.DirectoryServices namespace. You can use a DirectorySearcher object and get the user account. The property to look for is "mail". I just did a phone/email list for a company last week. I don't have any of the code, but if you get stuck on something, I might be able to help.
When getting the value of a property from a DirectoryEntry object, always do a .Contains("prpertyname") before trying to access the property. Not every entry will have the property you are after.
|
|
|
|
|
Hi:
I've been told by people in the know, that the accepted way of storing program data (like screen coordinates, directory settings, etc.) in the .NET world is with XML files (as opposed to the registry or old fashioned INI files).
OK, I'll buy that. I have managed to create a simple XML file that writes two program settings, Start Directory and the maximum number of files. I threw in a comment just for the hell of it.
<datalocations startdir="C:\Marks\DataFind\GPS\" maxfiles="100">
What I would like to know is if it will be possible to read and alter this file in a random-access fashion. I was hoping that there would be some analogy to the old INI file approach where you had a key, a subkey, the value and possibly a default value if the key was not found. You could request to read or alter the settings of specific keys without disturbing the others and without having to read through the file sequentially to get to the key you are looking for.
I am just a bit overwhelmed by the number of XML classes. Admittedly, I do not know XML all that well, but I will say that, if I can help it, my file will not get any more complex than the sample I have provided above. There will be additional entries but I see no need for further nesting and "noding".
For now, I just want to be able to quickly read and update the StartDir and MaxFiles fields of the file without having to resort to a sequential read. Can this be done? Could you point me to the class or classes that would be most useful?
Thank you very much in advance,
Jethro
|
|
|
|
|
Try to search for application settings xml here on CodeProject.
You will get several hits and you are free to choose an existing solution for your problem.
|
|
|
|
|
Hi Robert:
Thanks, I can see that there are several useful articles there. I'll have a look.
Cheers,
Jethro
|
|
|
|
|
|
Hi,
For sequential read you would use the XmlTextReader to read your file. This is forward only and extremely fast. You would "pull" the information out in a while loop.
For random access, I would use XPathDocument and navigator.
Information on the above is well documented, just google for it.
...INI file approach where you had a key, a subkey, the value and possibly a default value if the key was not found...
With xml, YOU decide how you want to access the node!
<config>
<user-key name="something" value="111" default="0" />
<user-key name="something else" value="XML Rocks" default="Merry Christmas" />
</config>
Alternatively, look into storing your custom tags in web.config/machine.config.
Here is an article: http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=90
|
|
|
|
|