473,883 Members | 2,619 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 12806
On Fri, 17 Oct 2003 20:06:45 +0200, cody wrote:
"Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> schrieb im Newsbeitrag
news:Pi******** *************** ***********@uni x48.andrew.cmu. edu...
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).


Why should ~0 trap??? it results in the 1's complement of 0 which means all
bits are 1's. Padding bits are not affected by this operation, however the
values of padding bits should never be of your interest.

A Trap representation can *only* be generated when manipulating the value
using pointers which aren't the type of the value or doesn't start at the
exact address of the value. Wrong usage of an union can also result in a
Trap representation.

But all arithmetic or bitwise operations cannot result in a
trap-representation.


Go back and read the whole thread

Nov 13 '05 #31
"pete" <pf*****@mindsp ring.com> wrote in message
news:3F******** ***@mindspring. com...
Peter Nilsson wrote:

....
UB if sizeof(long) == 1.


No.
If sizeof(long) == 1,
then the loop doesn't execute.


My bad.

--
Peter
Nov 13 '05 #32
CBFalconer wrote:

pete wrote:
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.


You were not extracting bytes. You were extracting 8 bit
quantities, least significant part first. In other words you are
expressing the _value_ in base 256, so why not say so in the code?


I don't know what you're talking about.
Which code do you think is mine?

--
pete
Nov 13 '05 #33
Chris Torek <no****@elf.eng .bsdi.com> wrote in message news:<bm******* ***@elf.eng.bsd i.com>...
The section quoted above (along with others) define how the abtract
machine is to work. In the abstract machine, writing:

~0

means:

- make an int with the value 0
- now, flip all the bits

This process *can* give rise to a "trap representation" on a ones'
complement machine.


Thanks, I get it now. The correct usage is therefore ~0u or ~0U.

Sam
Nov 13 '05 #34
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>...
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.


I was wrong about everything. ~0 is "wrong" (in terms of the abstract
C machine); the correct expression is ~0u or ~0U.

Sam
Nov 13 '05 #35
"cody" <do************ *********@gmx.d e> writes:
"Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> schrieb im Newsbeitrag
news:Pi******** *************** ***********@uni x48.andrew.cmu. edu...
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).


Why should ~0 trap??? it results in the 1's complement of 0 which means all
bits are 1's. Padding bits are not affected by this operation, however the
values of padding bits should never be of your interest.

A Trap representation can *only* be generated when manipulating the value
using pointers which aren't the type of the value or doesn't start at the
exact address of the value. Wrong usage of an union can also result in a
Trap representation.

But all arithmetic or bitwise operations cannot result in a
trap-representation.


This is not true. You should read the relevant portions of the
standard before making such assertions.

-Micah
Nov 13 '05 #36
"Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> writes:
On Fri, 17 Oct 2003, cody wrote:

"Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> schrieb...
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).


Why should ~0 trap??? it results in the 1's complement of 0 which
means all bits are 1's


...which may be a trap representation on a ones'-complement
architecture.
Padding bits are not affected by this operation, however the
values of padding bits should never be of your interest.


Well, technically padding bits *might* be affected by the ~
operation, but the effect on the padding bits alone cannot create
a trap representation -- the system has to remember to do the
Right Thing with them in this case.


I don't believe this is true. Can you back this up with a quote
from the standard? I cannot remember any instance where the
standard says this can't be: and I can think of a specific spot
where the standard says (non-normatively) that this *can* be (see
footnote 45 to 6.2.6.2#5). The lack of exclusion means it
certainly is possible.

-Micah
Nov 13 '05 #37

On Sat, 18 Oct 2003, Micah Cowan wrote:

"Arthur J. O'Dwyer" writes:
On Fri, 17 Oct 2003, cody wrote:

Why should ~0 trap??? it results in the 1's complement of 0 which
means all bits are 1's
...which may be a trap representation on a ones'-complement
architecture.
Padding bits are not affected by this operation, however the
values of padding bits should never be of your interest.


Well, technically padding bits *might* be affected by the ~
operation, but the effect on the padding bits alone cannot create
a trap representation -- the system has to remember to do the
Right Thing with them in this case.


I don't believe this is true. Can you back this up with a quote
from the standard?


From N869, section 6.2.6.2, footnote 39:

Some combinations of padding bits might generate trap
representations , for example, if one padding bit is a
parity bit. Regardless, no arithmetic operation on valid
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^
values can generate a trap representation other than as
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^
part of an exception such as an overflow, and this cannot
occur with unsigned types. All other combinations of
padding bits are alternative object representations of
the value specified by the value bits.

(The index to N869 clearly puts "bitwise operators" under "arithmetic
operators," even if the text doesn't explicitly say so.)

This is obviously the Right Thing for the standard to say, too,
since if padding bits' values *could* create trap representations
out of thin air, how could programmers on those platforms ever
compute anything?
I cannot remember any instance where the
standard says this can't be: and I can think of a specific spot
where the standard says (non-normatively) that this *can* be (see
footnote 45 to 6.2.6.2#5).


N869 doesn't have a 6.2.6.2#5. Could you post that paragraph and
footnote, please?

-Arthur

Nov 13 '05 #38
"Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> writes:
On Sat, 18 Oct 2003, Micah Cowan wrote:

"Arthur J. O'Dwyer" writes:
On Fri, 17 Oct 2003, cody wrote:
>
> Why should ~0 trap??? it results in the 1's complement of 0 which
> means all bits are 1's

...which may be a trap representation on a ones'-complement
architecture.

> Padding bits are not affected by this operation, however the
> values of padding bits should never be of your interest.

Well, technically padding bits *might* be affected by the ~
operation, but the effect on the padding bits alone cannot create
a trap representation -- the system has to remember to do the
Right Thing with them in this case.
I don't believe this is true. Can you back this up with a quote
from the standard?


From N869, section 6.2.6.2, footnote 39:

Some combinations of padding bits might generate trap
representations , for example, if one padding bit is a
parity bit. Regardless, no arithmetic operation on valid
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^
values can generate a trap representation other than as
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^
part of an exception such as an overflow, and this cannot
occur with unsigned types. All other combinations of
padding bits are alternative object representations of
the value specified by the value bits.

(The index to N869 clearly puts "bitwise operators" under "arithmetic
operators," even if the text doesn't explicitly say so.)


The above quote is footnote 45 (*and* 44: typo!) of
6.2.6.2 in the final version.

The same grouping appears in the index of the final version; but
the index isn't normative (nor are the footnotes). However, the
footnote you quote above still seems to be borne out by normative
text. But I don't think the quote quite supports your assertion
that "the padding bits alone cannot create a trap
representation" : The fact that "proper" arithmetic operations
cannot affect the padding bits in such a way as to create a trap
representation doesn't imply that affecting the padding bits can
not be the sole cause of trap representations ; it means that
implementations can't cause padding bits to somehow find
themselves in that representation in the coruse of well-defined
arithmetic operations. But this is obvious, because the only way
to affect padding bits in the first place is to use arithmetic
operations in such a way as to cause exceptional conditions (such
as overflows). I see no reason why using ~ on a signed integer
type can't cause such an exceptional condition: Obviously,
overflow itself can easily occur, and the very act of flipping a
padding bit could be considered an "exceptiona l condition",
AFAICT.
This is obviously the Right Thing for the standard to say, too,
since if padding bits' values *could* create trap representations
out of thin air, how could programmers on those platforms ever
compute anything?


This is not a valid conclusion. Assuming padding bits' values
*can* create trap representations out of thin air, you can only
conclude that an implementation can't arrange padding bits to
such a representation when arithmetic operators are being
*properly* used. Can you think of a well-defined way to directly
affect padding bits? But some ways that don't involve arithmetic
operators (directly) would be to read an uninitialized value
(which could already *be* a trap representation) , or you could
do:

int foo;
unsigned char *bar = (unsigned char *)&foo; /* Well-defined */

bar[0] = bar[0] ^ 01000; /* Well-defined, but might set a
padding bit of foo. */
foo; /* Not well-defined: might be a trap representation. */
I cannot remember any instance where the
standard says this can't be: and I can think of a specific spot
where the standard says (non-normatively) that this *can* be (see
footnote 45 to 6.2.6.2#5).


N869 doesn't have a 6.2.6.2#5. Could you post that paragraph and
footnote, please?


The paragraph doesn't tell you anything you don't know (it's got
to be in N869), and the footnote is the very same one you've
quoted from N869. But here it is, anyway (6.2.6.2#5:)

The values of any padding bits are unspecified.45) A valid
(non-trap) object representation of a signed integer type where
the sign bit is zero is a valid object representation of the
corresponding unsigned type, and shall represent the same
value.

-Micah
Nov 13 '05 #39
Micah Cowan <mi***@cowan.na me> wrote:
"Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> writes:

<snip>
N869 doesn't have a 6.2.6.2#5. Could you post that paragraph and
footnote, please?


The paragraph doesn't tell you anything you don't know (it's got
to be in N869)

<snip>

Chapter 6.2.6.2:
C99 final N869
#1 --> #1
#2 --> #2
#3 --> --
#4 --> --
#5 --> #3
#6 --> #4

Regards
--
Irrwahn
(ir*******@free net.de)
Nov 13 '05 #40

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

Similar topics

17
3033
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
3612
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
3765
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
1636
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
17418
by: roopa.v1 | last post by:
Hi, How to assign long to character array and later extract it
2
6552
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
7679
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
9786
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,...
1
10836
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
10407
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9568
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...
1
7962
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7114
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();...
0
5794
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5982
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3230
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.