By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,068 Members | 913 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,068 IT Pros & Developers. It's quick & easy.

How to access the individual bytes of LONGLONG value

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

Mar 21 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
On 2006-03-21, shaji <sh***********@gmail.com> wrote:
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.
Mar 21 '06 #2

P: n/a
shaji wrote:
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.

Mar 21 '06 #3

P: n/a
sl*******@yahoo.com schrieb:
shaji wrote:
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[i] = (number >> (i*CHAR_BIT)) & (unsigned char)~0UL

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Mar 21 '06 #4

P: n/a
Michael Mair wrote:

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[i] = (number >> (i*CHAR_BIT)) & (unsigned char)~0UL


bytes[i] ?

--
Ian Collins.
Mar 21 '06 #5

P: n/a
shaji wrote:
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[i]);
putchar('\n');
/* if you malloced foo, then free it here */
}

This approach holds for some non-standard Windows type as well as
standard types.
Mar 21 '06 #6

P: n/a
On 2006-03-21, Martin Ambuhl <ma*****@earthlink.net> wrote:
shaji wrote:
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[i]);
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.

Mar 21 '06 #7

P: n/a
"shaji" <sh***********@gmail.com> writes:
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;
}
Mar 21 '06 #8

P: n/a
Ian Collins schrieb:
Michael Mair wrote:

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[i] = (number >> (i*CHAR_BIT)) & (unsigned char)~0UL


bytes[i] ?


Of course -- I just copied (oh, you snipped the attribution)'s
identifiers without thinking twice.
Thank you for catching that one :-)
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Mar 21 '06 #9

P: n/a
Richard G. Riley schrieb:
On 2006-03-21, Martin Ambuhl <ma*****@earthlink.net> wrote:
shaji wrote:
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[i]);
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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Mar 21 '06 #10

P: n/a
Richard G. Riley wrote:
On 2006-03-21, Martin Ambuhl <ma*****@earthlink.net> wrote:
shaji wrote:
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[i]);
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.

Mar 22 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.