Recent

Author Topic: how to convert from byte/char to single  (Read 8580 times)

whatsup

  • Jr. Member
  • **
  • Posts: 59
how to convert from byte/char to single
« on: April 04, 2011, 08:21:57 pm »
i tried this
MySingle := Single(MyByte)

and i get error

if i do it like this:
MySingle := Single(Byte(MyByte))
i don't get an error (is this a bug ?)


is there a function to do the cast ?

thanks in advanced

mas steindorff

  • Hero Member
  • *****
  • Posts: 566
Re: how to convert from byte/char to single
« Reply #1 on: April 04, 2011, 08:27:08 pm »
your trying to convince the compiler a bird is a Pig.
a byte is 8 to 16 bits
a single is 32 bits.
there live and diet are also different.

if you just need to set the single to the value of the byte, then drop all of the type casting
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: how to convert from byte/char to single
« Reply #2 on: April 04, 2011, 08:28:46 pm »
Byte value is a subset of Single, therefore no typecast is required to assign a Byte to Single (the other way around requires it or through some functions).

whatsup

  • Jr. Member
  • **
  • Posts: 59
Re: how to convert from byte/char to single
« Reply #3 on: April 04, 2011, 08:37:30 pm »
if you just need to set the single to the value of the byte, then drop all of the type casting

no, i'm trying to use it in experssion

mysingle := mybyte / 127

the problem is, if i don't pre-cast the byte
then the result will be always zero, if byte is less than 127

if i pre-cast it to single (without pre-storing it in a single, first)
than i could get a result with a frac which is the desired result.
a function to make the cast, could help here.

whatsup

  • Jr. Member
  • **
  • Posts: 59
Re: how to convert from byte/char to single
« Reply #4 on: April 04, 2011, 08:40:01 pm »

mysingle := mybyte / 127

the problem is, if i don't pre-cast the byte
then the result will be always zero, if byte is less than 127

unless i'm writing it like this :
mysingle := mybyte / 127.0

???

i will try this, hope it works.

mas steindorff

  • Hero Member
  • *****
  • Posts: 566
Re: how to convert from byte/char to single
« Reply #5 on: April 04, 2011, 08:42:27 pm »
if you just need to set the single to the value of the byte, then drop all of the type casting

no, i'm trying to use it in experssion

mysingle := mybyte / 127

OK, you do need type casting
mysingle := ((single)mybyte) / 127.0;
.. or do it in two steps ..
mysingle := mybyte;
mysingle := mysingle / 127;


windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

whatsup

  • Jr. Member
  • **
  • Posts: 59
Re: how to convert from byte/char to single
« Reply #6 on: April 04, 2011, 08:49:37 pm »
thank you very,
 didn't know that c syntax can work. :)

mas steindorff

  • Hero Member
  • *****
  • Posts: 566
Re: how to convert from byte/char to single
« Reply #7 on: April 04, 2011, 08:55:24 pm »
thank you very,
 didn't know that c syntax can work. :)
:o opps, better check that.  (I'm spending more time is C than Pascal at the moment)
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

Laksen

  • Hero Member
  • *****
  • Posts: 802
    • J-Software
Re: how to convert from byte/char to single
« Reply #8 on: April 04, 2011, 09:15:23 pm »
Code: [Select]
mysingle := mybyte/127;Will always cast mybyte to single first, and then divide with 127.0

/ is the floating point division operator. It simply doesn't handle integers :)
It's true that if you do
Code: [Select]
mysingle := mybyte div 127;then it'll be wrong. But since "div" is integer only, there's no way you can confuse them :)

whatsup

  • Jr. Member
  • **
  • Posts: 59
Re: how to convert from byte/char to single
« Reply #9 on: April 04, 2011, 09:19:29 pm »
thank you very much,
didn't know this, since in C, this is not the case.

 

TinyPortal © 2005-2018