LSB aligned data

Discussion in 'C++' started by wongjoekmeu@yahoo.com, Apr 18, 2008.

  1. Guest

    Hello all,
    I have a control software that retrieve data from an ethernet card.
    The function that is used to retrieve the data takes a reference to an
    unsigned char array whcih it fills for me. The data that I retrieve
    are actually 16-bits. So I want to convert them to unsigned short
    integers. For instance when I am expecting to retrieve ten 16 bits
    data, the function wants me to allocate an unsigned char array of 20
    elements to give to the retrieve function. I presume that for instance
    array element 0 and 1 represents my first 16 bits value. According to
    the manual the data is LSB aligned. I was wondering how I should
    calculate the ten array elements of my unsigned short integer array.
    Can anyone please help me out ?
    RR
     
    , Apr 18, 2008
    #1
    1. Advertising

  2. red floyd Guest

    wrote:
    > Hello all,
    > I have a control software that retrieve data from an ethernet card.
    > The function that is used to retrieve the data takes a reference to an
    > unsigned char array whcih it fills for me. The data that I retrieve
    > are actually 16-bits. So I want to convert them to unsigned short
    > integers. For instance when I am expecting to retrieve ten 16 bits
    > data, the function wants me to allocate an unsigned char array of 20
    > elements to give to the retrieve function. I presume that for instance
    > array element 0 and 1 represents my first 16 bits value. According to
    > the manual the data is LSB aligned. I was wondering how I should
    > calculate the ten array elements of my unsigned short integer array.
    > Can anyone please help me out ?
    > RR


    Instead of passing an array of 20 unsigned chars, pass an array of 10
    unsigned shorts and cast it to an unsigned char* when you call the function.

    This is common practice.
     
    red floyd, Apr 18, 2008
    #2
    1. Advertising

  3. SeanW Guest

    SeanW, Apr 18, 2008
    #3
  4. James Kanze Guest

    On 18 avr, 20:57, red floyd <> wrote:
    > wrote:


    > > I have a control software that retrieve data from an
    > > ethernet card. The function that is used to retrieve the
    > > data takes a reference to an unsigned char array whcih it
    > > fills for me. The data that I retrieve are actually 16-bits.
    > > So I want to convert them to unsigned short integers. For
    > > instance when I am expecting to retrieve ten 16 bits data,
    > > the function wants me to allocate an unsigned char array of
    > > 20 elements to give to the retrieve function. I presume that
    > > for instance array element 0 and 1 represents my first 16
    > > bits value. According to the manual the data is LSB aligned.
    > > I was wondering how I should calculate the ten array
    > > elements of my unsigned short integer array. Can anyone
    > > please help me out ?


    > Instead of passing an array of 20 unsigned chars, pass an
    > array of 10 unsigned shorts and cast it to an unsigned char*
    > when you call the function.


    > This is common practice.


    Maybe. Programs crashing is common practice, too. This
    solution doesn't work.

    The simplest solution would be to extract the values explicitly,
    one by one, using shifting and or'ing.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Apr 18, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DU
    Replies:
    8
    Views:
    628
    Toby A Inkster
    Oct 8, 2003
  2. Replies:
    9
    Views:
    982
    Juha Nieminen
    Aug 22, 2007
  3. Francis Moreau

    Portable way to mask the LSB

    Francis Moreau, May 29, 2010, in forum: C Programming
    Replies:
    7
    Views:
    748
    Keith Thompson
    May 31, 2010
  4. GinTon
    Replies:
    6
    Views:
    119
  5. daniel åkerud

    LSB and Ruby

    daniel åkerud, Feb 28, 2008, in forum: Ruby
    Replies:
    0
    Views:
    122
    daniel åkerud
    Feb 28, 2008
Loading...

Share This Page