473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 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 2492
"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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.