Re: How to copy a integer length in a array.

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Mon, 20 Jul 2009 13:22:52 -0700, Ben Voigt [C++ MVP] <rbv@xxxxxxxxxxxxxxxxx> wrote:

[...]
The byte twiddling works, of course. But it's lots more verbose and less readable. The array-based version even has to create a small temporary object (the array...of course Arne's version doesn't; his code does essentially what HostToNetworkOrder() does, just without the method call), which depending on how it's done could wind up being less readable _and_ less efficient.

But he wanted the bytes in an array anyway.

Um, sort of. If you look at the code he posted, the only thing he uses the array for is transmitting it. I doubt that he really _needs_ the bytes in an array; he just did the usual "I've already decided how I _think_ this problem should be solved, so I'm stating the solution I think I need, rather than what problem I'm actually trying to solve".

In other words, while he wrote "I want to copy a integer length in a array", what he really needs to do is send a 32-bit integer -- the data length -- in big-endian format prior to sending the bytes that are actually being sent. There's no need at all for that 32-bit integer to be contained in an array in order to accomplish that.

So you'd need HostToNetworkOrder or Arne's version, followed by BitConverter.GetBytes()

Not to produce the same output as the code the OP posted, you wouldn't.

Pete
.



Relevant Pages

  • Re: Conditional as lvalue
    ... struct array a, b; ... What this pathological expression has done is lifted a pointer to ... The idea is that after the next sequence point, that temporary object ... and _temporary lifetime_.34) Its lifetime begins when the ...
    (comp.std.c)
  • Re: circular shift array
    ... there a way to do it so that you don't need a separate storage array? ... You don't need a separate array; one temporary object to hold the ... rightmost value is sufficient. ...
    (comp.lang.c)
  • Re: Image data via Winsock
    ... But there is a general misunderstanding in this thread: I don't think I will have so much problems with transmitting the bitmap data even though I will probably have to dig around for a while to figure that out. ... What I would like the client application to be able to do, is to simply return a "tabular" array of data if applicable or a stdpicture object if the data transmitted is an image. ...
    (comp.lang.basic.visual.misc)
  • Re: Image data via Winsock
    ... will have so much problems with transmitting the bitmap data even though I ... What I would like the client application to be able to do (it is a dll ... as byte array - but I don't know how to create that byte array, ... file as bitmap and make it available to the host application. ...
    (comp.lang.basic.visual.misc)
  • Re: Converting to big -endian
    ... But this is much slower than bit shuffling with HostToNetworkOrder or just ... Even if it is already in a byte array it is simpler code to just convert it ... r u the same Kevin Spencer from takempis.com? ... a little endian machine ...
    (microsoft.public.dotnet.framework)