Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > convert a stream of bits to a primitive...

Reply
Thread Tools

convert a stream of bits to a primitive...

 
 
Sam Smith
Guest
Posts: n/a
 
      09-24-2004
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


 
Reply With Quote
 
 
 
 
Max Vasin
Guest
Posts: n/a
 
      09-24-2004
"Sam Smith" <(E-Mail Removed)> 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: http://www.velocityreviews.com/forums/(E-Mail Removed)
ICQ: 276438891

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert DVD with subtitle stream to DivX with same subtitle stream(selectable) malise Software 1 04-17-2007 09:15 AM
shifting bits, shift 32 bits on 32 bit int GGG C++ 10 07-06-2006 06:09 AM
8 bits/ch vs 16 bits/ch in PS Terry Digital Photography 5 01-21-2004 06:59 PM
8-Bits vs 12 or 16 bits/pixel; When does more than 8 bits count ? Al Dykes Digital Photography 3 12-29-2003 07:08 PM
win XP 32 bits on a 64 bits processor.. Abbyss Computer Support 3 11-13-2003 12:39 AM



Advertisments