将十六进制数字打印为十进制
我正在尝试在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)