472,358 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,358 software developers and data experts.

convert a stream of bits to a primitive...

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
1 2414
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: hicham | last post by:
Hi, I am looking for help, i would like to know how can i use the endian.h and config.h to convert compiled files under solaris from BIG-ENDIAN to compiled files LITTLE-ENDIAN. I am working...
2
by: Brad | last post by:
I have tried both the binary and the XML serialization; both give the same results. I have an object with a MemoryStream member. When I serialize and deserialize the object, the MemoryStream...
15
by: Adam H. Peterson | last post by:
I would like to make a stream or streambuf that tracks the number of lines that have been read and stuff like that (so, for example, when I get an error message, I can ask the stream for the line...
14
by: vib | last post by:
Hi there, union UC32 { unsigned int L; unsigned short S; unsigned char C; } UC32; with the above structure, I am able to shift bits of C, into C, and C into C so on and so forth till C as...
7
by: whatluo | last post by:
Hi, all I'm now working on a program which will convert dec number to hex and oct and bin respectively, I've checked the clc but with no luck, so can anybody give me a hit how to make this done...
6
by: Gator | last post by:
Hi All, Basically my situation is this, I have a server implemented in C++, unmanaged code, using proprietery protocol over TCP/IP to communicate with the cilent(c++ also). Now, I am implementing...
3
by: MJB | last post by:
I'm getting an IStream back from function xmlHttp.responsestream. I would like to convert this to a System.IO.Stream in order to work with it in my application. Has anyone encountered this and...
5
by: John Dumais | last post by:
Hello, I have been trying to figure out how to write an array of doubles (in this specific case) to a binary stream without using a loop. What I have been doing is... foreach(double d in...
8
by: te509 | last post by:
Hi guys, does anybody know how to convert a long sequence of bits to a bit-string? I want to avoid this: '949456129574336313917039111707606368434510426593532217946399871489' I would...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
0
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.