From: Hugo gleaves on
Hmm

This looks like it may be a pain, I have a need to simply convert the data
at address src_ptr from some type to another type whose storage is at
dest_ptr, the source data is known to be OK and the dest buffer will always
be large enough for result.

The variant stuff seems to require 'variants' to be defined etc, I am
working in pure old-fashioned C by the way and the code must run on 32-bit
and 64-bit OSs.

If I've misunderstood this do shout, else I might as well just plod on with
a load of simple functions (which pretty much just do a cast from one type to
another).

Thx
H


"Jochen Kalmbach [MVP]" wrote:

> Hi Hugo!
>
> > Does anyone know if there are any routines in the C runtime that allows
> > comversion from one type to another?
> >
> > We need to support from/to char, wchar, short, long, long long, float and
> > double including the signed/unsigned too.
>
> In general you would use "VARIANT" for this scenario.
>
> See also:
> VARIANT and VARIANTARG
> http://msdn.microsoft.com/en-us/library/ms221627
>
> VariantChangeType
> http://msdn.microsoft.com/en-us/library/ms221258
>
> Variant Manipulation Functions
> http://msdn.microsoft.com/en-us/library/ms221673
>
> --
> Greetings
> Jochen
>
> My blog about Win32 and .NET
> http://blog.kalmbachnet.de/
>
From: Jochen Kalmbach [MVP] on
Hi Hugo!

> The variant stuff seems to require 'variants' to be defined etc, I am
> working in pure old-fashioned C by the way and the code must run on 32-bit
> and 64-bit OSs.

Variant works for 32 and 64-bit... there is not problem...

> If I've misunderstood this do shout, else I might as well just plod on with
> a load of simple functions (which pretty much just do a cast from one type to
> another).

I don't know what you have understood... so I also don't know what you
have misunderstood ;)

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
From: Hugo gleaves on
Hi Jochen

What I meant was this:

I am coding purely in C and have ptrs to buffers that contain data. The type
is described by a simple type-code (identical values as used in .Net TypeCode
enum).

I therefore do stuff like this, if I want to convert a double to a long:

*((long*)(target_ptr)) = (long)(*((double*)(source_ptr)));

Every conversion is pretty much the same, just the type names vary (forget
about strings for now).

Clearly this is fast, and it seems that Variants require me to setup
structures and stuff and other code, which I want to avoid.

So have I miunderstood that, are variants fiddly generic structs that one
must setup and initialzie or is it simpler than that?

Thanks
H



"Jochen Kalmbach [MVP]" wrote:

> Hi Hugo!
>
> > The variant stuff seems to require 'variants' to be defined etc, I am
> > working in pure old-fashioned C by the way and the code must run on 32-bit
> > and 64-bit OSs.
>
> Variant works for 32 and 64-bit... there is not problem...
>
> > If I've misunderstood this do shout, else I might as well just plod on with
> > a load of simple functions (which pretty much just do a cast from one type to
> > another).
>
> I don't know what you have understood... so I also don't know what you
> have misunderstood ;)
>
> --
> Greetings
> Jochen
>
> My blog about Win32 and .NET
> http://blog.kalmbachnet.de/
>
From: Jochen Kalmbach [MVP] on
Hi Hugo!

> I therefore do stuff like this, if I want to convert a double to a long:
>
> *((long*)(target_ptr)) = (long)(*((double*)(source_ptr)));
>
> Every conversion is pretty much the same, just the type names vary (forget
> about strings for now).

You requested a (dynamic) *runtime* conversion... and your example is a
*compile-time* conversion...
So I don't understand what you want...


Also VARIANTs are working perfectly with (simple/native) C.

Greetings
Jochen
From: Hugo gleaves on
Hi Jochen

Yes this is a runtime conversion, our API receives two structs:

typedef struct arg_info
{
TypeCode type; // enum
char * data_ptr; // pointer to argument data bytes
}


The API needs to compare these two args in various ways, they could be any
combination of signed/unsigned int, short etc as well as float, double etc.

For any two types, there is a single 'common' type, so for a signed short
and a signed long, the common type is signed long (this is similar to what a
compiler has to do).

So we want to code stuff like:

ConvertFromTo[arg1.type][TypeCode.ULong](arg1);

and so on.

This is a dynamic scenarion and requires a runtime conversion of any type to
any other, an array of function is one idea we are looking at (my earlier
example was just one of the functions, there would be LOTS of them in total)

I looked at variants and it seems (I may be wrong) that a variant is a large
struct that must be initialized and stuff, so would be an overhead given that
our data is simply a type-code and a ptr to the data bytes.

I mean, does the variant or some other API let me just pass type codes and
pointers to data or must I use these large variant structs?

Thx
H



"Jochen Kalmbach [MVP]" wrote:

> Hi Hugo!
>
> > I therefore do stuff like this, if I want to convert a double to a long:
> >
> > *((long*)(target_ptr)) = (long)(*((double*)(source_ptr)));
> >
> > Every conversion is pretty much the same, just the type names vary (forget
> > about strings for now).
>
> You requested a (dynamic) *runtime* conversion... and your example is a
> *compile-time* conversion...
> So I don't understand what you want...
>
>
> Also VARIANTs are working perfectly with (simple/native) C.
>
> Greetings
> Jochen
>