对于16位数据,我应该使用什么而不是putchar()?(What should I use instead of putchar() for 16

编程入门 行业动态 更新时间:2024-10-26 13:32:58
对于16位数据,我应该使用什么而不是putchar()?(What should I use instead of putchar() for 16-bit data?)

我是C的新手,编写了一个简单的程序,它生成一个具有指定频率和采样率的正弦波,并将其作为无符号的8位字节发送到stdout。

#include <stdio.h> #include <math.h> #include <stdint.h> uint8_t sinco(int iCarrier, int iSampleRate, unsigned long ulIndex){return (sin(iCarrier * (2 * M_PI) * ulIndex / iSampleRate) * 127) + 128;} void main(){ unsigned long t; const int iCarrier = 500; const int iSampleRate = 8000; for(t=0;;t++){ putchar(sinco(iCarrier, iSampleRate, t)); } }

我意识到putchar()不是最合适的功能,但它适用于我当时需要的功能。 现在我正在尝试修改程序以输出无符号的16位数,但我不知道用什么替换putchar()。

这是我到目前为止:

#include <stdio.h> #include <math.h> #include <stdint.h> uint16_t sinco(int iCarrier, int iSampleRate, unsigned long ulIndex){return (sin(iCarrier * (2 * M_PI) * ulIndex / iSampleRate) * 65535) + 65536;} void main(){ unsigned long t; const int iCarrier = 500; const int iSampleRate = 8000; for(t=0;;t++){ printf(%hu, sinco(iCarrier, iSampleRate, t)); } }

但是,一旦该值大于65,536,程序就会开始向stdout发送32位。 有没有比putchar更好的替代方案,我可以使用哪种方法可以正确包裹?

I am fairly new to C, and have written a simple program which generates a sine wave with a specified frequency and sample rate, and sends that to stdout as an unsigned 8-bit byte.

#include <stdio.h> #include <math.h> #include <stdint.h> uint8_t sinco(int iCarrier, int iSampleRate, unsigned long ulIndex){return (sin(iCarrier * (2 * M_PI) * ulIndex / iSampleRate) * 127) + 128;} void main(){ unsigned long t; const int iCarrier = 500; const int iSampleRate = 8000; for(t=0;;t++){ putchar(sinco(iCarrier, iSampleRate, t)); } }

I realize that putchar() was not the most appropriate function, but it worked for what I needed at the time. Now I am currently trying to modify the program to output an unsigned 16-bit number, but I'm not sure what to replace putchar() with.

This is what I have so far:

#include <stdio.h> #include <math.h> #include <stdint.h> uint16_t sinco(int iCarrier, int iSampleRate, unsigned long ulIndex){return (sin(iCarrier * (2 * M_PI) * ulIndex / iSampleRate) * 65535) + 65536;} void main(){ unsigned long t; const int iCarrier = 500; const int iSampleRate = 8000; for(t=0;;t++){ printf(%hu, sinco(iCarrier, iSampleRate, t)); } }

However once the value gets larger than 65,536, the program starts sending 32 bits to stdout. Is there a better alternative to putchar I can use which will correctly wrap around?

最满意答案

您希望输出以两个字节编码的值。 所以连续输出这两个字节。 哪两个字节? 它取决于16位值应该如何编码为两个8位值,即取决于读取这两个字节的系统的字节序 。

小端:

uint16_t w = sinco(…); putchar(w & 0xff); putchar((w >> 8) & 0xff);

大端:

uint16_t w = sinco(…); putchar((w >> 8) & 0xff); putchar(w & 0xff);

如果读取该值的系统具有与CPU相同的字节顺序,则可以使用其他策略:通过转储其内存内容来写入该值。 您可以将内存中的任何值读取为字节数组,并且可以使用fwrite来写入字节数组。

uint16_t w = sinco(…); fwrite(&w, 1, 2, stdout);

You want to output a value that is encoded in two bytes. So output these two bytes successively. Which two bytes? It depends on how the 16-bit value is supposed to be encoded in two 8-bit values, i.e. on the endianness of the system that will read those two bytes.

Little-endian:

uint16_t w = sinco(…); putchar(w & 0xff); putchar((w >> 8) & 0xff);

Big-endian:

uint16_t w = sinco(…); putchar((w >> 8) & 0xff); putchar(w & 0xff);

If the system that reads the value has the same endianness as your CPU, then you can use a different strategy: write the value by dumping its memory contents. You can read any value in memory as an array of bytes, and you can use fwrite to write an array of bytes.

uint16_t w = sinco(…); fwrite(&w, 1, 2, stdout);

更多推荐

本文发布于:2023-08-07 23:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466028.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是   数据   putchar

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!