473,890 Members | 1,359 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to extract bytes from long?

RB
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

Thanks in advance,
Rita
Nov 13 '05
44 12810
op*****@yahoo.c om (Samuel Barber) wrote in message news:<37******* *************** ****@posting.go ogle.com>...
pete <pf*****@mindsp ring.com> wrote in message news:<3F******* ****@mindspring .com>...
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.


That risks a trap representation under C99.

--
Peter
Nov 13 '05 #11
Peter Nilsson wrote:

pete <pf*****@mindsp ring.com> wrote in message news:<3F******* ****@mindspring .com>...
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.


No.
If sizeof(long) == 1,
then the loop doesn't execute.
printf("%#x\n", value & (unsigned char)-1);
}
return 0;
}

/* END new.c */


--
pete
Nov 13 '05 #12
ai***@acay.com. au (Peter Nilsson) wrote in message news:<63******* *************** ****@posting.go ogle.com>...
op*****@yahoo.c om (Samuel Barber) wrote in message news:<37******* *************** ****@posting.go ogle.com>...
pete <pf*****@mindsp ring.com> wrote in message news:<3F******* ****@mindspring .com>...
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.


That risks a trap representation under C99.


Nonsense. ~0 is idiomatic C. Aren't you worried about the "risk" that
-1 may not be implemented as all-1s? That is, after all, an
implementation detail. It's not true for sign-magnitude or 1's
complement, for example.

Sam
Nov 13 '05 #13
CBFalconer wrote:

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?


#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.


I was addressing the more general subject,
in the subject line of this thread: "How to extract bytes from long?",
rather than how to extract bytes from 0x12345678
or any other number which doesn't require more than 32 bits.

--
pete
Nov 13 '05 #14
Samuel Barber wrote:

ai***@acay.com. au (Peter Nilsson) wrote in message news:<63******* *************** ****@posting.go ogle.com>...
op*****@yahoo.c om (Samuel Barber) wrote in message news:<37******* *************** ****@posting.go ogle.com>...
pete <pf*****@mindsp ring.com> wrote in message news:<3F******* ****@mindspring .com>...
> 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.


That risks a trap representation under C99.


Nonsense. ~0 is idiomatic C. Aren't you worried about the "risk" that
-1 may not be implemented as all-1s? That is, after all, an
implementation detail. It's not true for sign-magnitude or 1's
complement, for example.


You're wrong about everything.

~0 is negative zero in ones complement.
Implementations are allowed trap negative zero.

-1 is a value, not a bit pattern.
The value of negative one, cast to unsigned char, is UCHAR_MAX.

--
pete
Nov 13 '05 #15
On Fri, 17 Oct 2003 02:44:03 +0000, pete wrote:
Samuel Barber wrote:

ai***@acay.com. au (Peter Nilsson) wrote in message news:<63******* *************** ****@posting.go ogle.com>...
> op*****@yahoo.c om (Samuel Barber) wrote in message news:<37******* *************** ****@posting.go ogle.com>...
> > pete <pf*****@mindsp ring.com> wrote in message news:<3F******* ****@mindspring .com>...
> > > 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.
>
> That risks a trap representation under C99.
Nonsense. ~0 is idiomatic C. Aren't you worried about the "risk" that
-1 may not be implemented as all-1s? That is, after all, an
implementation detail. It's not true for sign-magnitude or 1's
complement, for example.


You're wrong about everything.

~0 is negative zero in ones complement.
Implementations are allowed trap negative zero.


~0u cannot be a trap representation, however.

6.5.3.3
4 The result of the ~ operator is the bitwise complement of its
(promoted) operand ... If the promoted type is an unsigned type,
the expression ~E is equivalent to the maximum value representable
in that type minus E.
-1 is a value, not a bit pattern.
The value of negative one, cast to unsigned char, is UCHAR_MAX.


Nov 13 '05 #16
pete <pf*****@mindsp ring.com> wrote in message news:<3F******* ****@mindspring .com>...
Samuel Barber wrote:

ai***@acay.com. au (Peter Nilsson) wrote in message news:<63******* *************** ****@posting.go ogle.com>...
op*****@yahoo.c om (Samuel Barber) wrote in message news:<37******* *************** ****@posting.go ogle.com>...
> pete <pf*****@mindsp ring.com> wrote in message news:<3F******* ****@mindspring .com>...
> > 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.

That risks a trap representation under C99.
Nonsense. ~0 is idiomatic C. Aren't you worried about the "risk" that
-1 may not be implemented as all-1s? That is, after all, an
implementation detail. It's not true for sign-magnitude or 1's
complement, for example.


You're wrong about everything.

~0 is negative zero in ones complement.
Implementations are allowed trap negative zero.


You're just repeating words, without any understanding. There's no
such thing as "negative zero" (or negative anything) in the context of
bitwise operations. How can a bitwise operation trap? It can't.
-1 is a value, not a bit pattern.
The value of negative one, cast to unsigned char, is UCHAR_MAX.


Hello? The point is that -1 is ***being used as*** a bit pattern. The
intent is to get "all 1s", which is true if the integer representation
is 2's complement; that's an implicit assumption of the code. (This is
the best reason not to use -1: the intent is not perfectly clear).

Sam
Nov 13 '05 #17

On Fri, 16 Oct 2003, Samuel Barber wrote:

pete wrote in message news:<3F******* ****@mindspring .com>...
Samuel Barber wrote:
(Peter Nilsson) wrote...
> Samuel Barber wrote...
> > pete wrote in message news:<3F******* ****@mindspring .com>...
> > >
> > > printf("%#x\n", value & (unsigned char)-1);
> >
> > I would use ~0 for "all 1s" rather than -1.
>
> That risks a trap representation under C99.

Nonsense. ~0 is idiomatic C. Aren't you worried about the "risk" that
-1 may not be implemented as all-1s? That is, after all, an
implementation detail. It's not true for sign-magnitude or 1's
complement, for example.
You're wrong about everything.

~0 is negative zero in ones complement.
Implementations are allowed trap negative zero.


You're just repeating words, without any understanding. There's no
such thing as "negative zero" (or negative anything) in the context of
bitwise operations.


It is AFAIK implementation-defined whether the concept of "negative
zero" is meaningful in standard C (depending on the representation
of signed integers).
How can a bitwise operation trap? It can't.
Of course it can! (Why wouldn't it? And do modern digital computers
perform any operations that *aren't* bitwise, anyway?)
-1 is a value, not a bit pattern.
The value of negative one, cast to unsigned char, is UCHAR_MAX.


To clarify pete's point:

(uchar)-1 == (uchar)-((int)1) == (-(int)1)+UCHAR_M AX == UCHAR_MAX-1

....which is guaranteed to have an all-ones bit pattern, regardless
of padding or n's-complement.

Hello? The point is that -1 is ***being used as*** a bit pattern.
Then it's being used incorrectly. -1 is *not* a bit pattern, it's
an integer expression equal to the additive inverse of the integer 1.
It doesn't have a "bit pattern" per se.
The intent is to get "all 1s", which is true if the integer
representation is 2's complement; that's an implicit assumption
of the code.
If that *were* an implicit assumption of the code, then the code
would be broken. But it's not. Pete's code AFAICT doesn't assume
anything earth-shattering about the integer representations used
by the target system.
(This is the best reason not to use -1: the intent is not
perfectly clear).


However, it *does* produce the right answer, which is a point in
its favor. ~0 might trap, and in any case I think (unsigned char)-1
has a bit more aesthetic value to it (YMMV, of course).

And if you want everything to be *perfectly* clear, then you might
want to consider a different programming language. C is just
full of 'for (i=0; a[i]; ++i)'s and 'while (*s++ = *t++)'s; if
a simple -1 throws you, then you're in trouble. :-)

-Arthur
Nov 13 '05 #18
On Thu, 16 Oct 2003 23:53:05 -0700, Samuel Barber wrote:
pete <pf*****@mindsp ring.com> wrote in message news:<3F******* ****@mindspring .com>...
Samuel Barber wrote:
>
> ai***@acay.com. au (Peter Nilsson) wrote in message news:<63******* *************** ****@posting.go ogle.com>...
> > op*****@yahoo.c om (Samuel Barber) wrote in message news:<37******* *************** ****@posting.go ogle.com>...
> > > pete <pf*****@mindsp ring.com> wrote in message news:<3F******* ****@mindspring .com>...
> > > > 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.
> >
> > That risks a trap representation under C99.
>
> Nonsense. ~0 is idiomatic C. Aren't you worried about the "risk" that
> -1 may not be implemented as all-1s? That is, after all, an
> implementation detail. It's not true for sign-magnitude or 1's
> complement, for example.


You're wrong about everything.

~0 is negative zero in ones complement.
Implementations are allowed trap negative zero.


You're just repeating words, without any understanding. There's no
such thing as "negative zero" (or negative anything) in the context of
bitwise operations. How can a bitwise operation trap? It can't.


6.2.6.2 Integer types
2 ... (It is implementation-defined) whether the value with ...
sign bit 1 and all value bits 1 (for one's complement), is a trap
representation or a normal value. In the case of ... one's
complement, if this representation is a normal value it is called
a negative zero.
...
3 If the implementation supports negative zeros, they shall be
generated only by: the &, |, ^, ~, <<, and >> operators with
arguments that produce such a value;
...
4 If the implementation does not support negative zeros, the
behavior of the &, |, ^, ~, <<, and >> operators with arguments
that would produce such a value is undefined.
Nov 13 '05 #19
Arthur J. O'Dwyer wrote:
....
(uchar)-1 == (uchar)-((int)1) == (-(int)1)+UCHAR_M AX == UCHAR_MAX-1


You must have miscalculated somewhere. (uc)-1 should be UCHAR_MAX.

(uc)-1 == (uc)(0 - 1) == (uc)(UINT_MAX + 1 - 1) == (uc)(UINT_MAX) == UCHAR_MAX

Jirka

Nov 13 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
3034
by: Pascal | last post by:
Hello, I've a binary file with data in it. This file come from an old ms dos application (multilog ~ 1980). In this application, a field is declared as a 'decimal' (999 999 999.99). I put 0.00 in the field and save the record to the file. When I look in the binary file (with python or an hex editor), the field is stored on 8 bytes: 00-00-00-00-00-00-7F-00. I try unpack from struct module but the result isn't good.
7
3613
by: William Payne | last post by:
Hello, I have a variable of type unsigned long. It has a number of bits set (with set I mean they equal one). I need to determine those bits and their position and create new numbers from them. For example, consider this four-bit number: 1100 from this number I want to extract two numbers: 1000 and 100 had the four-bit number been 0101 I would want to extract 100 and 1. How should I do this? I wish I had some code to post but I don't...
45
3769
by: Curt Geske | last post by:
I'm suprised no one suggested a union! #include <stdio.h> union _x { long lng; char byt; } X; void main( void )
0
1639
by: Vjay77 | last post by:
I posted this question, but I pressed 'post' and it disappeared. So once again: Problem: I need to go to lets say www.site.com/page.html Imagine that this html code is 6 mb long. I need to extract information between bytes 5000 and 5020.
7
3152
by: fool | last post by:
Dear group, Extract the integer value present in a given string. So I tried the following: int main(void) { int val; char *data; data = malloc(sizeof *data); if(data)
3
17420
by: roopa.v1 | last post by:
Hi, How to assign long to character array and later extract it
2
6555
by: Vinciz | last post by:
hi guys... im new in java and i would love to learn some of these... basically i got a sample code to retrieve the blob from the mysql. however, i dont really know what to do with these retrieved byte/binary data as i got no idea on how to save them in our pc. For this situation, what i need to do is give the byte/binary data an extension (retrieved from another field in the table) in order to revert back to the original data i had in the...
7
7681
by: erikcw | last post by:
Hi all, I'm trying to extract zip file (containing an xml file) from an email so I can process it. But I'm running up against some brick walls. I've been googling and reading all afternoon, and can't seem to figure it out. Here is what I have so far. p = POP3("mail.server.com")
2
2317
by: parag_paul | last post by:
I was looking into the following page http://gcc.gnu.org/onlinedocs/gcc/Long-Long.html Here there is a term open-coded , why to ? And I saw the following definition for long long 5.8 Double-Word Integers ISO C99 supports data types for integers that are at least 64 bits wide, and as an extension GCC supports them in C89 mode and in C++. Simply write long long int for a signed integer, or unsigned long long
0
9980
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11236
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10830
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10925
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9641
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7172
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
4276
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3283
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.