将十六进制数字打印为十进制

发布于 2024-12-08 15:02:09 字数 1419 浏览 0 评论 0原文

我正在尝试在Linux 中制作fdisk 程序。我有一个缓冲区,其中包含分区磁盘信息(起始扇区-结束扇区-分区大小...等)。

问题是我有缓冲区数组 char buf[512] 并且该缓冲区包含所需的信息,我想以十进制打印它。结果应该是dec 2048,但我的程序打印hex 0800

该程序可以看到每个数字八零零,我希望它看到八百。


代码

#define _LARGEFILE64_SOURCE
#define buff_SIZE 512

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>


int main(int argc, char ** argv){


  int fd; // File descriptor
  unsigned char buf[buff_SIZE];

  if(argc<2) {
     perror("Error: No such file or directory");
     exit(2); // File scripted: 0 input 1 output 2 error
  }

  // Open Driver::
  if((fd=open(argv[1], O_RDONLY | O_LARGEFILE ))==-1){
     perror("Error: Can not open the file");
     exit(2);
  }

  // Reading file descriptor into buffer::
  if(read(fd, buf, buff_SIZE)!=buff_SIZE)
    perror("Error: Reading error");

  if ((buf[446]==0x80)||(buf[446]==0x00))
     printf("%x\n",buf[446]);
  int i;
  for(i=447;i<=449;i++)
    printf("%x\n",buf[i]);
  printf("Partation ID:%x\n",buf[450]);
  printf("Starting Sector:%x%x%x%x\n",buf[454],buf[455],buf[456],buf[457]); //***THE PROBLEM***

 close(fd);
 return 0;}

输出

80
20
21
0
Partation ID:83
Starting Sector:0800

这是十六进制 800h 我需要它的十进制 2048

I'm trying to make fdisk program in Linux. I have a buffer which contains the partition disk information(starting sector- ending sector- size of partition...etc).

The problem that I have buffer array char buf[512] and that buffer contains the needed info and I want to print it in decimal. The result should be dec 2048 but my program print hex 0800

The program can see each digit eight zero zero and I want it to see eight hundreds.


The Code

#define _LARGEFILE64_SOURCE
#define buff_SIZE 512

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>


int main(int argc, char ** argv){


  int fd; // File descriptor
  unsigned char buf[buff_SIZE];

  if(argc<2) {
     perror("Error: No such file or directory");
     exit(2); // File scripted: 0 input 1 output 2 error
  }

  // Open Driver::
  if((fd=open(argv[1], O_RDONLY | O_LARGEFILE ))==-1){
     perror("Error: Can not open the file");
     exit(2);
  }

  // Reading file descriptor into buffer::
  if(read(fd, buf, buff_SIZE)!=buff_SIZE)
    perror("Error: Reading error");

  if ((buf[446]==0x80)||(buf[446]==0x00))
     printf("%x\n",buf[446]);
  int i;
  for(i=447;i<=449;i++)
    printf("%x\n",buf[i]);
  printf("Partation ID:%x\n",buf[450]);
  printf("Starting Sector:%x%x%x%x\n",buf[454],buf[455],buf[456],buf[457]); //***THE PROBLEM***

 close(fd);
 return 0;}

The output

80
20
21
0
Partation ID:83
Starting Sector:0800

This in hexa 800h i need it in decimal 2048

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缘字诀 2024-12-15 15:02:09
printf("Starting Sector:%lu\n",
         (unsigned long)buf[454] * 0x1000
       + (unsigned long)buf[455] * 0x100
       + (unsigned long)buf[456] * 0x10
       + (unsigned long)buf[457] * 0x1);
printf("Starting Sector:%lu\n",
         (unsigned long)buf[454] * 0x1000
       + (unsigned long)buf[455] * 0x100
       + (unsigned long)buf[456] * 0x10
       + (unsigned long)buf[457] * 0x1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文