By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,388 Members | 1,839 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,388 IT Pros & Developers. It's quick & easy.

data conversion

P: n/a
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

Nov 13 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Sun, 23 Nov 2003 19:03:11 GMT, andrewjj20 <an********@noemail.us>
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
Nov 13 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.