Connecting Tech Pros Worldwide Forums | Help | Site Map

Working with bytes in C++

Alex
Guest
 
Posts: n/a
#1: Feb 28 '06


Hello people,

I have a code written in JAVA that creates field of bytes:

byte[] uuid = new byte[16];

Now I have to translate this line into C++. I'm working in VS 6.0.
(unfortunately I have to).

Is there any class in MFC framework that I can use like bytes? Or I
have to use char[8] as a byte :(((? I want to avoid this, cause I need
bytes later, and fields of chars are not very comfortable to work with.


Or I have to create my own class? What do you suggest me?

I'm pretty new in this, don't mind.

Thx for answers,

Sash


Ben Pope
Guest
 
Posts: n/a
#2: Feb 28 '06

re: Working with bytes in C++


Alex wrote:[color=blue]
>
> Hello people,
>
> I have a code written in JAVA that creates field of bytes:
>
> byte[] uuid = new byte[16];
>
> Now I have to translate this line into C++. I'm working in VS 6.0.
> (unfortunately I have to).
>
> Is there any class in MFC framework that I can use like bytes? Or I[/color]

We don't deal with MFC here. There is no such type as "byte" in C++. A
byte and a char are sually both 8 bits, but there is no guarantee that
there is an 8 bit type in C++.
[color=blue]
> have to use char[8] as a byte :(((? I want to avoid this, cause I need
> bytes later, and fields of chars are not very comfortable to work with.[/color]

I'm not sure I understand your requirement. Where do you have to use
bytes, and how are they declared?
[color=blue]
> Or I have to create my own class? What do you suggest me?[/color]

No requirement to create your own class.

I would personally use:
#include <vector>
std::vector<unsigned char> uuid(16);

or if you prefer:
#include <vector>
typedef unsigned char byte
std::vector<byte> uuid(16);

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Alex
Guest
 
Posts: n/a
#3: Feb 28 '06

re: Working with bytes in C++


Thank you a lot, it helped! I'm not really used to work with vectors in
C++, just one question. This considers sentence "I'll need them later"
I wrote before. It's about this line:

private void nextRandomBytes(byte[] bytes);

How, I can write arguments byte[] bytes in this function? I can not
think about anything that can match with "std::vector<byte> uuid(16)".
Which cast I need?

And later, I'll need to parse this bytes to integers. How I can access
them in vector field?

Thank you once more.

Sash

Ben Pope
Guest
 
Posts: n/a
#4: Feb 28 '06

re: Working with bytes in C++


Alex wrote:[color=blue]
> Thank you a lot, it helped! I'm not really used to work with vectors in
> C++, just one question. This considers sentence "I'll need them later"
> I wrote before. It's about this line:
>
> private void nextRandomBytes(byte[] bytes);[/color]

What is byte? How does the function know how many elements there are in
bytes?
[color=blue]
> How, I can write arguments byte[] bytes in this function? I can not
> think about anything that can match with "std::vector<byte> uuid(16)".
> Which cast I need?[/color]

Do not cast, it is a sign of bad design.

private void nextRandomBytes(std::vector<byte> bytes);

If byte is the same type as the vector::value_type, then a call:
nextRandomBytes(uuid);

Will work even for your declaration of nextRandomBytes.
[color=blue]
> And later, I'll need to parse this bytes to integers. How I can access
> them in vector field?[/color]

Parse how?

int index = 3; // value 0-15 inclusive
integer i = uuid[index];

Works, for a suitable definition of "parse".

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Ben Pope
Guest
 
Posts: n/a
#5: Feb 28 '06

re: Working with bytes in C++


Ben Pope wrote:[color=blue]
> private void nextRandomBytes(std::vector<byte> bytes);[/color]

Oops, I meant:
private void nextRandomBytes(std::vector<byte>& bytes);

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Gavin Deane
Guest
 
Posts: n/a
#6: Feb 28 '06

re: Working with bytes in C++



Ben Pope wrote:[color=blue]
> private void nextRandomBytes(std::vector<byte> bytes);[/color]

private void nextRandomBytes(const std::vector<byte>& bytes);

No reason to pass by value is there?

Gavin Deane

Ben Pope
Guest
 
Posts: n/a
#7: Feb 28 '06

re: Working with bytes in C++


Gavin Deane wrote:[color=blue]
> Ben Pope wrote:[color=green]
>> private void nextRandomBytes(std::vector<byte> bytes);[/color]
>
> private void nextRandomBytes(const std::vector<byte>& bytes);
>
> No reason to pass by value is there?[/color]

Of course, I beat you to the reply! In fact, passing by value would
probably be pointless since I presume that it is supposed to modify
bytes, it has no return value.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Gavin Deane
Guest
 
Posts: n/a
#8: Feb 28 '06

re: Working with bytes in C++



Ben Pope wrote:[color=blue]
> Gavin Deane wrote:[color=green]
> > Ben Pope wrote:[color=darkred]
> >> private void nextRandomBytes(std::vector<byte> bytes);[/color]
> >
> > private void nextRandomBytes(const std::vector<byte>& bytes);
> >
> > No reason to pass by value is there?[/color]
>
> Of course, I beat you to the reply! In fact, passing by value would
> probably be pointless since I presume that it is supposed to modify
> bytes, it has no return value.[/color]

Yes, I hadn't seen your reply when I started to write mine, but it had
appeared by the time I'd finished and posted :-)

And you're right, the function does look like it's supposed to modify
bytes, so a const reference is no good either, it needs to be a
non-const reference.

Gavin Deane

Closed Thread


Similar C / C++ bytes