Search Results

Search found 3 results on 1 pages for 'cgydeveloper'.

Page 1/1 | 1 

  • Marshal.PtrToStructure (and back again) and generic solution for endianness swapping

    - by cgyDeveloper
    I have a system where a remote agent sends serialized structures (from and embedded C system) for me to read and store via IP/UDP. In some cases I need to send back the same structure types. I thought I had a nice setup using Marshal.PtrToStructure (receive) and Marshal.StructureToPtr (send). However, a small gotcha is that the network big endian integers need to be converted to my x86 little endian format to be used locally. When I'm sending them off again, big endian is the way to go. Here are the functions in question: private static T BytesToStruct<T>(ref byte[] rawData) where T: struct { T result = default(T); GCHandle handle = GCHandle.Alloc(rawData, GCHandleType.Pinned); try { IntPtr rawDataPtr = handle.AddrOfPinnedObject(); result = (T)Marshal.PtrToStructure(rawDataPtr, typeof(T)); } finally { handle.Free(); } return result; } private static byte[] StructToBytes<T>(T data) where T: struct { byte[] rawData = new byte[Marshal.SizeOf(data)]; GCHandle handle = GCHandle.Alloc(rawData, GCHandleType.Pinned); try { IntPtr rawDataPtr = handle.AddrOfPinnedObject(); Marshal.StructureToPtr(data, rawDataPtr, false); } finally { handle.Free(); } return rawData; } And a quick example structure that might be used like this: byte[] data = this.sock.Receive(ref this.ipep); Request request = BytesToStruct<Request>(ref data); Where the structure in question looks like: [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] private struct Request { public byte type; public short sequence; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 5)] public byte[] address; } What (generic) way can I swap the endianness when marshalling the structures? My need is such that the locally stored 'public short sequence' in this example will be little-endian for displaying to the user. I don't want to have to swap the endianness on a structure-specific way. My first thought was to use Reflection, but I'm not very familiar with that feature. Also, I hoped that there would be a better solution out there that somebody could point me towards. Thanks in advance :)

    Read the article

  • Pass an array from IronRuby to C#

    - by cgyDeveloper
    I'm sure this is an easy fix and I just can't find it, but here goes: I have a C# class (let's call it Test) in an assembly (let's say SOTest.dll). Here is something along the lines of what I'm doing: private List<string> items; public List<string> list_items() { return this.items; } public void set_items(List<string> new_items) { this.items = new_items; } In the IronRuby interpreter I run: >>> require "SOTest.dll" true >>> include TestNamespace Object >>> myClass = Test.new TestNamespace.Test >>> myClass.list_items() ['Apples', 'Oranges', 'Pears'] >>> myClass.set_items ['Peaches', 'Plums'] TypeError: can't convert Array into System::Collections::Generic::List(string) I get a similar error whether I make the argument a 'List< string ', 'List< object ' or 'string[ ]'. What is the proper syntax? I can't find a documented mapping of types anywhere (because it's likely too complicated to define in certain scenarios given what Ruby can do).

    Read the article

  • Ruby: map tags into a boolean condition to get a true/false result

    - by cgyDeveloper
    I have an array of tags per item like so: item1 = ['new', 'expensive'] item2 = ['expensive', 'lame'] I also have a boolean expression as a string based on possible tags: buy_it = "(new || expensive) && !lame" How can I determine if an item matches the buying criteria based on the tags associated with it? My original thought was to do a gsub on all words in buy_it to become 'true' or 'false' based on them existing in the itemx tags array and then exec the resulting string to get a boolean result. But since the Ruby community is usually more creative than me, is there a better solution?

    Read the article

1