Double to byte

New to FreeBASIC? Post your questions here.
Post Reply
nimdays
Posts: 236
Joined: May 29, 2014 22:01
Location: West Java, Indonesia

Double to byte

Post by nimdays »

I read about how to convert float to char in c, and then i tried something like this:

Code: Select all

dim as double a = 3.14
dim as ubyte b = a

print b  'output = 3
Is this automatic without casting anymore ?

Thanks.
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Double to byte

Post by grindstone »

It's the same as

Code: Select all

Dim As UByte b = CInt(a)
Please note the difference between CInt, Int and Fix.

Also note that the result is not a character "3" but an integer number 3.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Double to byte

Post by MrSwiss »

nimdays wrote:I read about how to convert float to char in c, and then i tried something like this
Please note below:

Float (in C) = Single (in FB) -- (Double / Double is the same in C/FB)
nimdays
Posts: 236
Joined: May 29, 2014 22:01
Location: West Java, Indonesia

Re: Double to byte

Post by nimdays »

grindstone wrote:It's the same as

Code: Select all

Dim As UByte b = CInt(a)
Please note the difference between CInt, Int and Fix.

Also note that the result is not a character "3" but an integer number 3.
Thanks,

Do you mean an integer, not a byte anymore ?
MrSwiss wrote:
nimdays wrote:I read about how to convert float to char in c, and then i tried something like this
Please note below:

Float (in C) = Single (in FB) -- (Double / Double is the same in C/FB)
Thanks.
Trinity
Posts: 214
Joined: Sep 16, 2017 17:07

Re: Double to byte

Post by Trinity »

nimdays wrote:
grindstone wrote:It's the same as

Code: Select all

Dim As UByte b = CInt(a)
Please note the difference between CInt, Int and Fix.
Also note that the result is not a character "3" but an integer number 3.
Thanks,

Do you mean an integer, not a byte anymore ?
For reference :
CInt : https://www.freebasic.net/wiki/wikka.ph ... =KeyPgCint
Int : https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgInt
Fix : https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgFix
Ubyte : https://www.freebasic.net/wiki/wikka.ph ... KeyPgUbyte
Byte : https://www.freebasic.net/wiki/wikka.ph ... =KeyPgByte

Quick ref. : Standard variable types and limits : https://www.freebasic.net/wiki/wikka.ph ... blVarTypes

BYTE : Standard data type: 8 bit signed : BYTE , signed integer , value = -128 to +127
UBYTE : Standard data type: 8 bit unsigned. Equivalent to Unsigned Byte. : UBYTE , unsigned integer , value = 0 to +255

I hope that any of this is of use :-)
nimdays
Posts: 236
Joined: May 29, 2014 22:01
Location: West Java, Indonesia

Re: Double to byte

Post by nimdays »

@Trinity , Thanks

I'm confused because the variable is ubyte and then integer :)
Trinity
Posts: 214
Joined: Sep 16, 2017 17:07

Re: Double to byte

Post by Trinity »

nimdays wrote:@Trinity , Thanks
I'm confused because the variable is ubyte and then integer :)
You are very welcome :-)
Honestly , had I not looked it up I would not have known either , but generally it's always a good idea to consult the manual though sometimes it can be hard to find what one wants.
But if you are in doubt about a "keyword" then you can look in Alphabetical Keywords List : https://www.freebasic.net/wiki/wikka.ph ... gFullIndex
and sometimes problem can be of a kind where we may need to look at the categories in the Functional Keyword List : https://www.freebasic.net/wiki/wikka.ph ... FunctIndex
and last but not least then the search feature in the .chm help file is always very useful too :-)
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Double to byte

Post by grindstone »

Yes, I confess, my explanation was a bit ambiguous. With "integer" I meant the native meanig of that word, not the keyword.

"BYTE" (respectively "UBYTE") means an 8-bit-wide integer value (in opposite to a decimal value)
"SHORT" means a 16-bit-wide integer value
"LONG" means a 32-bit-wide integer value
"LONGINT" means a 64-bit-wide integer value
"INTEGER" (the keyword) means a 32-bit-wide integer value in a 32bit environment and a 64-bit-wide integer value in a 64bit environment.

BTW: To convert a floating point number to a string I would recommend using the "STR()" - function.
Post Reply