|
Hello experts,
I'm quite new to dotnet programming and so on, so perhaps my question is very easy to answer.
I'm sending data from a µC to PC over USB. This is byte wise and when I want so send a float, I did it like in this C++ test code and it worked:
int _tmain(int argc, _TCHAR* argv[])
{
char test[4] = {25,4,158,63};
float *blubb = (float*)&test[0];
printf("%f",*blubb);
return 0;
}
Now I want to do the same in C# and it doesn't work. Here is my attempt:
namespace csharpfloattest
{
class Program
{
unsafe static void Main(string[] args)
{
byte[] test = { 25, 4, 158, 63 };
fixed (float* p = (float*)test)
{
Console.WriteLine(*p);
}
}
}
}
Compiler gives out this error:
Cannot convert type 'byte[]' to 'float*'
Perhaps someone can give me an advice?
Thanks for your help!!
Florian
|
|
|
|
|
I would suggest using BitConverter.ToSingle to convert bytes to a float, and BitConverter.GetBytes to convert a float to bytes.
Depending on the architecture of the sender and receiver (little versus big Endian), you may have to consider byte ordering.
|
|
|
|
|
Thanks a lot! Didn't know this method...
|
|
|
|
|
In general, c# (or any managed .NET language) does not have pointers.
(Yes, there's unsafe and IntPtr but that is not necessary to accomplish what you've described.)
The way to do this kind of thing in c# is to use the class System.BitConverter .
So to convert a byte[] into a float is:
class Program
{
static void Main(string[] args)
{
byte[] test = { 25, 4, 158, 63 };
float p = BitConverter.ToSingle(test, 0);
Console.WriteLine(p);
}
}
Going the other way, from float to byte[] , you would use BitConverter.GetBytes(value);
|
|
|
|
|
You can't cast an array to a pointer (not even a fixed-size array). But you can take the address of the 0th item of the array, and then attempt to cast that:
fixed (float* p = (float*)&test[0])
{
}
But that turns out to be explicitly forbidden: "The right hand side of a fixed statement assignment may not be a cast expression".
So there's not much choice, you'd have to do something like this:
fixed (byte* temp = data)
{
float* p = (float*)temp;
}
There was the alternative of not using unsafe code (not directly anyway, BitConverter.ToSingle is implemented using unsafe code itself) in this case, so this is just for completeness really.
|
|
|
|
|
i got a routine which said that it will highlight word in word document but it is giving error "Bad variable type. (Exception from HRESULT: 0x80020008 (DISP_E_BADVARTYPE))"
when this below line execute then exception occur and error as above occur.
range.find.highlight()
here is my full code
private static void HighlightText()
{
object fileName = "D:\\CVArchievePath\\C0000000001.doc";
object textToFind = "test1";
object readOnly = true;
Word.Application word = new Word.Application();
Word.Document doc = new Word.Document();
object missing = Type.Missing;
try
{
doc = word.Documents.Open(ref fileName, ref missing, ref readOnly,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing);
doc.Activate();
object matchPhrase = false;
object matchCase = false;
object matchPrefix = false;
object matchSuffix = false;
object matchWholeWord = false;
object matchWildcards = false;
object matchSoundsLike = false;
object matchAllWordForms = false;
object matchByte = false;
object ignoreSpace = false;
object ignorePunct = false;
object highlightedColor = Word.WdColor.wdColorGreen;
object textColor = Word.WdColor.wdColorLightOrange;
object missingp = false;
Word.Range range = doc.Range();
bool highlighted = range.Find.HitHighlight(ref textToFind,
ref highlightedColor,
ref textColor,
ref matchCase,
ref matchWholeWord,
ref matchPrefix,
ref matchSuffix,
ref matchPhrase,
ref matchWildcards,
ref matchSoundsLike,
ref matchAllWordForms,
ref matchByte,
ref missingp,
ref missingp,
ref missingp,
ref missingp,
ref missingp,
ref ignoreSpace,
ref ignorePunct,
ref missingp);
System.Diagnostics.Process.Start(fileName.ToString());
}
catch (Exception ex)
{
Console.WriteLine("Error : " + ex.Message);
}
}
tbhattacharjee
modified 8-Mar-13 7:45am.
|
|
|
|
|
You are using the boolean type false for a number of your parameters, but the ducumentation[^] defines these as ref object types. Try changing your code.
Use the best guess
|
|
|
|
|
i modify my code. now send
object missingp = false; ref missingp instead of harcode code false but still no luck. can u guide me please. thanks
tbhattacharjee
|
|
|
|
|
Tridip Bhattacharjee wrote: can u guide me please Sorry but this method is not available in my versions of Word (2003 and 2007) so I am unable to test it. I can only suggest that you try one of the Microsoft forums.
Use the best guess
|
|
|
|
|
Is that the actual code? I surprised that it compiles as the HitHighlight methods has ref parameters.
|
|
|
|
|
i modify my code. now send
object missingp = false; ref missingp instead of harcode code false but still no luck. can u guide me please. thanks
tbhattacharjee
|
|
|
|
|
The problem with many of these MSWord methods is that they have many parameters and it not easy to know which combinations are valid. I would suggest simplifying the HitHighlight method call and just specify the text to find, as that is the only required parameter. Set all others to Type.Missing. If that works as expected then reintroduce the other parameters, possibly one at a time.
There are warnings in the documentation that some parameters "may not be available", e.g. HanjaPhoneticHangul is available only if you have support for Korean languages. This may mean that it must be set to Type.Missing and not true or false. The documentation is not clear about this.
http://msdn.microsoft.com/en-us/library/office/bb226067(v=office.12).aspx[^]
http://msdn.microsoft.com/en-jm/library/microsoft.office.interop.word.find.hithighlight.aspx[^]
Alan.
|
|
|
|
|
In a C# 2008 windows application, I planning on locating files that I need to find by utilizing the following code:
var RFiles = from path in Directory.EnumerateFiles(filesaveLocation, "*.*",
SearchOption.AllDirectories)
let extension = Path.GetExtension(path)
where extension == ".pdf" || extension == ".xlsx" || extension == ".xls"
select path;
However once I find each selected file, I need to know the exact location of where each specified file was located. I need to be able to store the exact directory structure location in a sql server 2008 r2 database.
Basically the code statement would be similar to tell me exactly where each seelcted file is located at.
Thus can you me in code and/or explain to me how to accomplish my goal?
|
|
|
|
|
Have you checked the properties of that Directory object for anything useful, like the directory path it's representing??
|
|
|
|
|
In this case, you can switch to use the DirectoryInfo class to do this:
DirectoryInfo filesaveDir = new DirectoryInfo(filesaveLocation);
var RFiles = from fi in filesaveDir.EnumerateFiles("*.*", SearchOption.AllDirectories)
let extension = fi.Extension
where extension == ".pdf" || extension == ".xlsx" || extension == ".xls"
select fi.FullName;
|
|
|
|
|
How to use corrupted sql server database into oracle using c#
|
|
|
|
|
why do you want to move a corrupt database into Oracle? surely you should fix the SQL Database first
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
|
|
|
|
|
|
Add the .MDF file as a BLOB.
|
|
|
|
|
Dear All,
I am using Gridfs to store all my images in mongodb. How do i retrieve the images from mongodb using Gridfs. And with which C# control i can display the mondodb images. Please sugget any ideas.
|
|
|
|
|
ArunRajendran1 wrote: I am using Gridfs to store all my images in mongodb. How do i retrieve the images from mongodb using Gridfs.
You'd need to go through the GridFS documentation; it's not a generic C# subject, and most members will not recognize it. FWIW, there's an example on SO[^].
ArunRajendran1 wrote: And with which C# control i can display the mondodb images.
I'd suggest either a Panel or a PictureBox .
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
How to insert data using combo box in c# base language in windows base application ???
|
|
|
|
|
|
Hello,
I recently finished a C# course and I'm ready to create what could be a complex program, to me at least. The idea I have is to monitor a cad application that runs on the users machine and work on a design file locally but save it to the server every x number of minutes.
Users currently work on the files on a blade server that backups are mediocre at best, a network running at 100 and not gigabit.
After reading a few articles here on C# threads and processes I now have a few questions.
Can C# monitor when the application starts\ends via the task manager and get the file path and name so I can then do a time\date compare with the version on the server vs. the one on the local machine?
Anything to consider C# wise if a second instance of the application is started? They would be two different drawings and the task manager does show two applications and processes running.
Can this be set as a service to launch, or what would be the best way to implement this once a user logs on to the machine to catch the start of the application?
Should the FileSystemWatcher or EventHandler reduce the complexity?
Any input or guides to an example already existing is appreciated.
Or should I just stay with the "Hello World" example in the book?
Thanks!
I'll tell you what, then. Why don't you call me some time when you have no class? - Thornton Melon
|
|
|
|
|
You should always move beyond the "Hello World"!
<sig notetoself="think of a better signature">
<first>Jim
<last willyoudomyhomeworkforme="false">Meadors
|
|
|
|