# Board index » cppbuilder » Converting Byte array to integer

## Converting Byte array to integer

2004-06-23 05:33:42 PM
cppbuilder85
Hello,
If I place an integer into a byte array, how do I later
retrieve the integer value?
TMemoryStream *strm;
typedef Byte abc[8];
..
..
abc[0]=strm->Size;
So how do I now convert abc[] back to an integer value?
int a=abc[0];
just converts the first byte to an integer.
It may be a silly question but i'm struggling.
Thanks.
Andy

## Re:Converting Byte array to integer

Andy wrote:
##### Quote
If I place an integer into a byte array, how do I later
retrieve the integer value?
Why would you place an integer in a byte array ?

TMemoryStream *strm;
typedef Byte abc[8];
..
..
abc[0]=strm->Size;
This will not even compile. Why using a typedef ? Moreover
you would have assigned something to the first byte only;
abc[1] is the second byte.
Try:
Byte abc[8];
abc [0] = 100;
ShowMessage ( abc[0] ); // shows 100
abc [0] = 1000;
ShowMessage ( abc[0] ); // does not show 1000!
An integer is four bytes long. A Byte only one. abc[0] is one byte.
A Byte can only hold values 1 - 255.
##### Quote
So how do I now convert abc[] back to an integer value?

int a=abc[0];

just converts the first byte to an integer.
Yes indeed.
##### Quote
It may be a silly question but i'm struggling.
There is nothing silly in trying in vain while learning a language.
Please explain what you really want.
Hans.

## Re:Converting Byte array to integer

OK, I was hoping someone would have been able to just say "Oh!
this is how you do it..."
The code I'm talking about is from the Turbopower Lockbox
code in the help files that has been translated from Delphi
into C++.
It doesn't work and I was trying to figure out how to make it
work. The integer is placed into the byte array so that it
can be written as the first block of the stream and contains
the size of the stream data. The other half of the code which
reads the data in reads the first block to decide how big the
stream is. The code isn't working!
I think the code uses an 8 bit Byte array because the ->Size
property of the Stream is an int64 (so 8*8bits = 64).
The abc[0] = strm->Size command seems to do the job and put the
value into the byte array and I'm having great difficulty in getting it out again.
Thanks for your help so far and sorry I didn't explain more
fully in the first place.
Andy
Hans Galema < XXXX@XXXXX.COM >wrote:
##### Quote
Andy wrote:

>If I place an integer into a byte array, how do I later
>retrieve the integer value?

Why would you place an integer in a byte array ?
>
>TMemoryStream *strm;
>typedef Byte abc[8];
>..
>..
>abc[0]=strm->Size;

This will not even compile. Why using a typedef ? Moreover
you would have assigned something to the first byte only;
abc[1] is the second byte.

Try:
Byte abc[8];

abc [0] = 100;
ShowMessage ( abc[0] ); // shows 100

abc [0] = 1000;
ShowMessage ( abc[0] ); // does not show 1000!

An integer is four bytes long. A Byte only one. abc[0] is one byte.
A Byte can only hold values 1 - 255.

>So how do I now convert abc[] back to an integer value?
>
>int a=abc[0];
>
>just converts the first byte to an integer.

Yes indeed.

>It may be a silly question but i'm struggling.

There is nothing silly in trying in vain while learning a language.

Please explain what you really want.

Hans.

{smallsort}

## Re:Converting Byte array to integer

On 23 Jun 2004 05:09:33 -0700, Andy wrote:
##### Quote
The abc[0] = strm->Size command seems to do the job and put the
value into the byte array and I'm having great difficulty in getting it out again.
try a union:
union U
{
char Bytes[8];
int64 Num;
} thing;
thing.Bytes[0[ = 100;
etc
--
good luck,
liz

## Re:Converting Byte array to integer

Thanks Liz,
I'll give that a try. I've never used a Union before but that
looks promising.
Thank you
Andy
Liz Albin < XXXX@XXXXX.COM >wrote:
##### Quote
On 23 Jun 2004 05:09:33 -0700, Andy wrote:

>The abc[0] = strm->Size command seems to do the job and put the
>value into the byte array and I'm having great difficulty in getting it out again.

try a union:

union U
{
char Bytes[8];
int64 Num;
} thing;

thing.Bytes[0[ = 100;

etc

--
good luck,
liz

## Re:Converting Byte array to integer

Before I tear my hair out completely, does the following do
what I think it does? i.e. would this actually work?
abc[0]=strm->Size;
Andy
"Andy" < XXXX@XXXXX.COM >wrote:
##### Quote

Hello,

If I place an integer into a byte array, how do I later
retrieve the integer value?

TMemoryStream *strm;
typedef Byte abc[8];
..
..
abc[0]=strm->Size;

So how do I now convert abc[] back to an integer value?

int a=abc[0];

just converts the first byte to an integer.

It may be a silly question but i'm struggling.

Thanks.

Andy

## Re:Converting Byte array to integer

Andy wrote:
##### Quote
Before I tear my hair out completely, does the following do
what I think it does? i.e. would this actually work?

abc[0]=strm->Size;
You were already told that that does nor work. The reason
was told too: A bute can only have values 0 to 255.
Hans.

## Re:Converting Byte array to integer

Andy wrote:
##### Quote
OK, I was hoping someone would have been able to just say "Oh!
this is how you do it..."
You can use memcpy() to copy the four bytes of an integer to a byte array.
##### Quote
I think the code uses an 8 bit Byte array because the ->Size
property of the Stream is an int64 (so 8*8bits = 64).
Since when is TMemoryStream::Size an __int 64 ? It is a
normal int of four bytes.
Please trim quotes. You are reposting complete messages.
Hans.

## Re:Converting Byte array to integer

##### Quote
Since when is TMemoryStream::Size an __int 64 ? It is a
normal int of four bytes.

TStream::Size
__property __int64 Size = {read=GetSize, write=SetSize64, nodefault};
BCB v6?
Andy

## Re:Converting Byte array to integer

Andy wrote:
##### Quote
>Since when is TMemoryStream::Size an __int 64 ? It is a
>normal int of four bytes.
>

TStream::Size

__property __int64 Size = {read=GetSize, write=SetSize64, nodefault};

BCB v6?
In bcb3 TStream::Size is an int.
But why mentioning TStream suddenly ? You were speaking
##### Quote
TMemoryStream *strm;
typedef Byte abc[8];
..
..
abc[0]=strm->Size;
Hans.

## Re:Converting Byte array to integer

##### Quote
In bcb3 TStream::Size is an int.

But why mentioning TStream suddenly ? You were speaking

TMemoryStream->Size is inherited from TStream->Size.
Andy

## Re:Converting Byte array to integer

Thank you for your help. My solution is...
typedef Byte TDESBlock[8];
To put the integer into the byte array..
TDESBlock DESBlock;
__int64 n = strm->Size;
memcpy(DESBlock,&n,sizeof(DESBlock);
..
..
To get the integer out of the byte array...
__int64 n;
memcpy(&n,DESBlock,sizeof(n));
..
..
Andy

## Re:Converting Byte array to integer

Andy wrote:
##### Quote
typedef Byte TDESBlock[8];
typedef Byte TDESBlock[sizeof (__int64)];
typedef Byte TDESBlock[sizeof (strm->Size)];
##### Quote
To put the integer into the byte array..

TDESBlock DESBlock;
__int64 n = strm->Size;
memcpy(DESBlock,&n,sizeof(DESBlock);
You do not need an extra n.
memcpy(DESBlock,&strm->Size,sizeof(DESBlock);
Hans.

## Re:Converting Byte array to integer

Liz Albin < XXXX@XXXXX.COM >writes:
##### Quote
try a union:

union U
{
char Bytes[8];
int64 Num;
} thing;

thing.Bytes[0[ = 100;
To later read the Num member? This would have undefined behavior.
From a union, only reading the member that was last written has defined
behavior.

## Re:Converting Byte array to integer

"Andy" < XXXX@XXXXX.COM >writes:
##### Quote
If I place an integer into a byte array, how do I later
retrieve the integer value?
The opposite way.
This may sound stupid, and maybe it is, but it's hard to say more.
##### Quote
TMemoryStream *strm;
typedef Byte abc[8];
..
..
abc[0]=strm->Size;

So how do I now convert abc[] back to an integer value?

int a=abc[0];

just converts the first byte to an integer.
The "slicing" happens in the first of these assignments. abc[0] is of type
Byte (which probably means unsigned char), and stream->Size is certainly
larger.
The question is: How do you want the number to be represented by the 8 Bytes?
If you have complete freedom, and if both conversion happen within the same
program, compiled with the same compiler version, a simple solution is
__int64 size(strm->Size);
std::size_t const nrOfBytes(sizeof size);
Byte abc[nrOfBytes];
unsigned char * const start(reinterpret_cast<unsigned char *>(&size));
std::copy(start,start+nrOfBytes,abc);
and later
std::copy(abc,abc+nrOfBytes,start);
You could also use std::memcopy() or std::memmove(), but a decently written
Standard Library implementation should do just that here if the types involved
allow it.