使用write()系统调用来输出char数组缓冲区的dec / hex值

时间:2014-09-09 14:39:57

标签: c formatting

int fd = open(argv[argc-1], O_RDONLY, 0);
 if (fd >=0) {
   char buff[4096]; //should be better sized based on stat
   ssize_t readBytes;
   int j;

   readBytes = read(fd, buff, 4096);

   char out[4096];
   for (j=0; buff[j] != '\0'; j++) {
     out[j] = buff[j];
     //printf("%d ", out[j]);
   }

   write(STDOUT_FILENO, out, j+1);

   close(fd);
}
 else {
   perror("File not opened.\n");
   exit(errno);
 }

这是文件转储程序的代码。目标是拥有一个文件,并将其内容作为ASCII字符和十六进制/十进制值转储到命令行。当前代码能够转储ascii值,但不能转储hex / dec。我们被允许使用printf(如注释部分所示)但如果我们不使用任何高级别(高于系统)功能,我们可以获得额外的功劳。我已经尝试了多种方法来操作循环中的char数组,但似乎无论我如何添加或转换它们作​​为字符出现的字符。

这并不奇怪,因为我知道字符,至少在C中,技术上是整数。我对如何使用write()打印char的十六进制/十进制值感到茫然,并且还没有在堆栈上看到任何没有默认为printf()或putchar()的答案

2 个答案:

答案 0 :(得分:2)

你可以制作一个更大的缓冲区,然后从中转换为ASCII / dec(根据需要)并打印新的缓冲区。我希望这个例子说明了这个想法:

#include <stdlib.h>
#include <io.h>

int main (int argc, char** argv)
{
    const char* pHexLookup = "0123456789abcdef";
    char pBuffer[] = {'a', 'b', 'c'}; // Assume buffer is the contents of the file you have already read in
    size_t nInputSize = sizeof(pBuffer); // You will set this according to how much your input read in
    char* pOutputBuffer = (char*)malloc(nInputSize * 3); // This should be sufficient for hex, since it takes max 2 symbols, for decimal you should multiply by 4
    for (size_t nByte = 0; nByte < nInputSize; ++nByte)
    {
        pOutputBuffer[3 * nByte] = pBuffer[nByte];
        pOutputBuffer[3 * nByte + 1] = pHexLookup[pBuffer[nByte] / 16];
        pOutputBuffer[3 * nByte + 2] = pHexLookup[pBuffer[nByte] % 16];
    }
    write(1 /*STDOUT_FILENO*/, pOutputBuffer, nInputSize * 3);
    free(pOutputBuffer);
    return EXIT_SUCCESS;
}

这将并排打印a61b62c63,ASCII和十六进制值。

这是在Windows上完成的,所以不要试图直接复制它,我试图坚持POSIX系统调用。基本上对于十六进制,你分配一个比原始大3倍的内存块(如果需要用空格填充输出,则分配更多),并放置一个与其旁边字节的十六进制值相对应的ASCII符号。对于十进制,您将需要更多空间,因为它可以跨越3个字符。然后只需编写新的缓冲区。希望这很清楚。

答案 1 :(得分:-1)

怎么样:

unsigned char val;
val = *out / 100 + 48;
write(STDOUT_FILENO, &val, 1);
val = (*out - *out / 100 * 100 ) / 10 + 48;
write(STDOUT_FILENO, &val, 1);
val = (*out - *out / 10 * 10) + 48;
相关问题