newbie: Converting Buffer data to Hexadecimal

D

DSKR

Hi all,
I am new to C and have been fiddling with a problem.I have a pointer to a
'big' buffer of raw binary data.What is the ideal way of converting this
data to Hexadecimal format.I need each 'Byte' to be converted to the
equivalent Hex code.Any help is appreciated.
Thanks in Advance
Regards,
DSKR
 
N

Neil Kurzman

DSKR said:
Hi all,
I am new to C and have been fiddling with a problem.I have a pointer to a
'big' buffer of raw binary data.What is the ideal way of converting this
data to Hexadecimal format.I need each 'Byte' to be converted to the
equivalent Hex code.Any help is appreciated.
Thanks in Advance
Regards,
DSKR

for(X=0;X<dataBytes;X++)
{
printf("%X",hexBuffer[X]);
}
Assuming you want to print them
 
P

Pierre Maurette

DSKR said:
Hi all,
I am new to C and have been fiddling with a problem.I have a pointer to a
'big' buffer of raw binary data.What is the ideal way of converting this
data to Hexadecimal format.I need each 'Byte' to be converted to the
equivalent Hex code.Any help is appreciated.
Thanks in Advance
Regards,
DSKR
Hello,
("Hex" is just a text notion)
I assume unsigned char is 8 bits wide and unsigned int is 32 bits
wide.
I assume the buffer may be declared as other type than a unsigned char
array. So, be carefull with pointers arithmetics.
I assume you want to see the memory as "bytes" (as "words" may be
different, depending on endianess).

Try this:
<code>
typedef unsigned char ubyte;

#define NB 10
unsigned int ActualBuffer[NB]; /* just for test */
const size_t SZ = NB * sizeof(unsigned int);

ubyte* WorkBuffer = (ubyte*)ActualBuffer;


ActualBuffer[0] = 0x12345678; /* just for test */

for(compteur = 0; compteur < (int)SZ; ++compteur)
{
printf("%02X.", WorkBuffer[compteur]);
}
</code>

"%02X."
0 : fill with leading 0
2 : number of digits, 4 for words.
X : uppercase hexa, x for lowercase hexa
.. : just a separator, if you want.

Depending of what you need, you can use sprintf() and a double loop to
generate "lines".
 
C

CBFalconer

DSKR said:
I am new to C and have been fiddling with a problem.I have a
pointer to a 'big' buffer of raw binary data.What is the ideal
way of converting this data to Hexadecimal format.I need each
'Byte' to be converted to the equivalent Hex code.Any help is
appreciated.

for (i = 0; i < SIZE; i++) {
fprintf(stdout, "%2x ", buffer);
if (0 == (i+1) % PERLINE) putc('\n', stdout);
}
if (0 != (i+1) % PERLINE) putc('\n', stdout);

(assuming the value of CHAR_BIT is 8 and suitable defines for SIZE
and PERLINE)
 
K

Karthik

DSKR said:
Hi all,
I am new to C and have been fiddling with a problem.I have a pointer to a
'big' buffer of raw binary data.What is the ideal way of converting this
data to Hexadecimal format.I need each 'Byte' to be converted to the
equivalent Hex code.Any help is appreciated.
Thanks in Advance
Regards,
DSKR

You may want to look at this question in a larger perspective as to
what you really need to do, because if you want to print (then the
options suggested by Neil, Pierre and CBFalconer does work pretty well
), but if you are thinking about processing the data - you may want to
think about the data structure design.

HTH
 
D

Darrell Grainger

Hi all,
I am new to C and have been fiddling with a problem.I have a pointer to a
'big' buffer of raw binary data.What is the ideal way of converting this
data to Hexadecimal format.I need each 'Byte' to be converted to the
equivalent Hex code.Any help is appreciated.

Common problem with new programmers. Everything is binary data. You can
display it as hexidecimal format for the benefit of humans. For example,
if I have:

int a = 234;

I can print it as hexidecimal using:

printf("%X\n", a);

This will print EA. The only time I need to 'convert' binary to
hexidecimal is when I'm dealing with strings. If I have the string:

char s[] = "10011101";

and I want to convert it to a hexidecimal string (9D) it becomes a little
harder. If I have it in binary form and I want to create a hexidecimal
formatted string then I can just use sprintf rather than printf. I.e.

#include <limits.h>

int a = 234;
char hex[sizeof a * CHAR_BIT / 4 + 1];
sprintf(hex, "%X\n", a);

The only tricky thing here is the size of the hex array. (sizeof a) will
be the number of bytes in a. CHAR_BIT will be the number of bits per
bytes. I know that one hexidecimal digit is 4 bits. So I divide the total
bits by 4. The +1 is for the null character that is needed at the end of
every string.
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top