data conversion

Discussion in 'C Programming' started by andrewjj20, Nov 23, 2003.

  1. andrewjj20

    andrewjj20 Guest

    I have a program producing data as a double somehow I need to get red of
    the decimal and convert it to a short, and then conver that to a char to
    be read to /dev/dsp as 16-bit audio data.

    andrewjj20
     
    andrewjj20, Nov 23, 2003
    #1
    1. Advertising

  2. andrewjj20

    Jack Klein Guest

    On Sun, 23 Nov 2003 19:03:11 GMT, andrewjj20 <>
    wrote in comp.lang.c:

    > I have a program producing data as a double somehow I need to get red of
    > the decimal and convert it to a short, and then conver that to a char to
    > be read to /dev/dsp as 16-bit audio data.
    >
    > andrewjj20


    double d;
    char c;

    /* do something to produce a value in d */

    c = d;

    Assigning a floating point value to any integer type truncates the
    fractional part.

    But if the whole number part of the floating point type is outside the
    range of values of the integer type, the result is undefined behavior.

    Assuming that plain char is signed on your system, the assignment will
    do exactly what you want as long as d is greater than -128.0 and less
    than +128.0. If the double value is outside the range of a char, you
    need to do something about that first.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
     
    Jack Klein, Nov 24, 2003
    #2
    1. Advertising

  3. andrewjj20

    andrewjj20 Guest

    Jack Klein wrote:
    > On Sun, 23 Nov 2003 19:03:11 GMT, andrewjj20 <>
    > wrote in comp.lang.c:
    >
    >
    >>I have a program producing data as a double somehow I need to get red of
    >>the decimal and convert it to a short, and then conver that to a char to
    >>be read to /dev/dsp as 16-bit audio data.
    >>
    >>andrewjj20

    >
    >
    > double d;
    > char c;
    >
    > /* do something to produce a value in d */
    >
    > c = d;
    >
    > Assigning a floating point value to any integer type truncates the
    > fractional part.
    >
    > But if the whole number part of the floating point type is outside the
    > range of values of the integer type, the result is undefined behavior.
    >
    > Assuming that plain char is signed on your system, the assignment will
    > do exactly what you want as long as d is greater than -128.0 and less
    > than +128.0. If the double value is outside the range of a char, you
    > need to do something about that first.
    >

    currently here is the code that I am using to change a short into signed
    16-bit data:

    short int t = data[a];
    unsigned char high = t >> 8;
    unsigned char low = t;
    buf[2*a]=low;
    buf[2*a+1]=high;

    it was in a for loop and buf is an unsigned char array.

    andrewjj20
     
    andrewjj20, Nov 30, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. eneko
    Replies:
    2
    Views:
    3,015
    eneko
    Oct 20, 2004
  2. Calvin Lai

    Data Conversion

    Calvin Lai, Dec 31, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    341
    Calvin Lai
    Dec 31, 2003
  3. luna
    Replies:
    1
    Views:
    6,856
  4. egsdar
    Replies:
    0
    Views:
    1,585
    egsdar
    Dec 13, 2008
  5. , India
    Replies:
    2
    Views:
    500
    Fraser Ross
    Sep 15, 2009
Loading...

Share This Page