Velocity Reviews > How to extract bytes from long?

# How to extract bytes from long?

RB
Guest
Posts: n/a

 10-15-2003
How to extract bytes from long, starting from the last byte?
For example, I have a long number:
0x12345678
I need to represent it as the following bytes list:
0x78, 0x56, 0x34, 0x12

Rita

Ed Morton
Guest
Posts: n/a

 10-15-2003

RB wrote:
> How to extract bytes from long, starting from the last byte?
> For example, I have a long number:
> 0x12345678
> I need to represent it as the following bytes list:
> 0x78, 0x56, 0x34, 0x12
>
> Rita

Sounds a bit homework-y, but as a hint try this:

#include <stdio.h>

int main(void)
{
long val=0x12345678;

printf("0x%x\n",val >> 16 & 0xff );

return 0;
}

Regards,

Ed.

Guest
Posts: n/a

 10-15-2003
On Wed, 15 Oct 2003 01:59:04 -0700, RB wrote:

> How to extract bytes from long, starting from the last byte?

#include <stdio.h>

int main() {

unsigned long value = 0x12345678;
int i;

printf("%#lx\n",value);

for (i = sizeof value; i > 0; --i) {
printf("%#x\n", value & 0xff);
value >>= 8;
}
return 0;
}

--
NPV

"the large print giveth, and the small print taketh away"
Tom Waits - Step right up

Thomas Stegen
Guest
Posts: n/a

 10-15-2003
RB wrote:
> How to extract bytes from long, starting from the last byte?
> For example, I have a long number:
> 0x12345678
> I need to represent it as the following bytes list:
> 0x78, 0x56, 0x34, 0x12
>
> Rita

Look at bitwise operators. &, |, ^, <<, >>. Then look up bitmasks
and how to use them and everything should be rather straightforward.

--
Thomas.
"What is the future c existence which does not do in all languages"

pete
Guest
Posts: n/a

 10-15-2003
>
> On Wed, 15 Oct 2003 01:59:04 -0700, RB wrote:
>
> > How to extract bytes from long, starting from the last byte?

>

#include <limits.h>
> #include <stdio.h>
>
> int main() {
>
> unsigned long value = 0x12345678;
> int i;
>
> printf("%#lx\n",value);
>
> for (i = sizeof value; i > 0; --i) {
> printf("%#x\n", value & 0xff);
> value >>= 8;

/*
** You realise that you don't know the size of value,
** so you might as well go all the way.
*/
printf("%#x\n", value & (unsigned char)-1);
value >>= CHAR_BIT;

> }
> return 0;
> }

--
pete

pete
Guest
Posts: n/a

 10-15-2003
pete wrote:
>
> >
> > On Wed, 15 Oct 2003 01:59:04 -0700, RB wrote:
> >
> > > How to extract bytes from long, starting from the last byte?

.... and now for the (sizeof(long)==1) portable way:

/* BEGIN new.c */

#include <stdio.h>
#include <limits.h>

int main(void)
{
unsigned long value = 0x12345678;
size_t i;

printf("%#lx\n",value);
printf("%#x\n", value & (unsigned char)-1);
for (i = sizeof value - 1; i != 0; --i) {
value >>= CHAR_BIT;
printf("%#x\n", value & (unsigned char)-1);
}
return 0;
}

/* END new.c */

and now for the way which interprets "last byte"
as being the one furthest from ((char*)&value)

/* BEGIN new2.c */

#include <stdio.h>

int main(void)
{
unsigned long value = 0x12345678;
unsigned char *pointer = (unsigned char*)&value + sizeof value;

printf("%#lx\n",value);
do {
--pointer;
printf("%#x\n", (unsigned)*pointer);
} while (pointer != (unsigned char*)&value);
return 0;
}

/* END new2.c */

--
pete

John Bode
Guest
Posts: n/a

 10-15-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (RB) wrote in message news:<(E-Mail Removed) om>...
> How to extract bytes from long, starting from the last byte?
> For example, I have a long number:
> 0x12345678
> I need to represent it as the following bytes list:
> 0x78, 0x56, 0x34, 0x12
>
> Rita

There are a couple of ways. The safest is to use a bitmask (0xFF),
the bitwise & operator, and the >> and << shift operators.
Alternately, you can treat the long as an array of unsigned char by
creating an unsigned char pointer and setting it to the same address
as the long (unsigned char *p = (unsigned char *) &mylong and then
either use array subscript notation to access individual bytes or
"walk" the array by incrementing p, but you have to be aware of
endianness issues (i.e., on a little-endian machine, p[0] would be the
LSB, whereas on a big-endian machine it would be the MSB). The first
method (bitmask and shift) works the same regardless of endian issues.

Peter Nilsson
Guest
Posts: n/a

 10-16-2003
pete <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> pete wrote:
> >
> > Nils Petter Vaskinn wrote:
> > >
> > > On Wed, 15 Oct 2003 01:59:04 -0700, RB wrote:
> > >
> > > > How to extract bytes from long, starting from the last byte?

>
> ... and now for the (sizeof(long)==1) portable way:

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>
> /* BEGIN new.c */
>
> #include <stdio.h>
> #include <limits.h>
>
> int main(void)
> {
> unsigned long value = 0x12345678;
> size_t i;
>
> printf("%#lx\n",value);
> printf("%#x\n", value & (unsigned char)-1);
> for (i = sizeof value - 1; i != 0; --i) {
> value >>= CHAR_BIT;

UB if sizeof(long) == 1.

value = value >> (CHAR_BIT - 1) >> 1;

> printf("%#x\n", value & (unsigned char)-1);
> }
> return 0;
> }
>
> /* END new.c */

--
Peter

CBFalconer
Guest
Posts: n/a

 10-16-2003
pete wrote:
> > On Wed, 15 Oct 2003 01:59:04 -0700, RB wrote:
> >
> > > How to extract bytes from long, starting from the last byte?

>
> #include <limits.h>
> > #include <stdio.h>
> >
> > int main() {
> >
> > unsigned long value = 0x12345678;
> > int i;
> >
> > printf("%#lx\n",value);
> >
> > for (i = sizeof value; i > 0; --i) {
> > printf("%#x\n", value & 0xff);
> > value >>= 8;

>
> /*
> ** You realise that you don't know the size of value,
> ** so you might as well go all the way.
> */
> printf("%#x\n", value & (unsigned char)-1);
> value >>= CHAR_BIT;
>
> > }
> > return 0;
> > }

You need neither CHAR_BIT nor shifts nor limits.h nor sizeof:

for (i = 8; i > 0; --i) {
printf("%x ", value % 256);
value /= 256;
}
putchar('\n'); /* <--AND HERE is where the \n goes */
return 0;
}

and the result is portable.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.

Samuel Barber
Guest
Posts: n/a

 10-16-2003
pete <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> printf("%#x\n", value & (unsigned char)-1);
> for (i = sizeof value - 1; i != 0; --i) {
> value >>= CHAR_BIT;
> printf("%#x\n", value & (unsigned char)-1);
> }
> return 0;
> }

I would use ~0 for "all 1s" rather than -1.

Sam