|
Is this shortcut new since .NET 2.0? I don't remember it in .NET 1.1 - I just used the regular ternary style.
|
|
|
|
|
Hi,
I have this methods and they work fine for FileStreams, but once I try to use the Xml.Save(stream) option and encode this stream, it reads nothing from it. Here is my code:
private void geraGzipToolStripMenuItem1_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<teste><dirso>ama posa</dirso></teste>");
FileStream f = new FileStream("C:\\DADOS\\JR\\JRFW\\bin\\output\\read.xml.gz",
FileMode.OpenOrCreate, FileAccess.Write);
GZipEx gz = new GZipEx();
gz.EncodeXml(doc, f);
}
public void EncodeXml(XmlDocument xml, Stream streamOut)
{
Stream m = new MemoryStream();
xml.Save(m);
EncodeStream(m, streamOut);
xml.Save("C:\\DADOS\\JR\\JRFW\\bin\\output\\teste.xml");
}
public void EncodeStream(Stream streamIn, Stream streamOut)
{
GZipOutputStream gz = new GZipOutputStream(streamOut);
byte[] btout = new byte[bufferSize];
int nRead;
while ((nRead = streamIn.Read(btout, 0, bufferSize)) > 0)
{
gz.Write(btout, 0, nRead);
}
gz.Flush();
gz.Close();
streamOut.Close();
}
Please, don't worry about GZipOutputStream. It's working fine. Just the streamIn is not reading anything to btout. I also checked the streamIn length and it DO have data.
Thanks,
Dirso.
|
|
|
|
|
|
You're right
streamIn.Seek(0, SeekOrigin.Begin); Sorry
Thanks,
Dirso
|
|
|
|
|
Hi Friends .
I had some question about base64 string.
First : What's the base64 string ?
Second : How can we find, our string is base64 string ?
Third : How can we create base64 string from an image ?
Fourth : I want to create Image type from base64 string , How I can do it ?
Thanks in advance.
Sorry for my English. I'm a freshman .
|
|
|
|
|
Check out System.Convert.ToBase64String and System.Convert.FromBase64String in the MSDN doc. I've never used it but the methods are in place.
|
|
|
|
|
M-Dayyan wrote: What's the base64 string ?
It is an ASCII representation of binary data. The 64 means that it splits the raw binary up into 6 bit chunks (0-63) and encodes them as letters, numbers and a few symbols 26 (a-z) + 26 (A-Z) + 10 (0-9) + some symbols. More detail can be found here[^]
M-Dayyan wrote: How can we find, our string is base64 string ?
Because it only uses the characters in the standard. If it contains any other characters (excluding whitespace) then it is not base64
M-Dayyan wrote: How can we create base64 string from an image ?
M-Dayyan wrote: Fourth : I want to create Image type from base64 string , How I can do it ?
The answer for both is pretty much the same, but one is the reverse of the other.
There are methods in the .NET framework for this. I think you might want to look at the Convert class (use Google to find it). It has "Base64" in the method name.
The conversion will turn the base64 in to binary (a byte array) which can be used to create an Image object of some description.
|
|
|
|
|
Thanks Colin.
But can you write snippet code to detect base64 string ?
Sorry for my English. I'm a freshman .
|
|
|
|
|
Not tested, but according to the RFC something like this could work:
static bool IsBase64(string s)
{
return Regex.IsMatch(s, @"^[A-Za-z0-9\+\/]*=*$");
}
Might need some adjustments in the regex pattern, I didn't read the entire RFC now.
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Thanks Greeeg.
You're nice.
Sorry for my English. I'm a freshman .
|
|
|
|
|
No problem. Make sure to read my answer again, I had to modify the regex pattern so that the result does not output "True" in some bad cases.
modified 12-Sep-18 21:01pm.
|
|
|
|
|
All right Greeeg. I saw that.
Sorry for my English. I'm a freshman .
|
|
|
|
|
Hello, I am currently writing an application that needs to be able to generate a private and public key, but it must be output in PEM format. It needs this format because other related applications require this format. I can't find any functions in any libraries that do this. What would be the best way to do this? If there is nothing that does this, is there a way I could write another program in (for example: C) that generates the keys given command line options and just hope it succeeds?
Kind Regards,
Mike
|
|
|
|
|
I have a temperature scanner that reads the temperature from certain field devices. Every morning at 7:00 a.m. the scanner sends ASCII data over the serial port (originally intended to go straight to a printer). The printer does not have a serial connection, so I am monitoring the data and using the PrintDocument class to print the data. The data contains 21 temperatures for at least 11 groups. I have no problem reading the data using the SerialPort class and the ReadLine function. However, the only way to know when the last grouping is sent is by using a timer (I can't just look for group #11's data and say that is the end). For some reason, my timer will not time out after I read serial data. Here is my code. Any help would be appreciated.
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)<br />
{ <br />
tmrRecData.Enabled = false;<br />
lblStatus.Text = "Reading Data ... ";<br />
string data = serialPort1.ReadLine();<br />
ExtractData(data);<br />
tmrRecData.Enabled = true;<br />
}<br />
<br />
private void ExtractData(string data)<br />
{<br />
lblStatus.Text = "Parsing Data ... ";<br />
<br />
if (data.StartsWith("Bin:"))<br />
currBinNum = Convert.ToInt32(data.Substring(4, 6).Trim());<br />
else if (data.Length > 10)<br />
{<br />
int lvIndex = -1;<br />
ListViewItem lvi = new ListViewItem();<br />
foreach (ListViewItem item in lvLastRead.Items)<br />
{<br />
if (item.Text == currBinNum.ToString("00"))<br />
{<br />
lvIndex = item.Index;<br />
lvi = item;<br />
break;<br />
}<br />
}<br />
if (lvIndex == -1)<br />
{<br />
lvi = lvLastRead.Items.Add(new ListViewItem(currBinNum.ToString("00")));<br />
lvIndex = lvi.Index;<br />
}<br />
lblDateTime.Text = DateTime.Now.ToString("hh:mm tt MM/dd/yyyy");<br />
string tempData = data.Substring(6, data.Length - 6);<br />
tempData = tempData.Replace((char)0x1B + "G", "");<br />
tempData = tempData.Replace((char)0x1B + "H", "").TrimEnd(null);<br />
lvi.SubItems.Clear();<br />
lvi.Text = currBinNum.ToString("00");<br />
for (int i = 0; i + 2 <= tempData.Length; i += 4)<br />
{<br />
string temp = tempData.Substring(i, 3).Trim();<br />
lvi.SubItems.Add(temp);<br />
}<br />
lvLastRead.Items[lvIndex] = lvi;<br />
}<br />
lblStatus.Text = "Waiting ...";<br />
}<br />
<br />
private void tmrRecData_Tick(object sender, EventArgs e)<br />
{<br />
tmrRecData.Enabled = false;<br />
<br />
lblStatus.Text = "Saving Data ...";<br />
DirectoryInfo di = new DirectoryInfo(Application.StartupPath);<br />
di.CreateSubdirectory("Monthly Temps " + DateTime.Now.ToString("yyyy-MM"));<br />
<br />
TextWriter tw = new StreamWriter(Application.StartupPath + @"\Monthly Temps " + DateTime.Now.ToString("yyyy-MM") + @"\Daily Temps " + DateTime.Now.ToString("yyyy-MM-dd") + ".csv");<br />
tw.WriteLine("Bin,Temp. #1,Temp. #2,Temp. #3,Temp. #4,Temp. #5,Temp. #6,Temp. #7,Temp. #8,Temp. #9,Temp. #10,Temp. #11,Temp. #12,Temp. #13,Temp. #14,Temp. #15,Temp. #16,Temp. #17,Temp. #18,Temp. #19,Temp. #20,Temp. #21");<br />
foreach (ListViewItem lvi in lvLastRead.Items)<br />
{<br />
string strLine = lvi.Text;<br />
for (int i=1; i<lvi.subitems.count;> {<br />
strLine += "," + lvi.SubItems[i].Text;<br />
}<br />
tw.WriteLine(strLine);<br />
}<br />
tw.Close();<br />
<br />
lblStatus.Text = "Printing Data ...";<br />
printPreviewDialog1.ShowDialog();<br />
lblStatus.Text = "Waiting ...";<br />
}
Chase Davis
|
|
|
|
|
I suspect that the stream of temperature data may not have a NewLine at the end for ReadLine() to pick up. If that's the case you'll have to accumulate the entire stream then parse it. I'd make clever use of the ReceivedBytesThreshold and some supporting classes to do that. Hope this helps...
Scott P
“It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration.”
-Edsger Dijkstra
|
|
|
|
|
Hi,
Which Timer are you using, System.Windows.Forms or System.Timer, and can you post your code for creating it, assigning the delegate, setting the interval etc.
Are your controls actually updating correctly when data is received? I would have thought you would see cross thread errors as the serial port event and the timer event will be raised on threadpool threads, not the form thread. You should be using the control.Invoke method to update your UI correctly.
I'm just about to leave work so I won't be able to check back for many hours.
Alan.
|
|
|
|
|
It is a System.Windows.Forms Timer
I got a cross thread exception when I first ran the program, and just shrugged it off and added the Control.CheckForIllegalCrossThreadCalls = false; line to my program. I don't know anything about threads, but if I understand what you are saying the timer that I created on my form cannot be updated (enabled / disabled) through the SerialPort DataReceived event? How would I use the Invoke methods to update my UI?
Chase Davis
|
|
|
|
|
The technique used to update controls correctly in multithreaded programmes is given here http://msdn.microsoft.com/en-us/library/ms171728(VS.80).aspx[^] and it will also be in the local help if you are using a version of Visual Studio.
The knowledge required to implementing threading reliably is not small and when you first encounter the subject it it can be quite daunting. The advice from Microsoft is to ensure that controls are only updated from the thread that created them and the reason for this is to prevent corruption of their “state” . The Control.Invoke methods essentially allow any thread to send the instruction, “Update the control with this information”, to the control’s creator thread.
That said, it is not obvious from the code you have posted if control updating has caused your problem and I would urge you to post your timer setup code. After all, if the timer is setup correctly and it is enabled then it should fire.
The Forms.Timer runs on the UI thread so your code within the tick handler looks ok. I was having a bit of a brainstorm in my previous post when I implied that the Forms.Timer would run on a separate thread. Also changing the timer’s enabled state from the serial port event thread should not be a problem.
Alan.
|
|
|
|
|
Hi all,
I'm needing to do some fancy parsing of keyboard input into a text box. Basically, the need here arises because I have three possible devices that will put input into this box, the regular keyboard, a mag stripe reader and a laser scanner.
The keyboard and laser scanner are fine, but some types of input into the mag stripe reader will contain a carriage return in the middle of input which obviously creates a problem.
I was considering using the raw input API to somehow handle the mag stripe input, but I think it would be simpler if I just used the following logic:
1)Catch first CR
2) Wait for some time interval, probably 100->200 ms
3) If we get another keypress, ignore the first one
4) Repeat until we get a condition where after the wait we have no keypress, then submit input
I'm a little unsure, though, how to 'ignore' the first CR, and how to properly wait and then check if we had another keypress event. I'd appreciate any help, either with the code or perhaps making this logic workflow simpler.
Thanks!
|
|
|
|
|
The easiest solution that comes into my mind is to keep track of the time when the last CR input happend in the keypress handler. Something like
DateTime lastCRinput;
double timeDelta = 150;
void OnKeyPress(...)
{
DateTime now = DateTime.Now;
if ((key == '\r') && ((now - lastCRinput).TotalMilliseconds > timeDelta))
{
lastCRinput = now;
}
}
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Hi,
I need to parse the XML resulted after browsing to a url.
I already have a way to do this, but I'm not quite happy with it: I have a form with an AxWebBrowser in it, I navigate the webBrowser to the url, handle webBrowser_DocumentComplete and in there grab the XML and close the form.
Is there a better way of doing this without loading and hiding a form?
thanx
|
|
|
|
|
Do an HttpWebRequest[^] and grab it?
Life, family, faith: Give me a visit.
From my latest post: "A lot of Christians struggle, perhaps at a subconscious level, about the phrase "God of Israel". After all, Israel's God is the God of Judaism, is He not? And the God of Christianity is not the God of Judaism, right?"
Judah Himango
|
|
|
|
|
Thanx!
It's official, I'm losing my mind...
|
|
|
|
|
I'm using the SetWindowsHookEx Win32 method to set a hook on window activation for my process. It's working fine until garbage collection occurs so I'm trying to pin the delegate using GCHandle.Alloc.
This is failing with an error message that the delegate is nonprimitive and nonblitable even though I've changed the method signature to this:
public delegate IntPtr HProc(IntPtr nCode, IntPtr wParam, IntPtr lParam);
How can I change the delegate so that I can use it, or is there a better approach altogether?
I can imagine the sinking feeling one would have after ordering my book,
only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon
|
|
|
|
|
I've run into a similar situation.
public delegate void ControlEventHandler(ConsoleEvent consoleEvent);
ControlEventHandler handler = new ControlEventHandler(OnControlEvent);
GC.KeepAlive(handler);
Not sure if this will help, but GC.KeepAlive will render the object ineligible for garbage collection.
Scott P
|
|
|
|
|