How to get the size of BYTE array - BYTE const* pbBinary
(int)strlen(c_pbBinary) ??
=====================
typedef unsigned char BYTE;
std::ifstream file1("c:/test2.xml");
std::vector<BYTE> bytes(
(std::istreambuf_iterator<char>(file1))
,
(std::istreambuf_iterator<char>())
);
if(bytes.empty())
; // no bytes have been read
BYTE const* pbBinary = &bytes[0];
WriteComm((LPBYTE)pbBinary, 1193);
======================
DWORD WriteComm(const LPBYTE lpBuffer, DWORD dwCount);
Like what?
>How to get the size of BYTE array - BYTE const* pbBinary
>
>(int)strlen(c_pbBinary) ??
Is it an array of arbitrary bytes, or really a single byte null
terminated string of characters? For what you show above, you probably
just need to cast to LPCSTR, and drop the int cast since strlen
returns size_t - use it!
Remember that casting is generally evil and should be avoided. You
should ensure that you declare your data as what it really is - and be
consistent with it.
Dave
>>Can I just cast (BYTE const *) to const LPBYTE like the following?
>
>Like what?
>
>>How to get the size of BYTE array - BYTE const* pbBinary
>>
>>(int)strlen(c_pbBinary) ??
>
>Is it an array of arbitrary bytes, or really a single byte null
>terminated string of characters? For what you show above, you probably
>just need to cast to LPCSTR, and drop the int cast since strlen
>returns size_t - use it!
I have a byte array - (pbBinary) -
BYTE const* pbBinary = &bytes[0];
The bytes will contain all bytes in the file - test2.xml
std::ifstream file1("c:/test2.xml");
sine a socket function will take 2 parameters - (const LPBYTE, and
dwCount) -
DWORD WriteComm(const LPBYTE lpBuffer, DWORD dwCount);
So I need to change (BYTE const* pbBinary) - to - (const LPBYTE
pbBinary)
and get the size of (const LPBYTE pbBinary)
William:
What about just
WriteComm(&bytes[0], bytes.size());
--
David Wilkinson
Visual C++ MVP
No, you should never cast any values. Instead, use the correct type then you
don't need any conversions.
Further, you haven't understood the meaning of 'const' yet. This is further
complicated by the use of the typedef 'LPBYTE'. This 'LPBYTE' is actually
a 'BYTE*'. Now, a 'const LPBYTE' (also and IMHO better written as 'LPBYTE
const') is actually a 'BYTE* const', i.e. a constant pointer, which is
simply not compatible with a pointer to a constant. Read the C++ FAQ, it
explains that.
> (int)strlen(c_pbBinary) ??
Don't cast. strlen() returns a 'size_t' which is what you should use instead
of an 'int'.
> std::vector<BYTE> bytes[...]
> BYTE const* pbBinary = &bytes[0];
>
> WriteComm((LPBYTE)pbBinary, 1193);
In particular in C++, never use C-style casts. Seriously!
> DWORD WriteComm(const LPBYTE lpBuffer, DWORD dwCount);
Okay, if this function is declared like that, the author didn't know what
they were doing. They probably meant 'BYTE const*' (or 'const BYTE*')
instead of 'BYTE* const'. If you can change it, do so. If you can't and
still know that the buffer is treated as read-only, a const_cast with a
suitable comment will help.
Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
Why don't you just be consistent with the definition?
const BYTE * pbBinary = &bytes[0];
... or do what David Wilkinson suggests.
Dave