Problem with language conversion ... lol

This is a discussion on Problem with language conversion ... lol within the C# Programming forums, part of the General Programming Boards category; Fairly new to C# programming, but very familiar with C and decent with C++. I am in the processing of ...

  1. #1
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66

    Problem with language conversion ... lol

    Fairly new to C# programming, but very familiar with C and decent with C++. I am in the processing of making an attempt to convert an old C & C++ library over to the new language, but having some difficulties in the process.

    Now the problem exactly is that I am taking data in one form or another (through the use of an Object), converting it to an array of Byte (through the System.BitConverter.GetBytes method). This class dissects the data based on parameters, does necessary changes, etc. etc. and this leads me to the actual problem. I need to convert the Byte array back into the form of data it was originally, passing it as an Object. I honestly wish the ToChar, ToString, ToInt32, etc. methods all returned Objects for delegates would make this a very easy task. However, since that doesn't seem to be an option, is there perhaps something I am missing? The two methods I have considered, and this could be only because of my familiarity with the previous two languages and me being very new to C#, are as follows (the second example includes more detailed pseudo as to processing for a specific Type so then you can see the exact method I'm considering). I'm not sure how casting from Byte[] to Char[], String, Double, Single, etc. would otherwise be done as I can't find anything other then the BitConverter class. I haven't considered direct casting, though I guess that might work as well? Figured there was a reason why the BitConverter (and possibly others) were supplied.

    My train of thought kinda went as follows... Object is similar to what I would be familiar with... the lovely void*. So in this assumption, unless I'm totally off, by returning an Object I could essentially cast the Object to what it technically already is... (in the second example below, and the Char pseudo example, retCharArray=(Char[])retObj).

    Code:
    // pseudo-code, if-style
    if (dataType == Char) {}
    else if (dataType == Double) {}
    else if (dataType == Int32) {}
    .
    .
    .
    Code:
    // pseudo-code, switch-style
    switch (dataType)
    {
             case Char:
                       new CharArray (Length/2+1)
                       foreach Byte[2] in data // needs a for/while/etc. loop
                              CharArray[cur] = BitConverter.ToChar(...)
                       CharArray[last] = NULL Terminator
                       return (Object)CharArray;
                       break;
             case String:
                       break;
             case Double:
                       break;
             .
             .
             .
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I've never used it myself, but it appears you could use System.BitConverter.ToString(data), etc. to convert it back to it's original type.

  3. #3
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66
    Yup, thats what I was considering using. But I was hoping I could find something like a delegate (or some other class I was unaware of that would allow a similar functionality in regards to using various methods all through one call). Such a functionality would be nice considering these methods take all the same input, just return a different value.

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I am a bit confused on your converting back the data. You pass the data as an object, use the BitConverter.GetBytes() method to make the data in a byte array, no matter what the data type is. Then you want to convert it back to the original data? I don't see the need for a converstion. Just a simple cast it back as an object, or to a a string, to an int or anything you want. If the data is in the correct form this will be ok

    If you want something more generic, like casting it to the correct type without searching for the type with if and/or switch statements, I believe there is still a way. You should, I believe, search under Dynamic Generic Casting or something like that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. problem for amateur lol
    By clarky101 in forum C Programming
    Replies: 3
    Last Post: 10-21-2007, 09:04 AM
  3. Strange loop
    By D@rk_force in forum C++ Programming
    Replies: 22
    Last Post: 12-18-2004, 01:40 PM
  4. simple currency conversion problem
    By sweetgem in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2002, 12:41 PM
  5. Replies: 2
    Last Post: 02-07-2002, 08:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21