home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!olivea!spool.mu.edu!yale.edu!ira.uka.de!slsvaat!josef!kanze
- From: kanze@us-es.sel.de (James Kanze)
- Newsgroups: comp.lang.c++
- Subject: Re: packing/unpacking unsigned in char ?
- Message-ID: <KANZE.92Nov18160657@slsvdnt.us-es.sel.de>
- Date: 19 Nov 92 00:06:57 GMT
- References: <1992Nov15.225151.7502@imada.ou.dk>
- Sender: news@us-es.sel.de
- Organization: SEL
- Lines: 55
- In-Reply-To: adelt@imada.ou.dk's message of Sun, 15 Nov 1992 22:51:51 GMT
-
- In article <1992Nov15.225151.7502@imada.ou.dk> adelt@imada.ou.dk (Adel
- Shavandi) writes:
-
- |> I need some suggestions for an effective and correct way to
- |> pack/unpack unsigneds in [0,127] in chars and communicate
- |> them to another process.
-
- |> What i do myself is as the following code:
-
-
- |> // child process:
- |> // ...
- |> unsigned num1;
- |> // ...
- |> // num1 is in [0,127] now
- |> printf("%c",num1); // later is rediercted to parent by pipe mech.
- |> // ...
-
-
- |> // parent process:
- |> // ...
- |> unsigned num2;
- |> char ch;
- |> int n=read (fd,&ch,1);
- |> if (n>0)
- |> num2=(unsigned) ch;
- |> // ...
-
-
- |> Now i know this may be wrong since sometimes i receive some wrong
- |> numbers. Any ideas/suggestions ?
-
- |> But what is interesting is that the wrong numbers are very similiar
- |> each time. Am i confronting with pitfalls ?!
-
- You don't say what OS you're using.
-
- On most non-Unix systems (MS-DOS, for example), there is a distinction
- between binary files and text files. In such cases, stdout (which
- printf uses) is normally opened as a *text* file. Independantly of
- this, I'm not sure what the implications would be of using printf on a
- binary file.
-
- As an example of the difference, in a text file, MS-DOS will convert
- the character '\n' (0x0a) to a two character sequence 0x0d,0x0a.
-
- So the correct solution is to open a file (don't use stdout) for
- binary output (fopen( <filename> , "wb" )), and output to it, with
- either fwrite or putc. (On some systems, it's possible to change the
- mode of stdout, but this is non-portable.)
- --
- James Kanze GABI Software, Sarl.
- email: kanze@us-es.sel.de 8 rue du Faisan
- 67000 Strasbourg
- France
-