convert a stream of bits to a primitive...

S

Sam Smith

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
 
M

Max Vasin

Sam Smith said:
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 &, |, ^,
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 said:
3) How are float and double types taken care of?
What do mean?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top