Converting byte data to hexadecimal ascii

M

Mike Jeffers

Hi everyone,

I need to convert data from a structure into hexadecimal ascii format.

The structure is like this:

struct ROOM_DATA {
short room_number;
short floor_number;
long total_area;
char comment[42];
};

The structure has different data types so they would occupy different
number of bytes.

The question is if there is a way this data can be printed out in
hexadecimal ascii format?

I'm not sure if there are already functions in C which can do this or
I have to write one myself.

Thanks for any help given.
 
J

Jack Klein

Hi everyone,

I need to convert data from a structure into hexadecimal ascii format.

The structure is like this:

struct ROOM_DATA {
short room_number;
short floor_number;
long total_area;
char comment[42];
};

The structure has different data types so they would occupy different
number of bytes.

The question is if there is a way this data can be printed out in
hexadecimal ascii format?

I'm not sure if there are already functions in C which can do this or
I have to write one myself.

Thanks for any help given.

Consult your C book for the "%x" conversion specifier of the printf()
function. It does hexadecimal quite well. It even does it in ASCII,
if that is the execution character set of your implementation. Which
is quite likely.
 
R

Robert B. Clark

I need to convert data from a structure into hexadecimal ascii format.

The structure is like this:

struct ROOM_DATA {
short room_number;
short floor_number;
long total_area;
char comment[42];
};

The structure has different data types so they would occupy different
number of bytes.

The question is if there is a way this data can be printed out in
hexadecimal ascii format?

There is no built-in function in C to do this, but you can easily write one
yourself.

Here's a proposed declaration:

void dumpobj(const void *object, size_t objsize);

This function could be used to dump the contents of any arbitrarily-sized
data type.

In dumpobj, simply walk through the object in a character-by-character
fashion, displaying each character as a hex number. You can get as fancy
as you wish--display the data as both hex and ASCII characters, justify the
dump in x-byte paragraphs, etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top