Connecting Tech Pros Worldwide Help | Site Map

BYTE array size

  #1  
Old November 20th, 2008, 07:35 AM
Chunekit Pong
Guest
 
Posts: n/a
I have a BYTE array -
BYTE const* pbBinary

I would like to know how many bytes in that byte array

but if I do - sizeof(* pbBinary); - then I got 1
but if I do - sizeof( pbBinary); - then I got 4

I am sure the array has hundreds of bytes

how can I do that?

======================

BYTE const* pbBinary = &bytes[0];
int size = sizeof( pbBinary);
  #2  
Old November 20th, 2008, 07:55 AM
Fred Zwarts
Guest
 
Posts: n/a

re: BYTE array size


"Chunekit Pong" <worlman385@yahoo.comwrote in message news:f54ai4l6o3hdj8k2j0as78nhm0166ndt4r@4ax.com...
Quote:
>I have a BYTE array -
BYTE const* pbBinary
You don't have a BYTE array, you have a pointer to BYTE.
(What is a BYTE?)
Quote:

I would like to know how many bytes in that byte array

but if I do - sizeof(* pbBinary); - then I got 1
but if I do - sizeof( pbBinary); - then I got 4

I am sure the array has hundreds of bytes

how can I do that?
Without other information you can't.


  #3  
Old November 20th, 2008, 08:15 AM
mliptak
Guest
 
Posts: n/a

re: BYTE array size


On Nov 20, 8:27*am, Chunekit Pong <worlman...@yahoo.comwrote:
Quote:
I have a BYTE array *-
BYTE const* pbBinary
>
I would like to know how many bytes in that byte array
>
but if I do - *sizeof(* pbBinary); *- then I got 1
but if I do - sizeof( pbBinary); - then I got 4
>
I am sure the array has hundreds of bytes
>
pbBinary is just a pointer.. it does not store the information about
number of bytes/elements it points to
if you care about the size of an "array", use std::vector<instead.
m.
  #4  
Old November 20th, 2008, 08:35 AM
Chunekit Pong
Guest
 
Posts: n/a

re: BYTE array size


On Thu, 20 Nov 2008 08:46:12 +0100, "Fred Zwarts" <F.Zwarts@KVI.nl>
wrote:
Quote:
>"Chunekit Pong" <worlman385@yahoo.comwrote in message news:f54ai4l6o3hdj8k2j0as78nhm0166ndt4r@4ax.com...
Quote:
>>I have a BYTE array -
>BYTE const* pbBinary
>
>You don't have a BYTE array, you have a pointer to BYTE.
>(What is a BYTE?)
>
Quote:
>>
>I would like to know how many bytes in that byte array
>>
>but if I do - sizeof(* pbBinary); - then I got 1
>but if I do - sizeof( pbBinary); - then I got 4
>>
>I am sure the array has hundreds of bytes
>>
>how can I do that?
>
>Without other information you can't.
>
The whole code is like this - the purpose is to convert binary file
into a Base64 string
=================
typedef unsigned char BYTE;
std::ifstream file1("c:/test2.png");

// read from test2.png into BYTE array
std::vector<BYTEbytes(
(std::istreambuf_iterator<char>(file1))
, (std::istreambuf_iterator<char>())
);
if(bytes.empty())
; // no bytes have been read

BYTE const* pbBinary = &bytes[0];
int size = sizeof(pbBinary);

unsigned long ulEncLen = 0;
char *pEncOut = NULL;

BOOL fRet = ::CryptBinaryToString( pbBinary, size,
CRYPT_STRING_BASE64, pEncOut, &ulEncLen );
====================
BOOL WINAPI CryptBinaryToString(
__in const BYTE *pbBinary,
__in DWORD cbBinary,
__in DWORD dwFlags,
__out_opt LPTSTR pszString,
__inout DWORD *pcchString
);
  #5  
Old November 20th, 2008, 09:35 AM
Fred Zwarts
Guest
 
Posts: n/a

re: BYTE array size


"Chunekit Pong" <worlman385@yahoo.comwrote in message news:ok7ai4hn1aifrcput99okpvvoricqu7qsa@4ax.com...
Quote:
On Thu, 20 Nov 2008 08:46:12 +0100, "Fred Zwarts" <F.Zwarts@KVI.nl>
wrote:
Quote:
>>"Chunekit Pong" <worlman385@yahoo.comwrote in message news:f54ai4l6o3hdj8k2j0as78nhm0166ndt4r@4ax.com...
Quote:
>>>I have a BYTE array -
>>BYTE const* pbBinary
>>
>>You don't have a BYTE array, you have a pointer to BYTE.
>>(What is a BYTE?)
>>
Quote:
>>
>>I would like to know how many bytes in that byte array
>>
>>but if I do - sizeof(* pbBinary); - then I got 1
>>but if I do - sizeof( pbBinary); - then I got 4
>>
>>I am sure the array has hundreds of bytes
>>
>>how can I do that?
>>
>>Without other information you can't.
>>
The whole code is like this - the purpose is to convert binary file
into a Base64 string
=================
typedef unsigned char BYTE;
std::ifstream file1("c:/test2.png");

// read from test2.png into BYTE array
std::vector<BYTEbytes(
(std::istreambuf_iterator<char>(file1))
, (std::istreambuf_iterator<char>())
);
if(bytes.empty())
; // no bytes have been read

BYTE const* pbBinary = &bytes[0];
int size = sizeof(pbBinary);
bytes.size () gives you the size of the vector.
  #6  
Old November 20th, 2008, 11:55 AM
Tonni Tielens
Guest
 
Posts: n/a

re: BYTE array size


On Nov 20, 9:25*am, Chunekit Pong <worlman...@yahoo.comwrote:
Quote:
The whole code is like this - the purpose is to convert binary file
You cannot get the size of an array, by doing sizeof on a pointer to
an external array. Only if the pointer is in fact declared as a
statically dimensioned array (eg. BYTE pbBinary[10]) you can get the
size of the array by doing sizeof(pbBinary). In your case you are in
the same scope, but still pbBinary is considered as a pointer to a
dynamically allocated or external array. Note that sizeof is
calculated compile time, and when you declare a pointer to an array,
the compiler CANNOT know what the size of the array will be to what
the pointer will point at runtime.
  #7  
Old November 20th, 2008, 01:35 PM
Rolf Magnus
Guest
 
Posts: n/a

re: BYTE array size


Chunekit Pong wrote:
Quote:
I have a BYTE array -
BYTE const* pbBinary
>
I would like to know how many bytes in that byte array
>
but if I do - sizeof(* pbBinary); - then I got 1
Well, *pbBinary is a BYTE, so you get the size of a BYTE.
Quote:
but if I do - sizeof( pbBinary); - then I got 4
pbBinary is a pointer to const BYTE, so you get the size of a pointer to
const BYTE.
Quote:
I am sure the array has hundreds of bytes
>
how can I do that?
You can't, unless the array contains some end marker that you can use to find
out.

  #8  
Old November 20th, 2008, 01:55 PM
James Kanze
Guest
 
Posts: n/a

re: BYTE array size


On Nov 20, 12:47 pm, Tonni Tielens <tonnitiel...@gmail.comwrote:
Quote:
On Nov 20, 9:25 am, Chunekit Pong <worlman...@yahoo.comwrote:
Quote:
Quote:
The whole code is like this - the purpose is to convert
binary file
Quote:
You cannot get the size of an array, by doing sizeof on a
pointer to an external array.
I know what you mean, but you really should say that you can't
get the size of an array given a pointer to its first element.
(Pointers to arrays, i.e. BYTE (*p)[ 200 ], exist, and in such
cases, sizeof( *p ) would be useful. But they aren't very
idiomatic.)
Quote:
Only if the pointer is in fact declared as a statically
dimensioned array (eg. BYTE pbBinary[10]) you can get the size
of the array by doing sizeof(pbBinary).
If something is declared as an array, it's not a pointer.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
  #9  
Old November 20th, 2008, 01:55 PM
James Kanze
Guest
 
Posts: n/a

re: BYTE array size


On Nov 20, 8:27 am, Chunekit Pong <worlman...@yahoo.comwrote:
Quote:
I have a BYTE array -
BYTE const* pbBinary
Quote:
I would like to know how many bytes in that byte array
Quote:
but if I do - sizeof(* pbBinary); - then I got 1
but if I do - sizeof( pbBinary); - then I got 4
Quote:
I am sure the array has hundreds of bytes
Ask whoever created the array to tell you. Once the array has
been converted to a pointer to the first element, all
information concerning the size has been lost.

A better solution might be to use std::vector.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
  #10  
Old November 20th, 2008, 03:05 PM
zr
Guest
 
Posts: n/a

re: BYTE array size


On Nov 20, 10:25*am, Chunekit Pong <worlman...@yahoo.comwrote:
Quote:
On Thu, 20 Nov 2008 08:46:12 +0100, "Fred Zwarts" <F.Zwa...@KVI.nl>
wrote:
>
>
>
Quote:
"Chunekit Pong" <worlman...@yahoo.comwrote in messagenews:f54ai4l6o3hdj8k2j0as78nhm0166ndt4r@4ax .com...
Quote:
>I have a BYTE array *-
BYTE const* pbBinary
>
Quote:
You don't have a BYTE array, you have a pointer to BYTE.
(What is a BYTE?)
>
Quote:
Quote:
I would like to know how many bytes in that byte array
>
Quote:
Quote:
but if I do - *sizeof(* pbBinary); *- then I got 1
but if I do - sizeof( pbBinary); - then I got 4
>
Quote:
Quote:
I am sure the array has hundreds of bytes
>
Quote:
Quote:
how can I do that?
>
Quote:
Without other information you can't.
>
The whole code is like this - the purpose is to convert binary file
into a Base64 string
=================
* * * * * * * * typedef unsigned char BYTE;
* * * * * * * * std::ifstream file1("c:/test2.png");
>
* * * * * * * * // read from test2.png into BYTE array
* * * * * * * * std::vector<BYTEbytes(
* * * * * * * * * * * * (std::istreambuf_iterator<char>(file1))
* * * * * * * * * * * * , (std::istreambuf_iterator<char>())
* * * * * * * * * * * * );
* * * * * * * * if(bytes.empty())
* * * * * * * * * * * * ; // no bytes have been read
>
* * * * * * * * BYTE const* pbBinary = &bytes[0];
* * * * * * * * int size = sizeof(pbBinary);
>
* * * * * * * * unsigned long ulEncLen = 0;
* * * * * * * * char *pEncOut = NULL;
>
* * * * * * * * BOOL fRet = ::CryptBinaryToString( pbBinary, size,
CRYPT_STRING_BASE64, pEncOut, &ulEncLen );
====================
BOOL WINAPI CryptBinaryToString(
* __in * * * const BYTE *pbBinary,
* __in * * * DWORD cbBinary,
* __in * * * DWORD dwFlags,
* __out_opt *LPTSTR pszString,
* __inout * *DWORD *pcchString
);
The previous posters have explained the problem. A possible fix to
your program:

Instead of:
int size = sizeof(pbBinary);
Try:
int size = bytes.size();
  #11  
Old November 20th, 2008, 03:35 PM
Andre Kostur
Guest
 
Posts: n/a

re: BYTE array size


Chunekit Pong <worlman385@yahoo.comwrote in
news:ok7ai4hn1aifrcput99okpvvoricqu7qsa@4ax.com:
Quote:
On Thu, 20 Nov 2008 08:46:12 +0100, "Fred Zwarts" <F.Zwarts@KVI.nl>
wrote:
>
Quote:
>>"Chunekit Pong" <worlman385@yahoo.comwrote in message
>>news:f54ai4l6o3hdj8k2j0as78nhm0166ndt4r@4ax.com. ..
Quote:
>>>I have a BYTE array -
>>BYTE const* pbBinary
>>
>>You don't have a BYTE array, you have a pointer to BYTE.
>>(What is a BYTE?)
>>
Quote:
>>>
>>I would like to know how many bytes in that byte array
>>>
>>but if I do - sizeof(* pbBinary); - then I got 1
>>but if I do - sizeof( pbBinary); - then I got 4
>>>
>>I am sure the array has hundreds of bytes
>>>
>>how can I do that?
>>
>>Without other information you can't.
>>
>
The whole code is like this - the purpose is to convert binary file
into a Base64 string
=================
typedef unsigned char BYTE;
std::ifstream file1("c:/test2.png");
>
// read from test2.png into BYTE array
std::vector<BYTEbytes(
(std::istreambuf_iterator<char>(file1))
, (std::istreambuf_iterator<char>())
);
if(bytes.empty())
; // no bytes have been read
>
BYTE const* pbBinary = &bytes[0];
int size = sizeof(pbBinary);
Why bother to make pbBinary and size ?
Quote:
>
unsigned long ulEncLen = 0;
char *pEncOut = NULL;
>
BOOL fRet = ::CryptBinaryToString( pbBinary, size,
CRYPT_STRING_BASE64, pEncOut, &ulEncLen );

Why not:

BOOL fRet = ::CryptBinaryToString(&bytes[0], bytes.size(),
CRYPT__STRING_BASE64, pEncOut, &ulEncLen);

?


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
WCF Byte Array Limitation in XBAP =?Utf-8?B?SXpvcmljaA==?= answers 3 June 27th, 2008 09:19 PM
Maximum Size of Byte Array Gerrit answers 23 October 17th, 2006 04:35 PM
byte array size for read or write of filestream news.microsoft.com answers 5 May 10th, 2006 12:35 PM
Maximum char array size? Walter Dnes (delete the 'z' to get my real address answers 11 November 14th, 2005 08:35 AM