473,748 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem printing Hexadecimal

Hi,
I am trying to read an exe file and print it out character by
character in hexadecimal format. The file goes something like this in
hexadecimal
0x4d 0x5a 0x90 0x00 0x03 .... so on
When I read it into my array " two_bytes" which has 16 characters in
total, I get each of these values read correctly, but when I attempt
printing them out, it prints something like this:
0x4d 0x5a 0xffffff90 0x00 0x03 .... so on. I have no clue why. How can
I correct this

FILE *fp = NULL;
int count = 0;
char nibble = NULL;
char two_bytes[16];
int i;
fp = fopen("xyz.exe" ,"r");
if(fp == NULL)
{
printf("Unable to Open file\n");
exit(0);
}
while(!feof(fp) )
{
fscanf(fp,"%c", &two_bytes[count++]);
if(count % 16 == 0)
{
for(i=0; i<count ; i++)
printf("%#x ",two_bytes[i]);
printf(" || ");
for(i=0; i<count ; i++)
printf("%4c ",two_bytes[i]);
count = 0;
printf("\n");
}
}
fclose(fp);
return 0;

Mar 19 '07 #1
14 2934
ab************@ gmail.com writes:
char two_bytes[16];
(...)
printf("%#x ",two_bytes[i]);
'char' can be signed. A negative char is promoted to int, and
%#x prints it as unsigned int, thus char 0x90 becomes 0xffffff90.

Use unsigned char two_bytes[16];
or printf("%#x ", (unsigned char) two_bytes[i]);

--
Regards,
Hallvard
Mar 19 '07 #2
In article <11************ *********@n59g2 000hsh.googlegr oups.com>,
<ab************ @gmail.comwrote :
I am trying to read an exe file and print it out character by
character in hexadecimal format. The file goes something like this in
hexadecimal
0x4d 0x5a 0x90 0x00 0x03 .... so on
When I read it into my array " two_bytes" which has 16 characters in
total, I get each of these values read correctly, but when I attempt
printing them out, it prints something like this:
0x4d 0x5a 0xffffff90 0x00 0x03 .... so on. I have no clue why. How can
I correct this
>FILE *fp = NULL;
int count = 0;
char nibble = NULL;
char two_bytes[16];
make two_bytes unsigned char
> int i;
fp = fopen("xyz.exe" ,"r");
if(fp == NULL)
{
printf("Unable to Open file\n");
exit(0);
}
while(!feof(fp) )
{
fscanf(fp,"%c", &two_bytes[count++]);
You've made a classic error there. feof() is not set until
you try and fail to read a character -- it does NOT "peek ahead"
to see whether the next read would fail or not. So you are going
to have to try to read the character and then determine whether
the read failed.

There is no good reason to fscanf with a single %c format:
see fgetc().
> if(count % 16 == 0)
{
for(i=0; i<count ; i++)
printf("%#x ",two_bytes[i]);
printf(" || ");
for(i=0; i<count ; i++)
printf("%4c ",two_bytes[i]);
Oh, you don't want to do that. What if it's a newline or a bell
or something like that?

You should first test that it's a printable character, and if
it is then you can putchar() it; if it isn't, then print
a placeholder instead ('.' is typical.)

Or instead of a placeholder, if it isn't important that the
text equivilent always be exactly the same length, you could
print a representation -- for example, the character pair
'\' 't' for tab, '\' 'b' for backspace. ^ followed by a letter
is a common representation for ASCII control characters.

That just leaves you with a choice of representations when
the character is above the ASCII printable range, such as if
it is 0xac ...
> count = 0;
printf("\n");
}
}
fclose(fp);
return 0;

--
Programming is what happens while you're busy making other plans.
Mar 19 '07 #3
Thank you !!! That fixed it. I realized I'd forgotten to make it a
"unsigned char"

On Mar 19, 5:13 pm, Hallvard B Furuseth <h.b.furus...@u sit.uio.no>
wrote:
abhishekkar...@ gmail.com writes:
char two_bytes[16];
(...)
printf("%#x ",two_bytes[i]);

'char' can be signed. A negative char is promoted to int, and
%#x prints it as unsigned int, thus char 0x90 becomes 0xffffff90.

Use unsigned char two_bytes[16];
or printf("%#x ", (unsigned char) two_bytes[i]);

--
Regards,
Hallvard

Mar 19 '07 #4
ab************@ gmail.com wrote:
Thank you !!! That fixed it. I realized I'd forgotten to make it a
"unsigned char"
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html >
Mar 19 '07 #5
ab************@ gmail.com wrote:
Hi,
I am trying to read an exe file and print it out character by
character in hexadecimal format. The file goes something like this in
hexadecimal
0x4d 0x5a 0x90 0x00 0x03 .... so on
When I read it into my array " two_bytes" which has 16 characters in
total, I get each of these values read correctly, but when I attempt
printing them out, it prints something like this:
0x4d 0x5a 0xffffff90 0x00 0x03 .... so on. I have no clue why.
Because the char is a small integer which can be positive or negative.
Before being used by printf, it is implicitly converted to a normal
integer. Every byte from 0x80 to 0xFF will be interpreted as a negative
number, and the print result is based on a four-byte-representation of
that number.
How can
I correct this

FILE *fp = NULL;
int count = 0;
char nibble = NULL;
char two_bytes[16];
Here: declare your bytes as unsigned char.

Good luck!

Tobias

Mar 19 '07 #6
In article <56************ *@mid.individua l.net>,
Default User <de***********@ yahoo.comwrote:
>ab************ @gmail.com wrote:
>Thank you !!! That fixed it. I realized I'd forgotten to make it a
"unsigned char"

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html >
Get a life!

Mar 19 '07 #7
On Mar 20, 9:19 am, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson)
wrote:
<abhishekkar... @gmail.comwrote :
char two_bytes[16];

make two_bytes unsigned char
fscanf(fp,"%c", &two_bytes[count++]);

There is no good reason to fscanf with a single %c format:
see fgetc().
Well, fgetc() returns a value in the range for unsigned char.
If he were still reading into a char, then using fgetc() would
cause implementation-defined behaviour due to out-of-range
assignment. However, fscanf with "%c" does not suffer that same
problem.
Mar 20 '07 #8
Old Wolf wrote:
>
On Mar 20, 9:19 am, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson)
wrote:
<abhishekkar... @gmail.comwrote :
char two_bytes[16];
make two_bytes unsigned char
fscanf(fp,"%c", &two_bytes[count++]);
There is no good reason to fscanf with a single %c format:
see fgetc().

Well, fgetc() returns a value in the range for unsigned char.
If he were still reading into a char, then using fgetc() would
cause implementation-defined behaviour due to out-of-range
assignment.
However, fscanf with "%c" does not suffer that same problem.
fgetc returns type int
and is capable of returning a negative value equal to EOF.

--
pete
Mar 20 '07 #9
In article <11************ **********@o5g2 000hsb.googlegr oups.com>,
Old Wolf <ol*****@inspir e.net.nzwrote:
>On Mar 20, 9:19 am, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson)
wrote:
> <abhishekkar... @gmail.comwrote :
char two_bytes[16];
>make two_bytes unsigned char
fscanf(fp,"%c", &two_bytes[count++]);
>There is no good reason to fscanf with a single %c format:
see fgetc().
>Well, fgetc() returns a value in the range for unsigned char.
If he were still reading into a char, then using fgetc() would
cause implementation-defined behaviour due to out-of-range
assignment.
You cannot "read into a char" when using fgetc(): you have
to assign its value to something. You would take the usual
precautions to assign to an int, check the value for EOF, and
only then assign from the int into the unsigned char.

>However, fscanf with "%c" does not suffer that same problem.
You would have to check the return value of fscanf(), which
is going to return EOF when the input ends. If you are prone
to make the mistake of assigning the return value to an unsigned
value for fgetc(), you are equally likely to be prone to assigning
the return value from fscanf() to an unsigned int.
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Mar 20 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1826
by: Pekka Niiranen | last post by:
Hi there, how can I write out Python Unicode character's hexadecimal value in generic format? I need to loop thru characters in Unicode string and store each character in format \U+hhhh, where hhhh is the value of unicode character in hexadecimal? For example string:
6
13849
by: Enrico `Trippo' Porreca | last post by:
Suppose I wanted to print an unsigned char (in hex). Should I say: unsigned char x = 0x12; printf("%X\n", x); or printf("%X\n", (unsigned) x);
10
32513
by: Maxime Barbeau | last post by:
Hi. I have the following code: uint32_t quadlet = 0x12345678; printf("quadlet = %08X\n", quadlet); my compiler give me the following warning: unsigned int format, uint32_t arg (arg 2) Is there any formtat I can use? %08uX ?
3
2293
by: buzzdee | last post by:
hi, i just wanted to print out some unsigned long int values in hexadecimal, printing out one value works, but not byte by byte. anybody has a suggestinon what my problem is? this is my program:
2
2531
by: Fernando Barsoba | last post by:
Dear all, I have been posting about a problem trying to encrypt certain data using HMAC-SHA1 functions. I posted that my problem was solved, but unfortunately, I was being overly optimistic. I am really desperate now, because I havent' been able to locate the origin of the problem for a couple of days now.. PROBLEM: the message digest obtained differs each time I execute the code, but works perfectly when applying the "control", that...
2
2765
by: KhzQas | last post by:
Hello mates. I am taking my very first programming language (C Programming) and as for practice purposes, I surf sites and try out to make programs. I am having problems with the following C Programming problem. 1) You are required to use at least one for() loop to control the printing of a table in this lab. 2) Do not use the scanf() function in this lab, use the getchar() instead. 3) Your table will consist of three data type...
11
6954
by: MSNEWS | last post by:
HI Public Enum WindowStyles As UInteger WS_OVERLAPPED = 0x00000000 WS_POPUP = 0x80000000 WS_CHILD = 0x40000000 end enum In the above i get an error at
6
14637
by: Andrea | last post by:
Hi, suppose that I have a string that is an hexadecimal number, in order to print this string I have to do: void print_hex(unsigned char *bs, unsigned int n){ int i; for (i=0;i<n;i++){ printf("%02x",bs); } }
13
3597
by: rsk | last post by:
Hi Friends, My requirement is as follows; A file is consisting of data in hexadecimal format(i.e a 32 bit data for example like "0xdeadbeef"). I have to read each of such data into my 'c' code and i need to assign them to a 32 bit integer array.
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8243
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.