Qbasicnews.com
TYpecasting - Printable Version

+- Qbasicnews.com (http://qbasicnews.com/newforum)
+-- Forum: General (http://qbasicnews.com/newforum/forum-6.html)
+--- Forum: General/Misc (http://qbasicnews.com/newforum/forum-18.html)
+--- Thread: TYpecasting (/thread-6001.html)

Pages: 1 2


TYpecasting - Hordeking - 02-11-2005

I'll probably take some flack for suggesting this, but here goes:

Is there any way to do typecasting of variables in FB?
I'm pretty sure QB didn't have it, but I've found it to be extremely useful in C (yes, I realise FB isn't C, but typecasting is nice)

I don't mean strange things like (void *), but reasonable things such as a typecast from a byte to a short and especially things like shorts to bytes and so on. This really comes into play when putting things like shorts into byte arrays (i.e. instead of setting two individual bytes one at a time, let the compiler handle that).


TYpecasting - DrV - 02-11-2005

Use CINT and related functions just like in QB.


TYpecasting - Hordeking - 02-11-2005

That works for a one-way conversion, but what if I decide to go from a LONG to a BYTE, or to an INT? The technical description of CINT/INT says it doesn't like numbers out of it's range.

Let's assume I want to typecast the long &hDEADBEEF to a byte.
CINT/INT would have a conniption.


TYpecasting - steven_basic - 02-11-2005

Quote:Let's assume I want to typecast the long &hDEADBEEF to a byte.

Typcast a long into a byte? OK, which of the 8 bytes do you expect to have in the single target byte once the typecast is performed?

Will C/C++ even allow something like this?


TYpecasting - Hordeking - 02-11-2005

I generally assume the least significant byte is what I end up with as the end product.

I'm pretty sure C/C++ allows it, but with a warning. I can't directly test this, as I recently reinstalled my OS and haven't set up C/C++ yet)

You're right, it's not usually too wise to do things like typecasting a long as a byte, but it would be more useful to do something like typecast an array (defined as a long array) as bytes, read in from a file, then be able to refer to the individual elements as longs (e.g. a file 16 bytes long would yield 2 longs) It might also come in handy for some forms of pointer arithmetic.

It also might be a moot point. But I have had places where I'd have liked to been able to typecast a byte array at index 0 as a short, send in the short to indices 0 and 1, and have the rest of the array behave normally, and be able to access the upper and lower bytes of the short individually.


TYpecasting - ShadowWolf - 02-11-2005

you could pull of the same effect simple by using a Integer pointer that points to the byte array then using the pointer index.


TYpecasting - helium - 02-11-2005

Quote:I'm pretty sure C/C++ allows it, but with a warning. I can't directly test this, as I recently reinstalled my OS and haven't set up C/C++ yet)

Such conversions from long to byte can even be performed implicitly in C (without a cast).


TYpecasting - Hordeking - 02-12-2005

Quote:you could pull of the same effect simple by using a Integer pointer that points to the byte array then using the pointer index.

You're right. I could. However, I don't like declaring variables for meta-actions. What if C were to require that? You'd have one huge mess because there'd be pointers declared everywhere for one-time use as a typecast. One-time is slightly relative because you can reassign pointers, but it still adds extra code and memory overhead (I'm a big believer in Moore's law, but I'm still a RAM hog)

Quote:Such conversions from long to byte can even be performed implicitly in C (without a cast)

Yeah, that happens when you store a long in a byte. What get's saved? I would expect it's the least significant byte.


TYpecasting - na_th_an - 02-12-2005

Quote:Yeah, that happens when you store a long in a byte. What get's saved? I would expect it's the least significant byte.

That depends on the system... little or big endian.


TYpecasting - The Car - 02-12-2005

Quote:
Hordeking Wrote:Yeah, that happens when you store a long in a byte. What get's saved? I would expect it's the least significant byte.

That depends on the system... little or big endian.

Hmm,, I've never had to cast a long to a byte. I've usually only casted longs to doubles and stuff.

If you have to cast a Dword to a byte then something is screwy with how you have your data set up. Smile Unless you can give me a valid example.