![]() |
| | #1 |
| uh oh Join Date: Jan 2005 Location: Ontario, CA
Posts: 66
| Problem with language conversion ... lol 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;
.
.
.
}
|
| cyreon is offline | |
| | #2 |
| Guest Join Date: Aug 2001
Posts: 4,923
| 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. |
| Sebastiani is offline | |
| | #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. |
| cyreon is offline | |
| | #4 |
| Registered User Join Date: Jun 2008
Posts: 1,135
| 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 |
| C_ntua is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Memory problem with Borland C 3.1 | AZ1699 | C Programming | 16 | 11-16-2007 11:22 AM |
| problem for amateur lol | clarky101 | C Programming | 3 | 10-21-2007 09:04 AM |
| Strange loop | D@rk_force | C++ Programming | 22 | 12-18-2004 02:40 PM |
| simple currency conversion problem | sweetgem | C++ Programming | 2 | 08-01-2002 12:41 PM |
| realloc and pointer conversion from sub to main function | cjtotheg | C Programming | 2 | 02-07-2002 09:39 AM |