🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #16060

Re: Writing a binary short out to a buffer

#16060
From: Robert J. Muller
Date: Wed, 31 Mar 1999 00:00
26 lines
1430 bytes
It turns out the easiest way to do this is to write the stuff directly out
into a file rather than going through a buffer. The RandomAccessFile class
has a very nice writeShort() method. So I just call that twice, then write
out the bytes of the message into the file.

  --Bob Muller

Robert J. Muller wrote:

> I'm converting an elderly C program to Java, and one function writes  a
> message into a buffer preceded by a header containing two short integers
> in the first four bytes of the file (pardon the globals):
>
> char gbuffer[8192];
>
> void write(char *message, short len, short b) {
>
> memcpy(gbuffer, (char*)&len, 2);
> memcpy(&gbuffer[2], (char*)&b, 2);
> memcpy(&gbuffer[4], message, len+1);
> }
>
> Can anyone tell me how to get the first four bytes of a byte[] to look
> like this given two shorts?
>
>   --Bob Muller
Technical Details
Message-ID: <37025BA6.20EC8267@valuestar.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <36F98F28.98E2C5B2@valuestar.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,d7e6823912d8922a
X-Google-Attributes: gidf74b3,public
From: "Robert J. Muller" <bmuller@valuestar.com>
Subject: Re: Writing a binary short out to a buffer
Date: 1999/03/31
Message-ID: <37025BA6.20EC8267@valuestar.com>
X-Deja-AN: 461231634
Content-Transfer-Encoding: 7bit
References: <36F98F28.98E2C5B2@valuestar.com>
Content-Type: text/plain; charset=us-ascii
Organization: ValueStar, Inc.
Mime-Version: 1.0
Reply-To: muller@computer.org
Newsgroups: borland.public.jbuilder.java.language