|
Hi,
Seems to me one of us is not understanding what the other meant.
Either you start with a TYPE double that holds an integer VALUE, then my advice is to
convert it to a TYPE int; that will succeed without problems (unless you exceed the
int's range).
Or your TYPE double holds a non-integer VALUE, and then you will not be able to explain to
me what the meaning of most of its bits is; your question does not make sense in this case.
Did you at all try what I suggested?
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
Hi,
I wrote a simple code to read a binary file.
It just shows contents of a file as hexadecimal digits.
<br />
using System;<br />
using System.IO;<br />
<br />
class Program<br />
{<br />
static void Main(string[] args)<br />
{<br />
FileInfo fi = new FileInfo(args[0]);<br />
BinaryReader br = new BinaryReader(fi.OpenRead());<br />
int offset = 0;<br />
while (br.PeekChar() != -1)
{<br />
if (offset++ % 16 == 0)<br />
Console.Write("\n{0:x8}:", offset);<br />
Console.Write("{0,4:x2}", br.ReadByte());<br />
}<br />
Console.WriteLine();<br />
br.Close();<br />
}<br />
}<br />
And an exception is thrown at PeekChar() method showing messages as follows:
<br />
Unhandled exception: System.ArgumentException: Cannot include the encoded character because of insufficient buffer<br />
Encoding 'Unicode (UTF-8)' (fallback) 'System.Text.DecoderReplacementFallback'.<br />
Argumane name: chars<br />
Location: System.Text.Encoding.ThrowCharsOverflow()<br />
Location: System.Text.Encoding.ThrowCharsOverflow(DecoderNLS decoder, Boolean nothingDecoded)<br />
Location: System.Text.UTF8Encoding.GetChars(Byte* bytes, Int32 byteCount, Char* chars, Int32 charCount, DecoderNLS baseDecoder)<br />
Location: System.Text.DecoderNLS.GetChars(Byte* bytes, Int32 byteCount, Char* chars, Int32 charCount, Boolean flush)<br />
Location: System.Text.DecoderNLS.GetChars(Byte[] bytes, Int32 byteIndex, Int32 byteCount, Char[] chars, Int32 charIndex, Boolean flush)<br />
Location: System.Text.DecoderNLS.GetChars(Byte[] bytes, Int32 byteIndex, Int32 byteCount, Char[] chars, Int32 charIndex)<br />
Location: System.IO.BinaryReader.InternalReadOneChar()<br />
Location: System.IO.BinaryReader.Read()<br />
Location: System.IO.BinaryReader.PeekChar()<br />
Location: Program.Main(String[] args) File D:\CSharp\ConsoleApp\Program.cs:Line 11<br />
I'd like to know what's wrong with my code and what is the right way.
I'm using VS 2005 & .NET framework 2.0.50727
Thanks in advance.
|
|
|
|
|
The file you're reading has unicode characters. Set the encoding to Encoding.Unicode
FileInfo fi = new FileInfo(args[0]);
BinaryReader br = new BinaryReader(fi.OpenRead(), Encoding.Unicode);
int offset = 0;
while (br.PeekChar() != -1)
{
if (offset++ % 16 == 0)
Console.Write("\n{0:x8}:", offset);
Console.Write("{0,4:x2}", br.ReadByte());
}
Console.WriteLine();
br.Close();
Eslam Afifi
|
|
|
|
|
I’ve developed an application that uses a third party ActiveX control for a long time. Now when I run the application on Windows Vista I got the following exception:
at System.Windows.Froms.UnsafeMethods.IOleObject.SetClientsite(IOleClientSite pClientsite)
at System.Windows.Forms.AxHost.SlowActivate()
at System.Windows.Forms.AxHostTransitionUpTo(Int32 state)
at System.Windows.Forms.AxHostCreateHandle()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.AxHost.EndInit()
at System.Windows.Forms.CortonaControl.InitializeComponent()
...
Any idea what’s wrong with Vista?
_____________________________
...and justice for all
APe
|
|
|
|
|
d00_ape wrote: Any idea what’s wrong with Vista?
d00_ape wrote: uses a third party ActiveX control for a long time
Not Vista, get an updated ActiveX control.
|
|
|
|
|
Errm. Why do you think it's a problem with Vista? If a nail punctures your tyre do you think that's a faulty tyre? I would guess that the problem has more to do with the control misbehaving - and Vista's a lot stricter than earlier MS OSes.
|
|
|
|
|
Ok nothing is 'wrong' with Vista but apparently my program that have worked on XP for years does not do that on Vista. Maybe it’s a ‘new feature’ that it does not can use old ActiveX.
By the way, I have one Vista computer that my program works on but the other five cups that I have tried it on does not work.
I don’t really know where I should start searching for help…
I have the same question on http://www.codeguru.com/forum/showthread.php?p=1692586#post1692586
_____________________________
...and justice for all
APe
|
|
|
|
|
Pete O'Hanlon wrote: Vista's a lot stricter than earlier MS OSes
Can I in some way make Vista less stricter on my application?
_____________________________
...and justice for all
APe
|
|
|
|
|
Have you tried to set Vista to run this program as if it were an earlier version of the OS?
|
|
|
|
|
Yes, I have tried Run this program as: Windows XP (Service Pack 2) and as administrator, but same result.
_____________________________
...and justice for all
APe
|
|
|
|
|
Have you tried turning off UAC? Perhaps the ActiveX has a problem when virtualization is active...
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
Now I've tried that but no difference
_____________________________
...and justice for all
APe
|
|
|
|
|
|
Hi,
c#, vs2005.
Im creating xml documents and later validating them using an xsd.
I use the following code to create and append elements then output the xml document to a file:
XmlElement element = xmlDoc.CreateElement(fieldName);
element.InnerText = fieldValue;
parentElemennt.AppendChild(element);
the document is saved to file using xmlDoc.Save(path...)
and validated later using an XmlReader with reader settings.
The problem is that sometimes the fieldValue can be a blank string - looking at the xml file the empty tags get written as:
<element>
</element>
This causes problems with validation where lets say the type is set to a maxLength of 2:
The value '
' is invalid according to its datatype. The actual length is greater than the MaxLength value.
<element></element> works fine if i change it in the xml document manually.
How can I get the xml to be written as <element></element> or <element /> without the carriage return?
Another validation error im getting is where the field type is an Integer but left blank.
The value '
' is invalid according to its datatype The string ' ' is not a valid Integer value.
With this i still get an error if I change the xml to <element></element> and I have added the nillable=true flag in the xsd.
How can I get blank numeric elements to get accepted?
Thanks in advance.
Chas
|
|
|
|
|
Check to see if there is actually anything in fieldValue (i.e. is it an empty string) before setting the innerText. Make sure the string has been trimmed before the test so that you don't get dodgy data issues creeping in.
|
|
|
|
|
<field name="textBox" y="3.175" x="3.17" w="62" h="9">
<ui>
<textBox>
<border>
<css style=""> </css>
</border>
<margin/>
</textBox>
</ui>
<font face="Arial"/>
<margin topInset="1" bottomInset="1" leftInset="1" rightInset="1"/>
<textAlign vAlign="middle"/>
<label reserve="25">
<Font fontFace="Arial" size="8pt" />
<textAlign vAlign="middle"/>
<value>
<text>textbox label</text>
</value>
</label>
</field>
So I am trying to read the values of Font (IF there is a textBox element present) and trying to read the attributes fontFace and size, here is my Xpath:
XPathNodeIterator iterator = nav.Select(@"/field");
try
{
while (iterator.MoveNext())
{
...
XPathNavigator nav2 = iterator.Current.Clone();
if (string.Compare(nav2.Name, "field") == 0)
{
XPathNodeIterator textBoxIterator = nav2.Select("child::*/child::textBox");
if (textBoxIterator.Count > 0)
{
XPathExpression exp = nav2.Compile("child::Font");
XPathNodeIterator it = nav2.Select(exp);
while (it.MoveNext())
{
XPathNavigator nav3 = it.Current.Clone();
string fontFace = nav3.GetAttribute("typeface", nav3.NamespaceURI);
string fontSize = nav3.GetAttribute("size", nav3.NamespaceURI);
}
}
}
}
catch
...
}
Am I doing it the proper way or is it too complicated and can be simplified?
modified on Tuesday, March 4, 2008 10:22 AM
|
|
|
|
|
//field/font@fontFace I believe, and you can put something in [] to specific a condition, such as where there's a sibling node called textbox.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Hi,
I have a problem with retrieving a wav file from the Resources folder.
The wav is file has it build action stands on Embedded Resource.
I thought to acces a png file I can use:
Bitmap bm = new Bitmap(GetType().Assembly.GetManifestResourceStream("image.png"));
So what to use to access a .wav file?
Stream wav = System.Reflection.Assembly.GetManifestResourceStream("attention.wav"); ?
Thanks
|
|
|
|
|
System.IO.Stream s = Properties.Resources.attention; // the name of the resource
Eslam Afifi
|
|
|
|
|
Stream wav = Assembly.GetExecutingAssembly().<br />
GetManifestResourceStream(<br />
MethodBase.GetCurrentMethod().<br />
DeclaringType.Namespace + ".Resources." + "attention.wav");
(MethodBase.GetCurrentMethod().DeclaringType.Namespace assuming your calling from within the same namespace)
Answered this for you in this thread[^]
Dave
modified on Wednesday, March 5, 2008 4:22 AM
|
|
|
|
|
Hello everybody
My project is a C# project on VS2005.
My application communicate with external device through Serial Port.I'm using USB to RS convertor whitch add another COM Port in my computer.When I connect with device through this COM Port(transmit and receive data) everythink is OK.When unplug the USB cable from PC and I try to transmit data the program is generate exeption.How can I detect when the COM Port is disappear?
Thanks in advance
|
|
|
|
|
Hi,
I beleive you are looking for .IsOpen
I've just tried this with a USB-Serial converter and it works. (Goes false on a disconnect).
It doesn't cause an event though.
Matthew Butler
|
|
|
|
|
Sorry but it doesn't works.
My code is
if (sp.IsOpen)
{
sp.Write(TxBuf, 0, (k + 2 + ofs));
}
else
{
FL_Connected = false;
MessageBox.Show("Message");
}
When I unplug USB cable (Converter) from my computer the COM Port is still yet open but when the program try to write data to serial port it's generate exception "Access to the Port is denied"
|
|
|
|
|
You should always put communication like this in a try/catch block so you can catch exceptions like this.
It may be worth starting a timer when you open your port, pausing it when you're about to read/write and stopping it when you close it. That way you can test the port on each Tick event so you always know when it's connected/disconnected.
You can also monitor USB devices, get notified when one is inserted/removed and check if it's the one you're intersted in so you can act accordingly. There are some articles on this site if you search.
Dave
|
|
|
|
|
thanks for an answer
I try to do this
try
{
sp.Write(TxBuf, 0, (k + 2 + ofs));
}
catch
{
FL_Connected = false;
MessageBox.Show("Message");
}
And it works.But when I close the program it generate the same exception(Access to the port is denied)
|
|
|
|