- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
当我输入相同的整数时,如何将整数转换为与使用 read(0,buff,nbytes) 获得的缓冲区相同的值/编码字符?我正在尝试编写类似 read() 的东西,但用整数数据代替读取到缓冲区的文件描述符参数。
like_read(int data,void *buff,size_t nbytes);
它应该类似于 read(),因为它应该读入缓冲区的值与 read(0,buff,nbytes) 从标准输入读入其缓冲区的值相同。例如,当我不首先使用 read(0,buff,nbytes) 直接提供整数地址作为缓冲区时
int Integer=25
int nbytes=2;
int rlen,wlen,wlen1;
int fd = open("ttyusb.txt", O_RDWR | O_CREAT,0777);
wlen = write(fd, &Integer, nbytes);
wlen1 = write(1, &Integer, nbytes);//for stdout
close(fd);
预期输出
25
实际输出/文件内容是一些编码字符
,它没有给我想要的结果,因为首先使用 read() 从 stdin 将整数读入缓冲区,然后使用 write() 将该缓冲区写入文件,例如:
int Integer;
int nbytes=2;
int rlen,wlen;
int fd = open("ttyusb.txt", O_RDWR | O_CREAT,0777);
rlen = read(0, &Integer, nbytes);
wlen = write(fd, &Integer, nbytes);
wlen1 = write(1, &Integer, nbytes);//for stdout
close(fd);
标准输入
25
预期输出
25
实际输出/文件内容
25
当我在 read(0, buffer, nbytes) 后打印缓冲区值时,它给出了一些编码值:
int Integer, nbytes=2;
int fd = open("ttyusb.txt", O_RDWR | O_CREAT,0777);
rlen = read(0, &Integer, nbytes);
wlen = write(fd, &Integer, nbytes);
wlen1 = write(1, &Integer, nbytes);
printf("\nInteger buffer value %d\n",Integer);
close(fd);
stdin 0 prints "Integer buffer value 2608", stdin 1 prints "Integer buffer value 2609", stdin 2 prints "Integer buffer value 2610",.....stdin 9 prints "Integer buffer value 2617"...
read() 使用什么编码来转换整数值,如果没有 read() 我该如何进行转换?
最佳答案
我不太确定您想要实现什么,但我的解释是您正在尝试将 int
的字节复制到缓冲区中。可以这样实现:
#include <string.h>
void like_read(int data, void *buff, size_t nbytes)
{
size_t n;
/* Limit number of bytes to copy from data variable. */
n = sizeof(data);
if (n < nbytes)
n = nbytes;
/* Copy bytes from data variable to the buffer. */
memcpy(buff, &data, n);
/* If destination buffer is larger than data variable, ... */
if (n < nbytes) {
/* ... set the remaining bytes in the destination to all zero. */
memset((char *)buff + n, 0, nbytes - n);
}
}
what encoding is read() using to convert integer values and how can I do that conversion without read()?
read()
只是从文件描述符中读取原始数据字节,不进行任何转换,至少在 POSIX 类型系统(例如 Unix 和 Linux)上是这样。 Microsoft Windows 等其他一些操作系统允许使用特殊标志打开文件,这些标志对原始数据字节进行一些转换,主要用于处理具有不同编码的文本文件。
like_read
函数中的 memcpy()
调用将原始数据字节从一个内存位置复制到另一个内存位置,而不进行任何转换。
编辑
我仍然不确定您 (OP) 想要实现什么,但根据您在下面的评论,也许您正在寻找一个函数,该函数将整数值作为十进制 ASCII 字符“打印”到带有换行符的缓冲区,但是截断为指定的字节数。这几乎可以通过 snprintf()
来完成,但是该函数总是将一个终止空字节写入缓冲区(如果缓冲区大小至少为 1),所以这不是我认为您想要的。因此,snprintf()
可用于将整数值打印到足够大小的中间缓冲区,然后将结果memcpy
打印到指定的缓冲区。这是一个实现:
#include <string.h>
#include <stdio.h>
void like_read(int data, void *buff, size_t nbytes)
{
char tempbuf[24]; /* ought to be large enough... */
size_t n;
/* Print data value to temporary buffer. */
n = snprintf(tempbuf, sizeof(tempbuf), "%d\n", data);
/* n is number of bytes that would be printed to tempbuf if room. */
/* Limit number of bytes to copy to size of temporary buffer. */
if (n > sizeof(tempbuf))
n = sizeof(tempbuf);
/* Limit number of bytes to copy to nbytes. */
if (n > nbytes)
n = nbytes;
/* Copy bytes from tempbuf to the buffer. */
memcpy(buff, tempbuf, n);
/* If destination buffer is larger than n, ... */
if (n < nbytes) {
/* ... set the remaining bytes in the destination to all zero. */
memset((char *)buff + n, 0, nbytes - n);
}
}
关于c - 需要像 read() 这样的函数将整数数据读入缓冲区并获得与 read() 相同的缓冲区值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58732404/
我的问题:非常具体。我正在尝试想出解析以下文本的最简单方法: ^^domain=domain_value^^version=version_value^^account_type=account_ty
好吧,这就是我的困境: 我正在为 Reddit 子版 block 开发常见问题解答机器人。我在 bool 逻辑方面遇到了麻烦,需要一双更有经验的眼睛(这是我在 Python 中的第一次冒险)。现在,该
它首先遍历所有 y 值,然后遍历所有 x 值。我需要 X 和 y 同时改变。 For x = 3 To lr + 1 For y = 2 To lr anyl.Cells(x, 1)
假设我有一个包含 2 列的 Excel 表格:单元格 A1 到 A10 中的日期和 B1 到 B10 中的值。 我想对五月日期的所有值求和。我有3种可能性: {=SUM((MONTH(A1:A10)=
如何转换 Z-score来自 Z-distribution (standard normal distribution, Gaussian distribution)到 p-value ?我还没有找到
我正在重写一些 Javascript 代码以在 Excel VBA 中工作。由于在这个网站上搜索,我已经设法翻译了几乎所有的 Javascript 代码!但是,有些代码我无法准确理解它在做什么。这是一
我遇到过包含日期格式的时间戳日期的情况。然后我想构建一个图表,显示“点击”项目的数量“每天”, //array declaration $array1 = array("Date" => 0); $a
我是scala的新手! 我的问题是,是否有包含成员的案例类 myItem:Option[String] 当我构造类时,我需要将字符串内容包装在: Option("some string") 要么 So
我正在用 PHP 创建一个登录系统。我需要用户使用他或她的用户名或电子邮件或电话号码登录然后使用密码。因为我知道在 Java 中我们会像 email==user^ username == user 这
我在 C++ 项目上使用 sqlite,但是当我在具有文本值的列上使用 WHERE 时出现问题 我创建了一个 sqlite 数据库: CREATE TABLE User( id INTEGER
当构造函数是显式时,它不用于隐式转换。在给定的代码片段中,构造函数被标记为 explicit。那为什么在 foo obj1(10.25); 情况下它可以工作,而在 foo obj2=10.25; 情况
我知道这是一个主观问题,所以如果需要关闭它,我深表歉意,但我觉得它经常出现,让我想知道是否普遍偏爱一种形式而不是另一种形式。 显然,最好的答案是“重构代码,这样你就不需要测试是否存在错误”,但有时没有
这两个 jQuery 选择器有什么区别? 以下是来自 w3schools.com 的定义: [attribute~=value] 选择器选择带有特定属性,其值包含特定字符串。 [attribute*=
为什么我们需要CSS [attribute|=value] Selector根本当 CSS3 [attribute*=value] Selector基本上完成相同的事情,浏览器兼容性几乎相似?是否存在
我正在解决 regx 问题。我已经有一个像这样的 regx [0-9]*([.][0-9]{2})。这是 amont 格式验证。现在,通过此验证,我想包括不应提供 0 金额。比如 10 是有效的,但
我正在研究计算机科学 A 考试的样题,但无法弄清楚为什么以下问题的正确答案是正确的。 考虑以下方法。 public static void mystery(List nums) { for (
好的,我正在编写一个 Perl 程序,它有一个我收集的值的哈希值(完全在一个完全独立的程序中)并提供给这个 Perl 脚本。这个散列是 (string,string) 的散列。 我想通过 3 种方式对
我有一个表数据如下,来自不同的表。仅当第三列具有值“债务”并且第一列(日期)具有最大值时,我才想从第四列中获取最大值。最终值基于 MAX(DATE) 而不是 MAX(PRICE)。所以用简单的语言来说
我有一个奇怪的情况,只有错误状态保存到数据库中。当“状态”应该为 true 时,我的查询仍然执行 false。 我有具有此功能的 Controller public function change_a
我有一个交易表(针对所需列进行了简化): id client_id value 1 1 200 2 2 150 3 1
我是一名优秀的程序员,十分优秀!