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 3109
* ro**********@gm ail.com:
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,
No, it isn't.
--
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?
On Fri, 02 Dec 2005 09:05:31 -0500, Kai-Uwe Bux <jk********@gmx .net>
wrote: 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; }
Not on my machine:
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
test_uchar.cpp:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
Error: Unresolved external 'std::basic_str ing<char,
std::char_trait s<char>, std::allocator< char> >::basic_string <char,
std::char_trait s<char>, std::allocator< char> >(const unsigned char *,
const unsigned char *, const std::allocator< char>&)' referenced from
E:\TEST_UCHAR.O BJ
--
Bob Hairgrove No**********@Ho me.com
On 2 Dec 2005 05:23:54 -0800, Mi************* @tomtom.com wrote: Nope. However, this does:
#include <string> const unsigned char msg[] = "Hello"; std::string s(msg,msg+sizeo f(msg));
Not on my machine:
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
test_uchar.cpp:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
Error: Unresolved external 'std::basic_str ing<char,
std::char_trait s<char>, std::allocator< char> >::basic_string <char,
std::char_trait s<char>, std::allocator< char> >(const unsigned char *,
const unsigned char *, const std::allocator< char>&)
' referenced from E:\TEST_UCHAR.O BJ
--
Bob Hairgrove No**********@Ho me.com
On Sun, 04 Dec 2005 22:48:59 +0100, Bob Hairgrove
<in*****@bigfoo t.com> wrote: On Fri, 02 Dec 2005 09:05:31 -0500, Kai-Uwe Bux <jk********@gmx .net> wrote:
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; }
Not on my machine:
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland test_uchar.cpp : Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland Error: Unresolved external 'std::basic_str ing<char, std::char_trai ts<char>, std::allocator< char> >::basic_string <char, std::char_trai ts<char>, std::allocator< char> >(const unsigned char *, const unsigned char *, const std::allocator< char>&)' referenced from E:\TEST_UCHAR. OBJ
OK -- it compiles, but it doesn't link because Borland provides no
*compiler magic* to make it work.
--
Bob Hairgrove No**********@Ho me.com
On Sun, 04 Dec 2005 22:49:59 +0100, Bob Hairgrove
<in*****@bigfoo t.com> wrote: On 2 Dec 2005 05:23:54 -0800, Mi************* @tomtom.com wrote:
Nope. However, this does:
#include <string> const unsigned char msg[] = "Hello"; std::string s(msg,msg+sizeo f(msg));
Not on my machine:
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland test_uchar.cpp : Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland Error: Unresolved external 'std::basic_str ing<char, std::char_trai ts<char>, std::allocator< char> >::basic_string <char, std::char_trai ts<char>, std::allocator< char> >(const unsigned char *, const unsigned char *, const std::allocator< char>&) ' referenced from E:\TEST_UCHAR.O BJ
OK -- it compiles, but it doesn't link because Borland provides no
*compiler magic* to make it work.
--
Bob Hairgrove No**********@Ho me.com
Ron Natalie wrote: 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.
True, but utterly irrelevant:
May I remind you of the original post:
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; }
The operative phrase being "*how* can I avoid...", the OP asks about a way
to implement GetStringFromBu ffer that avoids casts. This clearly can be
done using the templated std::string constructor I mentioned. The following
solution is essentially stolen from Alf Steinbach's post:
std::string GetStringFromBy teBuffer(const BYTE* const buffer, int pos )
{
return std::string( buffer+pos, buffer+pos+12 );
}
This fits the specs given by the OP. The claim that "there is really no way
to avoid it [casting] if you must pass unsigned char to this function" is
therefore false. The line "Because std::string has no constructor that
takes unsigned char* as an argument" was given as a reason, and since it
tries to argue something false, it is false itself or fails to imply what
it is supposed to argue. Make your pick: if you read it as narrow as
std::string ( unsigned char*, int ) is not valid
then it fails to imply what it is supposed to argue; if you use a broader
reading, the statement will become false.
Best
Kai-Uwe Bux
* Bob Hairgrove: On Sun, 04 Dec 2005 22:49:59 +0100, Bob Hairgrove <in*****@bigfoo t.com> wrote:
On 2 Dec 2005 05:23:54 -0800, Mi************* @tomtom.com wrote:
Nope. However, this does:
#include <string> const unsigned char msg[] = "Hello"; std::string s(msg,msg+sizeo f(msg));
Not on my machine:
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland test_uchar.cpp : Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland Error: Unresolved external 'std::basic_str ing<char, std::char_trai ts<char>, std::allocator< char> >::basic_string <char, std::char_trai ts<char>, std::allocator< char> >(const unsigned char *, const unsigned char *, const std::allocator< char>&) ' referenced from E:\TEST_UCHAR.O BJ
OK -- it compiles, but it doesn't link because Borland provides no *compiler magic* to make it work.
Try
bcc32 -D"_RWSTD_COMPIL E_INSTANTIATE" -w-ccc -w-rch test_uchar.cpp
Hth.,
- Alf
PS: I recommend using some other free compiler, e.g. g++ or MSVC; <url:
http://home.no.net/dubjai/win32cpptut/html/w32cpptut_01_01 _02.html>.
--
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?
Bob Hairgrove wrote: Mi************* @tomtom.com wrote:
#include <string> const unsigned char msg[] = "Hello"; std::string s(msg,msg+sizeo f(msg));
Not on my machine:
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland test_uchar.cpp: Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland Error: Unresolved external 'std::basic_str ing<char, std::char_trait s<char>, std::allocator< char> >::basic_string <char, std::char_trait s<char>, std::allocator< char> >(const unsigned char *, const unsigned char *, const std::allocator< char>&)
That's a bug in the standard library supplied with Borland C++.
The body of the constructor is never defined.
My workaround is to avoid using that constructor.
* Old Wolf: Bob Hairgrove wrote:
Mi************* @tomtom.com wrote:
#include <string> const unsigned char msg[] = "Hello"; std::string s(msg,msg+sizeo f(msg));
Not on my machine:
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland test_uchar.cpp: Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland Error: Unresolved external 'std::basic_str ing<char, std::char_trait s<char>, std::allocator< char> >::basic_string <char, std::char_trait s<char>, std::allocator< char> >(const unsigned char *, const unsigned char *, const std::allocator< char>&)
That's a bug in the standard library supplied with Borland C++. The body of the constructor is never defined. My workaround is to avoid using that constructor.
See my reply to the same posting -- the constructor body is defined in
a file that is conditionally included by <string>.
I suspect there is also a way to link a compiled version of that file,
using some language extension (doing the job of 'export'), but, it's an
old compiler, so I don't care to use the time to find out.
Cheers,
- Alf
--
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?
On Mon, 05 Dec 2005 01:06:01 GMT, al***@start.no (Alf P. Steinbach)
wrote: Try
bcc32 -D"_RWSTD_COMPIL E_INSTANTIATE" -w-ccc -w-rch test_uchar.cpp
Hth.,
No, it doesn't really "help" (HTH == "hope this helps") <g>
The basic question remains:
Is it *required* by the C++ standard that your code compiles (and
links)? And if you think so, please explain why (and not just: "it
does").
[I hope you realize that the above "hack" can create serious problems
with code that is distributed among different DLLs which rely on
things defined in the DLLs supplied by Borland (e.g., anything even
remotely depending on VCL)?]
--
Bob Hairgrove No**********@Ho me.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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
|
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
|
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
|
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.
| |
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...
|
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;
|
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;
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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...
|
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();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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
|
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...
| |