How to access the individual bytes of LONGLONG value

S

shaji

Hi,
Could anybody tell me how to access the individual bytes that consists
of a Windows LONGLONG value?
 
J

Jordan Abel

Hi, Could anybody tell me how to access the individual bytes that
consists of a Windows LONGLONG value?

Same way you access the individual bytes for any type.
 
S

slebetman

shaji said:
Hi,
Could anybody tell me how to access the individual bytes that consists
of a Windows LONGLONG value?

The protable solution is to use bit shifts:

unsigned char bytes[8]; /* 0 is least significant, 7 is most
significant */
unsigned long long number;

/* get least significant byte */
char[0] = number & 0xff;
/* get next byte */
char[1] = (number >> 8) & 0xff;

/* get most significant byte */
char[7] = (number >> 56) & 0xff;

Of course, you can use a for loop for this but I'm leaving that as an
exercise to the OP.
 
M

Michael Mair

shaji said:
Hi,
Could anybody tell me how to access the individual bytes that consists
of a Windows LONGLONG value?

The protable solution is to use bit shifts:

unsigned char bytes[8]; /* 0 is least significant, 7 is most
significant */
unsigned long long number;

The portable solution probably would read
unsigned long long number;
unsigned char bytes[sizeof number];
/* get least significant byte */
char[0] = number & 0xff;
/* get next byte */
char[1] = (number >> 8) & 0xff;

/* get most significant byte */
char[7] = (number >> 56) & 0xff;

Of course, you can use a for loop for this but I'm leaving that as an
exercise to the OP.

The loop is necessary for the portable solution.
char = (number >> (i*CHAR_BIT)) & (unsigned char)~0UL

Cheers
Michael
 
I

Ian Collins

Michael said:
The portable solution probably would read
unsigned long long number;
unsigned char bytes[sizeof number];
/* get least significant byte */
char[0] = number & 0xff;
/* get next byte */
char[1] = (number >> 8) & 0xff;

/* get most significant byte */
char[7] = (number >> 56) & 0xff;

Of course, you can use a for loop for this but I'm leaving that as an
exercise to the OP.


The loop is necessary for the portable solution.
char = (number >> (i*CHAR_BIT)) & (unsigned char)~0UL


bytes ?
 
M

Martin Ambuhl

shaji said:
Hi,
Could anybody tell me how to access the individual bytes that consists
of a Windows LONGLONG value?

If there is a type available
sometype_t
one can do something like:

#include <stdio.h>
#include <string.h>
void show_sometype_by_byte (sometype_t bar)
{
unsigned char foo[sizeof bar];
size_t i;
/* if your compiler doesn't like this, make
foo an 'unsigned char *foo' and malloc
the space */
memcpy(foo, &bar, sizeof bar);
for (i = 0; i < sizeof bar; i++)
printf("%#o ", foo);
putchar('\n');
/* if you malloced foo, then free it here */
}

This approach holds for some non-standard Windows type as well as
standard types.
 
R

Richard G. Riley

shaji said:
Hi,
Could anybody tell me how to access the individual bytes that consists
of a Windows LONGLONG value?

If there is a type available
sometype_t
one can do something like:

#include <stdio.h>
#include <string.h>
void show_sometype_by_byte (sometype_t bar)
{
unsigned char foo[sizeof bar];
size_t i;
/* if your compiler doesn't like this, make
foo an 'unsigned char *foo' and malloc
the space */
memcpy(foo, &bar, sizeof bar);
for (i = 0; i < sizeof bar; i++)
printf("%#o ", foo);
putchar('\n');
/* if you malloced foo, then free it here */
}

This approach holds for some non-standard Windows type as well as
standard types.


Why do you do the memcpy? Some sort of alignment issue?

Why not just read straight from (char *)&bar using array index notation?

Since he is probably interested in lsb & msb ordering we
probably need to take into account endian issues to ensure that we are
displaying the bytes in correct order for a concrete type.
 
M

Micah Cowan

shaji said:
Hi,
Could anybody tell me how to access the individual bytes that consists
of a Windows LONGLONG value?

Here's an example that uses the more standard long long type. It can
be modified for use with any type at all.

#include <stdio.h>
#include <stdlib.h>

int print_bytes(void *vp, size_t s)
{
unsigned char *cur = vp, *end = cur+s;
int rv = 0, t;

t = printf("Byte values at address %p:\n", vp);
if (t < 0) return t;
rv += t;
while (cur != end) {
t = printf(" 0x%02X\n",*cur);
if (t < 0) return t;
rv += t;
++cur;
}
t = putchar('\n');

if (t == EOF) return t;
else return ++rv;
}

int main(void)
{
long long foo = 215;

return print_bytes(&foo, sizeof foo) > 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
 
M

Michael Mair

Ian said:
Michael said:
The portable solution probably would read
unsigned long long number;
unsigned char bytes[sizeof number];
/* get least significant byte */
char[0] = number & 0xff;
/* get next byte */
char[1] = (number >> 8) & 0xff;

/* get most significant byte */
char[7] = (number >> 56) & 0xff;

Of course, you can use a for loop for this but I'm leaving that as an
exercise to the OP.



The loop is necessary for the portable solution.
char = (number >> (i*CHAR_BIT)) & (unsigned char)~0UL


bytes ?


Of course -- I just copied (oh, you snipped the attribution)'s
identifiers without thinking twice.
Thank you for catching that one :)


Cheers
Michael
 
M

Michael Mair

Richard said:
shaji said:
Hi,
Could anybody tell me how to access the individual bytes that consists
of a Windows LONGLONG value?

If there is a type available
sometype_t
one can do something like:

#include <stdio.h>
#include <string.h>
void show_sometype_by_byte (sometype_t bar)
{
unsigned char foo[sizeof bar];
size_t i;
/* if your compiler doesn't like this, make
foo an 'unsigned char *foo' and malloc
the space */
memcpy(foo, &bar, sizeof bar);
for (i = 0; i < sizeof bar; i++)
printf("%#o ", foo);
putchar('\n');
/* if you malloced foo, then free it here */
}

This approach holds for some non-standard Windows type as well as
standard types.


Why do you do the memcpy? Some sort of alignment issue?


Probably because Martin wanted to copy the representation.
Why not just read straight from (char *)&bar using array index notation?

(unsigned char*)&bar, please -- this may make a difference
if char effectively is signed char and there are padding bits.
Since he is probably interested in lsb & msb ordering we
probably need to take into account endian issues to ensure that we are
displaying the bytes in correct order for a concrete type.

This assumes non-portable knowledge.
There is no really portable endianness test. Especially if you
store
('A' << 3*CHAR_BIT) + ('B' << 2*CHAR_BIT)
+ ('C' << 1*CHAR_BIT) + ('D' << 0*CHAR_BIT)
and get a representation like "BADC"... ;-)

Cheers
Michael
 
M

Martin Ambuhl

Richard said:
shaji said:
Hi,
Could anybody tell me how to access the individual bytes that consists
of a Windows LONGLONG value?

If there is a type available
sometype_t
one can do something like:

#include <stdio.h>
#include <string.h>
void show_sometype_by_byte (sometype_t bar)
{
unsigned char foo[sizeof bar];
size_t i;
/* if your compiler doesn't like this, make
foo an 'unsigned char *foo' and malloc
the space */
memcpy(foo, &bar, sizeof bar);
for (i = 0; i < sizeof bar; i++)
printf("%#o ", foo);
putchar('\n');
/* if you malloced foo, then free it here */
}

This approach holds for some non-standard Windows type as well as
standard types.



Why do you do the memcpy? Some sort of alignment issue?


The idea is to have an approach that allows you to cleanly go both ways
in the conversion without any possible problem and to manipulate each
byte, if needed. I find directly manipulating bytes within a larger
arithmetic type distasteful, but your mileage may vary. You are
correct: for the actual case, my approach is overkill for simply showing
the bytes, though.
Why not just read straight from (char *)&bar using array index notation?

If you had written "(unsigned char *)&bar", I would just refer you to
the above. However, the (char *) cast risks signedness problems.
Since he is probably interested in lsb & msb ordering we
probably need to take into account endian issues to ensure that we are
displaying the bytes in correct order for a concrete type.

Then my approach, assuming that you can find out the ordering, has more
in its favor, I think.
 

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,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top