convert a stream of bits to a primitive...

Discussion in 'C++' started by Sam Smith, Sep 24, 2004.

  1. Sam Smith

    Sam Smith Guest

    Hi,

    I wan't a function to take a const char*, a start bit position and number of
    bits and convert that bit-stream into a primitive of desired type.

    I.e. something like:

    char convert(const unsigned char* buffer, size_t start_pos, size_t length)
    {
    char value = 0;

    // Do a bit-fiddling conversion. E.g. if buffer is two bytes,
    // where buffer[0]=103 and buffer[1]=57, <=> 0110011100111001,
    // and if start_pos is 4 with length 6 <=> 011100,
    // returned decimal value should be 28.

    return value;
    }

    One problem is that this function works for char only. I wan't version for
    the other primitives as well. Because of this I think that this function
    ought to be a function template where the primitive type is specified.
    Problem now is that a function cannot be overloaded on return value. Hmm...

    Questions:
    0) How do I safely convert a char buffer into a unsigned char* (The buffer
    origins from an external C-API function which just returns a char*)
    1) How is the bit-fiddling implemented? Naive, but straight-forward and/or
    optimal implementations are of interest.
    2) How is the the overload problem solved, which btw may not be an overload
    problem since the problem could be reformulated. Anyway, how is a generic
    solution obtained?
    3) How are float and double types taken care of?

    Thanks in advance!
    Sam
    Sam Smith, Sep 24, 2004
    #1
    1. Advertising

  2. Sam Smith

    Max Vasin Guest

    "Sam Smith" <> writes:

    > Hi,
    >
    > I wan't a function to take a const char*, a start bit position and number of
    > bits and convert that bit-stream into a primitive of desired type.
    >
    > I.e. something like:
    >
    > char convert(const unsigned char* buffer, size_t start_pos, size_t length)
    > {
    > char value = 0;
    >
    > // Do a bit-fiddling conversion. E.g. if buffer is two bytes,
    > // where buffer[0]=103 and buffer[1]=57, <=> 0110011100111001,
    > // and if start_pos is 4 with length 6 <=> 011100,
    > // returned decimal value should be 28.
    >
    > return value;
    > }
    >
    > One problem is that this function works for char only. I wan't version for
    > the other primitives as well. Because of this I think that this function
    > ought to be a function template where the primitive type is specified.
    > Problem now is that a function cannot be overloaded on return value. Hmm...
    > Questions:
    > 0) How do I safely convert a char buffer into a unsigned char* (The buffer
    > origins from an external C-API function which just returns a char*)

    Well, I think static_cast will be safe for a char buffer.

    > 1) How is the bit-fiddling implemented? Naive, but straight-forward and/or
    > optimal implementations are of interest.

    Well, if I understand you correctly: using bitwise operators &, |, ^,
    ~, <<, >> - they one by one correspond to machine instructions, which
    are quite efficient.

    > 2) How is the the overload problem solved, which btw may not be an overload
    > problem since the problem could be reformulated. Anyway, how is a generic
    > solution obtained?

    template<typename T>
    T convert (...) { ... }

    int x = convert<int>(...);

    > 3) How are float and double types taken care of?

    What do mean?

    --
    WBR, Max Vasin.
    JID:
    ICQ: 276438891
    Max Vasin, Sep 24, 2004
    #2
    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. GGG
    Replies:
    10
    Views:
    12,550
    Donar
    Jul 6, 2006
  2. Replies:
    7
    Views:
    600
    Victor Bazarov
    May 9, 2005
  3. sarmin kho
    Replies:
    2
    Views:
    825
    A. Lloyd Flanagan
    Jun 15, 2004
  4. Miki Tebeka
    Replies:
    1
    Views:
    443
    Marcin 'Qrczak' Kowalczyk
    Jun 14, 2004
  5. Daniel Pitts
    Replies:
    7
    Views:
    472
Loading...

Share This Page