Print Hex number as decimal
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 I开发者_开发问答D:%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
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);
精彩评论