473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Working with bytes in C++



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

Feb 28 '06 #1
7 14563
Alex wrote:

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
--
I'm not just a number. To many, I'm known as a string...
Feb 28 '06 #2
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

Feb 28 '06 #3
Alex wrote:
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
--
I'm not just a number. To many, I'm known as a string...
Feb 28 '06 #4
Ben Pope wrote:
private void nextRandomBytes(std::vector<byte> bytes);


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...
Feb 28 '06 #5

Ben Pope wrote:
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

Feb 28 '06 #6
Gavin Deane wrote:
Ben Pope wrote:
private void nextRandomBytes(std::vector<byte> bytes);


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
--
I'm not just a number. To many, I'm known as a string...
Feb 28 '06 #7

Ben Pope wrote:
Gavin Deane wrote:
Ben Pope wrote:
private void nextRandomBytes(std::vector<byte> bytes);


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.


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

Feb 28 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Adam T. Gautier | last post by:
I have been unable to solve a problem. I am working with MD5 signatures trying to put these in a database. The MD5 signatures are not generated using the python md5 module but an external...
1
by: Larry Menard | last post by:
Folks, I've written the world's simplest java UDF, and it is complaining that it can't load the method. The class seems OK, it's complaining about the method. The JDBC Sample UDFs (e.g.,...
8
by: den 2005 | last post by:
Hi everybody, I am not sure where to put this in this forum. So, I posted this at several topics. I created a class library that has two public methods Encrypt() and Decrypt(). I reference this...
4
by: SpreadTooThin | last post by:
client: import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(("192.168.1.101", 8080)) print 'Connected' s.send('ABCD') buffer = s.recv(4) print buffer s.send('exit')
6
by: RaulAbHK | last post by:
Dear all, I guess this is a basic question with an easy answer but I am a beginner and I would much apreciate your feedbacks. Let's say I have a library with some functionality to perform some...
0
by: George2 | last post by:
Hello everyone, I am using perfmon to watch the working set and virtual bytes, when I do a keyword search in SourceInsight. I found the value of working set is larger than virtual bytes when...
0
by: George2 | last post by:
Hello everyone, From the definition of working set, it is a subset of virtual pages resident in physical memory -- from book Windows Internals. It means working set could not be larger than...
3
by: PrabodhanP | last post by:
I have CSS based mouseover scrolling for divContent embeded in my webpage.It works fine in IE,but not working in mozilla-FF. It is located at the location.....
9
by: RichG | last post by:
I'm working with a data stream of 8 bytes in an embedded application. In most cases the data is byte aligned so I can define a structure and then memcpy the data directly to the structure elements. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.