By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,909 Members | 2,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,909 IT Pros & Developers. It's quick & easy.

convert a stream of bits to a primitive...

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Sam Smith" <an*******@foo.bar.com> 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: ma******@jabber.ru
ICQ: 276438891

Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.