473,698 Members | 2,571 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to avoid reinterpret_cas t in this snippet?

KK
Hello all,
I have a unsigned char buffer 'buffer[1024]' and I need to convert the
first 12 bytes of it into a string. Below is a code that should work,
however, how can I avoid reinterpret_cas t operator?
Or Is there a simple way to get around this?
Thanks.
-KK
/* not tested yet */
typedef unsigned char BYTE
std::string GetStringFromBy teBuffer(const BYTE* const buffer, int pos )
{
const char *chAry = reinterpret_cas t <const char *> (buffer + pos);
std::string tmp(chAry,12);
return chAry;
}

Dec 1 '05
32 3111
On Fri, 02 Dec 2005 05:31:13 -0500, Kai-Uwe Bux <jk********@gmx .net>
wrote:
Hm, std::string has a templated constructor:
template<class InputIterator>
basic_string(In putIterator begin, InputIterator end,
const Allocator& a = Allocator());
Since unsigned char is convertible to char, this constructor should match.


// test_uchar.cpp

#include <iostream>
#include <ostream>
#include <string>

int main()
{
const unsigned char msg[] = "Hello";
std::string s(msg);
std::cout << s << std::endl;
}

Does that compile on your system?

--
Bob Hairgrove
No**********@Ho me.com
Dec 2 '05 #11
On Fri, 02 Dec 2005 11:04:26 GMT, al***@start.no (Alf P. Steinbach)
wrote:
The reason? Because std::string has no constructor that takes unsigned
char* as an argument.


That is incorrect.


// test_uchar.cpp

#include <iostream>
#include <ostream>
#include <string>

int main()
{
const unsigned char msg[] = "Hello";
std::string s(msg);
std::cout << msg << std::endl;
}

Does that compile on your system?

--
Bob Hairgrove
No**********@Ho me.com
Dec 2 '05 #12

Bob Hairgrove wrote:
On Fri, 02 Dec 2005 11:04:26 GMT, al***@start.no (Alf P. Steinbach)
wrote:
The reason? Because std::string has no constructor that takes unsigned
char* as an argument.


That is incorrect.


// test_uchar.cpp

#include <iostream>
#include <ostream>
#include <string>

int main()
{
const unsigned char msg[] = "Hello";
std::string s(msg);
std::cout << msg << std::endl;
}

Does that compile on your system?


Nope. However, this does:

#include <string>
const unsigned char msg[] = "Hello";
std::string s(msg,msg+sizeo f(msg));

The statement you're tyring to prove is: "
std::string has no constructor that takes one argument, of type
unsigned char*.
" That statement is of course true, but not really relevant.

HTH,
Michiel Salters

Dec 2 '05 #13
On 2005-12-02, Bob Hairgrove <in*****@bigfoo t.com> wrote:
On Fri, 02 Dec 2005 11:04:26 GMT, al***@start.no (Alf P. Steinbach)
wrote:
The reason? Because std::string has no constructor that takes unsigned
char* as an argument.


That is incorrect.


// test_uchar.cpp

#include <iostream>
#include <ostream>
#include <string>

int main()
{
const unsigned char msg[] = "Hello";
std::string s(msg);
std::cout << msg << std::endl;
}

Does that compile on your system?

--
Bob Hairgrove
No**********@Ho me.com

--
Neil Cerutti
Dec 2 '05 #14
Bob Hairgrove wrote:
On Fri, 02 Dec 2005 05:31:13 -0500, Kai-Uwe Bux <jk********@gmx .net>
wrote:
Hm, std::string has a templated constructor:
template<class InputIterator>
basic_string(In putIterator begin, InputIterator end,
const Allocator& a = Allocator());
Since unsigned char is convertible to char, this constructor should match.


// test_uchar.cpp

#include <iostream>
#include <ostream>
#include <string>

int main()
{
const unsigned char msg[] = "Hello";
std::string s(msg);
std::cout << s << std::endl;
}

Does that compile on your system?


Nope, but that is not the constructor I was talking about. The following
*does* compile:

// test_uchar.cpp

#include <iostream>
#include <ostream>
#include <string>

int main()
{
const unsigned char msg[] = "Hello";
std::string s( msg, msg+5 );
std::cout << s << std::endl;
}
Best

Kai-Uwe Bux
Dec 2 '05 #15
On 2005-12-02, Neil Cerutti <le*******@emai l.com> wrote:
On 2005-12-02, Bob Hairgrove <in*****@bigfoo t.com> wrote:
On Fri, 02 Dec 2005 11:04:26 GMT, al***@start.no (Alf P.
Steinbach) wrote:
The reason? Because std::string has no constructor that
takes unsigned char* as an argument.

That is incorrect.


// test_uchar.cpp

#include <iostream>
#include <ostream>
#include <string>

int main()
{
const unsigned char msg[] = "Hello";
std::string s(msg);
std::cout << msg << std::endl;
}

Does that compile on your system?


Oops. Sorry for the all-quote null-response post. The slrn
configuration setting that allowed this mistake has been sacked.

I meant to say that it wouldn't compile on my system, but I could
make it work by using the templated constructor.

--
Neil Cerutti
Dec 2 '05 #16
Kai-Uwe Bux wrote:
Bob Hairgrove wrote:
On Fri, 02 Dec 2005 09:58:35 GMT, al***@start.no (Alf P. Steinbach)
wrote:
Since char and unsigned char are different types, you must use either
a C-style cast or reinterpret_cas t, as you have done. There is really
no way to avoid it if you must pass unsigned char to this function.
You're the second person to state that so I'm interesting in the
reeasoning.

The reason? Because std::string has no constructor that takes unsigned
char* as an argument.


Hm, std::string has a templated constructor:
template<class InputIterator>
basic_string(In putIterator begin, InputIterator end,
const Allocator& a = Allocator());
Since unsigned char is convertible to char, this constructor should match.

There is no coinstructor that takes ONLY an unsigned char* and an
integral value. He passed a unsigned char* and the integer 12.
Dec 4 '05 #17
Alf P. Steinbach wrote:
* Bob Hairgrove:
On Fri, 02 Dec 2005 09:58:35 GMT, al***@start.no (Alf P. Steinbach)
wrote:
Since char and unsigned char are different types, you must use either
a C-style cast or reinterpret_cas t, as you have done. There is really
no way to avoid it if you must pass unsigned char to this function.
You're the second person to state that so I'm interesting in the
reeasoning. The reason? Because std::string has no constructor that takes unsigned
char* as an argument.


That is incorrect.


It is correct.

Dec 4 '05 #18
* Ron Natalie:
Alf P. Steinbach wrote:
* Bob Hairgrove:
On Fri, 02 Dec 2005 09:58:35 GMT, al***@start.no (Alf P. Steinbach)
wrote:

> Since char and unsigned char are different types, you must use either
> a C-style cast or reinterpret_cas t, as you have done. There is really
> no way to avoid it if you must pass unsigned char to this function.
You're the second person to state that so I'm interesting in the
reeasoning.
The reason? Because std::string has no constructor that takes unsigned
char* as an argument.


That is incorrect.


It is correct.


Nope.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Dec 4 '05 #19

Mi************* @tomtom.com wrote:
Bob Hairgrove wrote:
On Fri, 02 Dec 2005 11:04:26 GMT, al***@start.no (Alf P. Steinbach)
wrote:
> The reason? Because std::string has no constructor that takes unsigned
> char* as an argument.

That is incorrect.


// test_uchar.cpp

#include <iostream>
#include <ostream>
#include <string>

int main()
{
const unsigned char msg[] = "Hello";
std::string s(msg);
std::cout << msg << std::endl;
}

Does that compile on your system?


Nope. However, this does:

#include <string>
const unsigned char msg[] = "Hello";
std::string s(msg,msg+sizeo f(msg));

The statement you're tyring to prove is: "
std::string has no constructor that takes one argument, of type
unsigned char*.
" That statement is of course true, but not really relevant.


Actually it is pretty obvious that std::string has no constructor that
takes unsigned char* as an argument just by looking at its
constructors... none of them take an unsigned char* as an argument. The
fact that you can pass one as an argument to some of these constructors
is a consequence of a language feature, mainly that it will
automatically do some conversions for you. The very important
distinction here is that unsigned char* is being converted to something
else, which is then passed as an argument to the function being called;
in other words no matter how you slice it the constructor is never
actually given an unsigned char* as it wouldn't accept it.

Not that I'm not enjoying this "No it doesn't," "Does too," "Does
not...,"method of arguing. It can be quite entertaining until
eventually it becomes annoying.

Dec 4 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
12826
by: Suzanne Vogel | last post by:
I'd like to convert a double to a binary representation. I can use the "&" bit operation with a bit mask to convert *non* float types to binary representations, but I can't use "&" on doubles. To get around this limitation on double, I'd like to keep the bits of the double the *same* but change its interpretation to long. I can use "&" on longs. I tried to use reinterpret_cast for this purpose, but it returned zero every time. double...
6
2792
by: hack_tick | last post by:
hi there i m trying to convert (viod *) data to (unsigned char) using reinterpret_cast as following void *aData; // some data already provided unsigned char c = reinterpret_cast<unsingned char>(aData); and the gcc is giving me the following error
5
4062
by: Christopher Benson-Manica | last post by:
enum foo {a,b,c,d}; int main() { unsigned int bar=reinterpret_cast< unsigned int >( a ); return bar; } g++ rejects the code based on the reinterpret_cast, so I presume it is not legal. Why not? And why should g++ accept the code when
4
4871
by: Alex Vinokur | last post by:
Should 'delete below (after casting to void*)' work fine? ------------------------------------------ char* p1 = new (nothrow) char; if (p1 == 0) return; void* p2 = reinterpret_cast<void*> (p1); delete p2; ------------------------------------------ Alex Vinokur
2
2434
by: Taran | last post by:
Hi All, I was trying some code which essentially does what the function 'func' here does. To simplify the things and make a consise post I have created a dummy app to show my problem. The issue was to assign a value to an array of pointers. The selection of array depends upon a condition and the void* ptr has to be casted to the correct type and inserted in the array.
27
4335
by: Noah Roberts | last post by:
What steps do people take to make sure that when dealing with C API callback functions that you do the appropriate reinterpret_cast<>? For instance, today I ran into a situation in which the wrong type was the target of a cast. Of course with a reinterpret_cast nothing complains until the UB bites you in the ass. It seems to me that there ought to be a way to deal with these kinds of functions yet still retain some semblance of type...
2
2955
by: jasm | last post by:
hello everybody! I have used reinterpret_cast for interpret a class object as a char*. This is the object: template<class T> class Coordinates { public: T *x; T *y;
2
2677
by: ciccio | last post by:
Hi, I was wondering what the main reason is why reinterpret_cast fails to work as expected when using optimizations. Here is the simple example code which fail to give the correct result when optimizing. #include <iostream> int main() { long D1 = 0xbcfbc4f0d9b65179; long D2 = 0xbcfbc4f0d9b65042;
0
8610
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6528
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4623
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.