473,666 Members | 2,080 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Binary files, little&big endian setting bits

Hi, i know this is an old question (sorry)

but its a different problem, i need to write a binary file as follows

00000011
00000000
00000000
00000101
00000000
11111111
11111111
00000000

program will be compiled in Microsoft Visual C++

was thinking of just writing it as chars (afaik chars are the only
unsigned int thats only 1 byte) so basicly i'll be writing
3,0,0,5,0,256,2 56,0

question is if i write a file like that will it come out as the bits
above, does VC++ write little or big endian and other than endian
issues if it doesn't come out as above, why not??
Nov 14 '05 #1
11 2565

"Steve" <st********@nui galway.ie> wrote in message
news:28******** *************** ***@posting.goo gle.com...
Hi, i know this is an old question (sorry)
Off topic, too...

question is if i write a file like that will it come out as the bits
above, does VC++ write little or big endian and other than endian
issues if it doesn't come out as above, why not??


The Intel 80x86 processors are Little Endian, regardless of your compiler,
16 bit and 32-bit words will not be "in order" but will have their order
reversed. Writing bytes, however, you should have no endiannes problems.

See
http://www.cs.umass.edu/~verts/cs32/endian.html
http://www.rdrop.com/~cary/html/endian_faq.html
for details.
Nov 14 '05 #2
Steve <st********@nui galway.ie> wrote:
but its a different problem, i need to write a binary file as follows 00000011
00000000
00000000
00000101
00000000
11111111
11111111
00000000 program will be compiled in Microsoft Visual C++
Should be irrelevant here, if you need something VC++ specific you
better should ask in some MS related newsgroup.
was thinking of just writing it as chars (afaik chars are the only
unsigned int thats only 1 byte)
Sorry, but a char isn't nessecarily a single byte (i.e. 8 bits) - a
char can have different numbers of bits on different architectures.
See the macro CHAR_BIT in <limits.h>, that tells you how many bits a
char has.
so basicly i'll be writing
3,0,0,5,0,256,2 56,0 question is if i write a file like that will it come out as the bits
above, does VC++ write little or big endian and other than endian
issues if it doesn't come out as above, why not??


When you write single bytes endianness isn't an issue at all - it
only becomes a problem when you write out data with a size larger
than a byte.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@p hysik.fu-berlin.de
\______________ ____________ http://www.toerring.de
Nov 14 '05 #3
Steve wrote:
Hi, i know this is an old question (sorry)

but its a different problem, i need to write a binary file as follows

00000011
00000000
00000000
00000101
00000000
11111111
11111111
00000000

program will be compiled in Microsoft Visual C++

was thinking of just writing it as chars (afaik chars are the only
unsigned int thats only 1 byte) so basicly i'll be writing
3,0,0,5,0,256,2 56,0

question is if i write a file like that will it come out as the bits
above, does VC++ write little or big endian and other than endian
issues if it doesn't come out as above, why not??


I think you're asking about the order in which the
individual bits of each byte will be written: will the
first bit of the 3 be the high-order zero or the low-
order one?

To begin with, there may not *be* any order at all.
For example, suppose the output is sent to a parallel
interface that presents all eight bits simultaneously:
which bit is "first among equals" when they all march
in line abreast? The individual bits may not even
exist as discrete units: Consider writing to a modem
that encodes many bits in each signal transition, or
which uses data compression and winds up transmitting
2.71828 bits to encode the eight you presented? At the
C language level -- and even at the machine language
level, for most machines -- the byte is an indivisible
unit of I/O, and since it's indivisible the "order" of
its components cannot be discerned.

The question does eventually arise, at the level of
the medium on which the data is stored or through which
it is transmitted. And here, each storage device or
transmission medium has its own standards for the encoding
of these "indivisibl e" bytes. Some, like serial interfaces,
will indeed "split the atom" and transmit the individual
bits in a specified other. Others, like SCSI controllers,
designate specific signal lines for specific bits. Still
others, like card punches (anybody remember punched cards?)
will produce a pattern of holes that encode the character
designated by 3; this pattern will probably not have any
obvious relation to the original eight bits.

But you needn't worry about this unless you're the
person charged with implementing the electrical interface
to the storage or transmission medium. It is the job of
that interface to accept the serialized bits or the SCSI
signals or the holes in a punched card and to reconstitute
the indivisible byte value from them. As a programmer you
almost never care about the details (unless, perhaps, you're
writing diagnostic code that wants to produce specified
patterns in the signal lines to detect cross-talk, or that
sort of thing). You write out a 3, and it's the business
of the various media through which that 3 travels to ensure
that a 3 comes out at the other end. No huhu, cobber.

Where you *do* need to worry about endianness issues
is when you're dealing with multi-byte data objects: the
low-level media take care of individual bytes for you, but
you're responsible for arranging those bytes into larger
structures. Different systems have different conventions
for such arrangements, and that's why you can't just use
`fwrite(&int_va lue, sizeof int_value, 1, stream)' to send
an integer from one system to another. But once you've
settled on an "exchange format" that specifies the order
and meaning of the individual bytes, all you need to do is
decompose your larger objects into those bytes before
writing them, and reassemble the bytes into the larger
objects when reading. The actual form of the bytes "in
flight" is not your problem.

The only possible worry you might have with byte-by-
byte data exchange is if the machines use different byte
sizes: Exchanging data between machines with 8-bit and
9-bit bytes, for instance, can be tricky. But if you're
dealing with a common byte size, all is well.

--
Er*********@sun .com

Nov 14 '05 #4
In article <news:28******* *************** ****@posting.go ogle.com>
... i need to write a binary file as follows
00000011
00000000
00000000
00000101
00000000
11111111
11111111
00000000
... (afaik chars are the only unsigned int thats only 1 byte) so
basicly i'll be writing 3,0,0,5,0,256,2 56,0


Actually, eight 1 bits, treated as an unsigned char, represents the
value 255, not 256.

Eric Sosman has already addressed the (lack of) endianness that
occurs when 8-bit units are your atomic level of input/output.

I want to point out that in C, "byte" and "char" mean the same
thing, which is not necessarily "8 bits" -- but it probably does
not matter, in part because you are unlikely to have a 9- or 32-bit
"char" system in the first place, and in part because those have
to deal with the rest of the world.

And then I just had to write this... :-)

Bits in the C

When using a protocol over a net
(like TCP/IP or one I forget)
Where the number of bits has got to be eight
The Standards for C won't keep the things straight:
A char that is un-signed has got enough bits
But it might have too many, giving you fits!

A byte is a char, and a char is a byte
Eight bits is common, but nine is in sight
Digital Signalling Processors? Whew!
Here you may find there's a whole thirty-two!

When external formats on you are imposed
The trick to remember (while staying composed):
The C system's "bytes" may well be too big
But this does not mean you must give up the jig
To talk to another, the box you are on
Must have SOME way for them to begone
("Them" being pesky extraneous bits)
It just is not Standard, the part that omits
Some high order zeros of values between
Oh oh and eff eff (and hex sure is keen!).

To hold the right values, a char that is un-signed
Will do the trick nicely, I think you will find.
Who cares if it's bigger than strictly required?
The values you need will never get mired.
The eight bits you want won't get overtired
And values you need will never get mired!

Perhaps, with some more work and a good rousing tune, this might
even make a Gilbert & Sullivan pastiche. :-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #5
On Thu, 28 Oct 2004 11:56:16 -0400, Eric Sosman <er*********@su n.com>
wrote:
<snip>
[Endianness] does eventually arise, at the level of
the medium on which the data is stored or through which
it is transmitted. <snip> Some, like serial interfaces,
will indeed "split the atom" and transmit the individual
bits in a specified other. Others, like SCSI controllers,
designate specific signal lines for specific bits. Still
others, like card punches (anybody remember punched cards?)
will produce a pattern of holes that encode the character
designated by 3; this pattern will probably not have any
obvious relation to the original eight bits.

If the bits were EBCDIC, it certainly does bear a relation obvious to
anyone who thinks a bit about it (and knows the BCDIC history); even
for ASCII significant chunks of the translation to and from EBCDIC
(and thus card aka Hollerith) are systematic.

(Otherwise concur.)

Now, if you want an octet-parallel interface people will probably have
trouble remembering, how about IEEE-488 (IIRC) GPIB nee HPIB? <G>
- David.Thompson1 at worldnet.att.ne t
Nov 14 '05 #6
On Mon, 01 Nov 2004 08:14:12 GMT
Dave Thompson <da************ *@worldnet.att. net> wrote:

<snip>
Now, if you want an octet-parallel interface people will probably have
trouble remembering, how about IEEE-488 (IIRC) GPIB nee HPIB? <G>


I think it was originally HPIB (Hewlet Packard Interface Bus), then GPIB
and IEEE-488 came alone as later names for it.

I've made plenty of use of it in the past talking to DSO, DMM...

I also did some low level hacking around with it trying to detect if kit
was connected without crashing the program doing the check or locking up
the bus. All in HP Pascal.

So I have absolutely no trouble remembering it and know where there is
kit still making use of it. :-)
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #7
<Je***********@ physik.fu-berlin.de> wrote in message
news:2u******** *****@uni-berlin.de...
Steve <st********@nui galway.ie> wrote:
but its a different problem, i need to write a binary file as follows
00000011
00000000
00000000
00000101
00000000
11111111
11111111
00000000

program will be compiled in Microsoft Visual C++


Should be irrelevant here, if you need something VC++ specific you
better should ask in some MS related newsgroup.
was thinking of just writing it as chars (afaik chars are the only
unsigned int thats only 1 byte)


Steve, note that type 'char' might or might not be unsigned.
This is defined by the implementation. If you want to ensure
an unsigned type, explicitly say so:

unsigned char c;
Sorry, but a char isn't nessecarily a single byte
Actually, yes it is.

See ISO 9899:

3.6

3.7.1

5.2.1/3

(i.e. 8 bits) - a
char can have different numbers of bits on different architectures.


As can a byte. "byte equals eight bits" is a very common
misconception.

-Mike
Nov 14 '05 #8

"Chris Torek" <no****@torek.n et> wrote in message
news:cl******** *@news3.newsguy .com...
In article <news:28******* *************** ****@posting.go ogle.com> And then I just had to write this... :-)

Bits in the C


I love it! Printed and pasted on my office wall.

Thanks.

-Mike
Nov 14 '05 #9
"Mike Wahler" <mk******@mkwah ler.net> wrote:
(i.e. 8 bits) - a
char can have different numbers of bits on different architectures.


As can a byte. "byte equals eight bits" is a very common
misconception.


OOI, how many bits are there in a kilobyte, if 1 byte is 32 bits?
Should I start referring to file sizes in bits to avoid confusion?
Nov 14 '05 #10

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

Similar topics

2
8528
by: Muumac | last post by:
I have problem with large textfiles! When I load over 4MB xml and then try to preg_match something in this I get always FALSE! I have <File>....</File> tags in XML. Between tags is files contents BASE64 encoded! When xml contains only one big over 4MB file in it, preg_match and preg_match_all doesnt find it! When included file size is nelow 4MB, preg functions works perfectly! But PHP manual says that there is no size limitations to...
4
2992
by: Geoff Caplan | last post by:
Hi folks, I am looking for a practical way of sending encrypted strings back and forth between a Python HTTP client on Windoze and an Apache/PHP server on Linux. I am looking for a simple, lightweight symmetrical solution using, say, blowfish: SSL would be a last resort as I suspect it will cause fairly major installation issues on the client. Encryption in PHP uses a wrapper around the mcrypt C library.
4
3769
by: Khalique | last post by:
I have built a web service whose purpose is to copy files from a secure place to client machine and vice versa. The problem I am having is perhaps related to permissions and access rights. For testing purposes, the secure place is setup on the client machine. The client (window app) calls the web service (on a different machine) and connects successfully to the web service. However, when client calls a method that copies the file from...
5
575
by: pt | last post by:
Hi, i am wonderng what is faster according to accessing speed to read these data structure from the disk in c/c++ including alignment handling if we access it on little endian system 32 bits system + OS e.g. Windows, Linux, WinCE. I am not quite sure about the alignment of the memory.... soln. 1: should be faster, I am not sure. idx size (bytes) 1 4
46
2156
by: Sensei | last post by:
I was having an interesting discussion about the ANSI C and some ``weird inconsistencies'', or at least what at first sight can be seen as an imbalance. I hope someone can satisfy my curiosity. The standard provides means to open files associating a /path/ with a /stream/. The standard does not provide any means to handle /directories/. There are three streams defined by the standard, stdin stdout stderr. Am I right? Now, what is the...
4
4303
by: UnknownBlue | last post by:
Hi. How do I display a binary data that is formatted in little endian in Visual C++ 6? Can I just display it as per normal or is there an additional code to use to read and display it?
1
7082
by: John Bowman | last post by:
Hi All, I originally sent this to the dotnet.security ng, but no repsonse in 5 days. Hopefully, someone here can explain to me what's going on. I'm fairly new to .NET app security and manifests. I'm working with a test app: "MyApp.exe". Couple Q's: 1) When I turn on the security project setting in VS2005 for "Enable
71
4187
by: iesvs | last post by:
Hello guys, every time a rode a doc or a book about the language C I saw that operators << and >exist. But each time they said that << translate the digit to the left (and >...) but no one said if a << 1 mean every time a * 2, because there is the problem with the way the integer are stored. So I never use thoses operators because I fear strange behaviour on different architecture. Can I use them and be sure that a << 1 means a * 2 on...
5
2854
by: joso | last post by:
public static bool isodd(int i) { return ((i & 1) == 1); } can someone explain me how this is working
0
8445
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
8356
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
8551
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,...
1
6198
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
5664
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
4198
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
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
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.