LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-21-2014, 10:24 AM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Rep: Reputation: 76
How do I print the value of a byte in C?


Hi: Let's say I read 512 bytes from a sector into variable sector.
Code:
unsigned char sector[512];
FILE * infile;

int main(){
    .................
    infile= fopen("/dev/sda", "r");
    fread(sector, sizeof(sector), 1, infile);
    .................
What I read into sector could be something like
Code:
00000000  fa eb 21 01 b4 01 4c 49  4c 4f 17 02 76 1e c4 53  |úë!.´.LILO..v.ÄS|
00000010  00 00 00 00 57 b9 e5 01  42 07 f3 4c 81 00 80 60  |....W¹å.B.óL...`|
Now, how could I print the contents of sector? The first byte is 0xfa. So, I should print "f" and "a" to see the first byte. How do I translate 0xfa to the string "fa"?

|Assuming the byte is in c, of type char, I could do
Code:
char car;
car = c >> 4;
if(car > 9) car = car + 7;
car = car + 0x30 // "0" is ASCII char 0x30
printf("%s", car);
And I can print the low nibble in an analogous way. And repeat this operation for each of the 512 bytes in sector. But is there not a more simple way to do this?
 
Old 07-21-2014, 10:32 AM   #2
gengisdave
Member
 
Registered: Dec 2013
Location: Turin, Italy
Distribution: slackware
Posts: 328

Rep: Reputation: 74
use printf("%x", car);
 
Old 07-21-2014, 10:59 AM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
use printf("%02x", car);
 
Old 07-21-2014, 11:09 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
use

Code:
printf("%02x", (unsigned char)car);
 
Old 07-21-2014, 11:14 AM   #5
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Thank you very much. I could print the contents of the MBR this way! First time I do something like this in Linux. What is weird is that the program does not need to get any kind of permissions to read the hard disk sectors. The only provision was to run it as root! I wonder what would happen if I open the device for write instead of read.

@NevemTeve: I already took the precaution of declaring car of type unsigned char. But it took me a while to realize I should not declare it just char.

Last edited by stf92; 07-21-2014 at 11:18 AM.
 
Old 07-21-2014, 11:25 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> The only provision was to run it as root!

No biggie, huh?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] memcpy fails to copy data, but byte by byte assignment work venu_s Programming 7 07-08-2011 03:29 AM
Need to copy an entire disk byte-for-byte Pawprint Linux - Software 6 06-16-2011 11:01 AM
byte to byte remote comparison louisJ Linux - Newbie 3 09-21-2007 05:28 PM
backup byte-for-byte axion0917 Linux - Software 2 12-11-2003 05:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration