Working with bytes in C++

A

Alex

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
 
B

Ben Pope

Alex said:
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

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++.
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.

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

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
 
A

Alex

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
 
B

Ben Pope

Alex said:
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);

What is byte? How does the function know how many elements there are in
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?

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.
And later, I'll need to parse this bytes to integers. How I can access
them in vector field?

Parse how?

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

Works, for a suitable definition of "parse".

Ben Pope
 
G

Gavin Deane

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

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

No reason to pass by value is there?

Gavin Deane
 
B

Ben Pope

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

No reason to pass by value is there?

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
 
G

Gavin Deane

Ben said:
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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,537
Latest member
BellCorone

Latest Threads

Top