Okay, so C++ has no specific byte class, but what they do have is an unsigned char class to convert ints/doubles/floats to binary/bytes.

In trying to convert some Java to C++, I am trying to keep some of the function signatures the same or close to the same, and in so doing, have realized that if you have an unsigned char*, you can't actually get the size of the number of bytes this represents. It was an easy solution to convert though because I was able to do memcpy of all the primitives into the char* and voila, done.

This is a problem. So now I am thinking of doing a
vector<unsigned char> but cant figure out how to copy a float into the vector to get each byte.

Any suggestions?

Kind of a crazy idea but what if you write all of the ints/ doubles to a txt/ binary file then read them back in as chars?

Or just create a function that converts to necessary data-types.

Well I could still use the memcpy that I have now in the functions (in the functions I still have a good idea of the size of the char *) and copy into the vector there, but someone told me to try and not use memcpy because it was too dangerous ;-).

I am using the vectors so that later down the road, I will know exactly how big the values are, and know the size of bytes. I could also write my own struct/class to accomplish the same thing, but why do that for something so small/simple.

>what they do have is an unsigned char class to
>convert ints/doubles/floats to binary/bytes.

You're thinking of type punning, I believe. Tricky business that stuff, not usually a good idea in C++.

>if you have an unsigned char*, you can't actually get
>the size of the number of bytes this represents

Obviously because it's a pointer, not an array of bytes. But you can use sizeof on the original object or the type of the original object to get the number of bytes.

>vector<unsigned char> but cant figure out how to
>copy a float into the vector to get each byte

It sounds like you're on the express train to undefinedbehaviorville with this conversion. But ask and ye shall receive:

double d = 123.456;
unsigned char *p = (unsigned char*)&d;
std::vector<unsigned char> v ( p, p + sizeof d );

Well let me ask you this, what do you think the best method is. Basically I am making a library that converts all the standard types into nothing but bytes aka unsigned chars in c++, and then can convert them back into the correct types.

For the Integer, we do mutliple sizes of ints, so I have to pad.

Well let me ask you this, what do you think the best method is. Basically I am making a library that converts all the standard types into nothing but bytes aka unsigned chars in c++, and then can convert them back into the correct types.

For the Integer, we do mutliple sizes of ints, so I have to pad.

>Basically I am making a library that converts all the standard types
>into nothing but bytes aka unsigned chars in c++, and then can
>convert them back into the correct types.

Why?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.