Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing Char Strings as Hexadecimal 1

Status
Not open for further replies.

rudejohn

IS-IT--Management
Jul 11, 2003
130
US
I'm relatively new to C so I appreciate your assistance for my novice question.

I have a struct pointing to some data. Let's say...

typedef struct
{
unsigned char var1[6];
unsigned char var2[6];
} MY_STRUCT;

Now, I'm pointing this struct at some hexadecimal data.

MY_STRUCT *tmpStruct;
tmpStruct = (MY_STRUCT*)ptrWherever;

My question is, how can I reference the items in this structure and print the hexadecimal results? I've done the following:

printf("%06x \n", tmpStruct->var1);

What I would LIKE to see would be something like:
00062977B1AD (6 pairs in a MAC address)
Instead, I'm getting:
A02798

Am I translating it incorrectly? Thanks in advance,

RJ

************
RudeJohn
************
 
Strictly speaking you are not pointing the structure at some hexadecimal data as computers only store binary. You may be pointing at a sequence of binary values that could be represented as a hexadecimal value by a series of ASCII digits and the letters A to F.

To represent them as hexadecimal you'll need to process each unsigned char, in a loop over the six entries in the array, by mapping the lower four bits to a character representing a hex digit and then again with the upper four bits. Each four bits becoming an eight bit character you'll need a 13 item char array to store then in and put a zero in the last element.

Now that array can be printed as a string.

==========================================
toff.jpg
Some cause happiness wherever they go; others whenever they go.
 
This will print the address of tmpStruct->var1
Code:
printf("%06x \n", tmpStruct->var1);
If you want the contents of var1, you'll have to do
Code:
/* assuming vv has been declared somewhere */
for (vv = 0; vv < 6; ++vv)
   printf ("%02x ", tmpStruct->var1[vv]);
printf ("\n");
and the same thing for var2
 
Great answers: Thank you, much obliged! Being a Java programmer by training I'm unfortunately weak on pointers and data storage.

************
RudeJohn
************
 
Very similar to code already posted:
Code:
#include <stdio.h>

typedef struct
{
   unsigned char var1[6];
   unsigned char var2[6];
} MY_STRUCT;

int main(void)
{
   MY_STRUCT my_struct = 
   {
      {0x00,0x06,0x29,0x77,0xB1,0xAD},
      {0x00,0x06,0x29,0x77,0xB1,0xAD},
   };
   size_t i;
   for ( i = 0; i < sizeof my_struct.var1 / sizeof *my_struct.var1; ++i )
   {
      printf("%02X ", (unsigned)my_struct.var1[i]);
   }
   putchar('\n');
   return 0;
}

/* my output
00 06 29 77 B1 AD 
*/
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top