unsigned __int8 result[]= new unsigned __int8[sizeof(username) * 4];
IntelliSense: initialization with '{...}' expected for aggregate object
The types are not the same; you cannot initialize an array with a pointer.
new unsigned __int8[sizeof(username) * 4]; returns a unsigned __int8*, not unsigned __int8[]
change your code to
unsigned __int8* result = new unsigned __int8[sizeof(username) * 4];
unsigned __int8 *result = new unsigned __int8[sizeof(username) * 4];
new returns a pointer, not an array. You should declare
unsigned __int8* result = ....
here, result is an array of __int8, so you can't assign one value into the entire array. You actually want:
unsigned __int8* p_result = new unsigned __int8[sizeof username * 4];
Related
I have an array of chars, stored in a char*. From this pointer I want to copy the elements from a specific offset to another to a std::vector<uint16_t>. I need to combine always two chars, cause they are meant to be one 16 bit value.
The start and end offset is known and I know this values should be integers. But what is the best way to cast and copy this array. It's about 25000 uint values, so it should be an efficient way.
Following a code snippet to make it more clear. The problem with this snippet, it uses each char value as an own integer, but should combine always two chars two one 16 bit value. So the resulting vector has a size of 50688, but it should be 25344.
Simple:
char * buf = new char[bufSize];
std::vector<uint16_t> vec;
vec.reserve(numPix);
vec.insert(vec.begin(), &buf[startOffset], &buf[endOffset]);
Original:
const size_t numPixel = m_cameraParams.width * m_cameraParams.height; // 25344
const size_t numBytesDistance = numPixel * m_distanceByteDepth; // 50688
const size_t numBytesIntensity = numPixel * m_intensityByteDepth; // 50688
const size_t numBytesConfidence = numPixel * m_confidenceByteDepth; // 50688
std::vector<uint16_t> m_distanceMap;
std::vector<uint16_t> m_intensityMap;
std::vector<uint16_t> m_confidenceMap;
char * inputBuffer = new char[bufferSize];
// buffer filled with data received from tcp connection
//-----------------------------------------------
// Extract the Images depending on the informations extracted from the XML part
size_t offsetDistance = offset;
size_t offsetIntensity = offsetDistance + numBytesDistance;
size_t offsetConfidence = offsetIntensity + numBytesIntensity;
offset = offsetConfidence + numBytesConfidence;
m_distanceMap.reserve(numPixel);
m_distanceMap.insert(m_distanceMap.begin(), &inputBuffer[offsetDistance], &inputBuffer[offsetIntensity]);
m_intensityMap.reserve(numPixel);
m_intensityMap.insert(m_intensityMap.begin(), &inputBuffer[offsetIntensity], &inputBuffer[offsetConfidence]);
m_confidenceMap.reserve(numPixel);
m_confidenceMap.insert(m_confidenceMap.begin(), &inputBuffer[offsetConfidence], &inputBuffer[offset]);
If I have
void *temp = malloc(128);
memset(temp, 0 , 128);
And I want to read the first byte alone, following is what I'm doing.
char a[2];
strncpy(a, (char*)temp, 1);
int p = a[0];
//p will be zero in this case
Q1. I'm sure there is a more elegant way to achieve the same. If so, what would it be?
Q2. Is there a way I can alter the value of that single byte alone?
Say I want the first byte to have the value equivalent to the int value 48 (i.e. 00110000)
How would I do that?
I was able to make no progress with the write.
you can cast it to char * then access the memory
char *buff = temp;
char p = buff[0]; // read first byte
I am trying to convert a short array into a bytearray in qt.
is there any function available to do the casting.
if i have to use const char * for conversion how should i do it.
and is there any better way than to use reinterpret cast
Thanks in advance.
You can convert an array of short to bytes simply by casting a byte pointer to the short array
short s[10];
unsigned char *p = reinterpret_cast<unsigned char*>(s);
then use the pointer to go through all the bytes in the array where you copy the *p to a byte array if u wish.
for ( unsigned char *p = reinterpret_cast<unsigned char*>(s);
p < s + sizeof(s);
++p)
{...}
You can use reinterpret_cast to do this, but be aware that you are making your code very architecture specific. Use of reinterpret_cast doesn't exactly guarantee your code is wrong, but it should be ringing warning bells.
If what you want to do is, given an array of shorts, produce an array of bytes with the same values, you probably want this:
void copy(char *to_byte_array, short const *from_short_array, std::size_t size)
{
for (std::size_t pos = 0; pos != size; ++pos)
{
to_byte_array[pos] = from_short_array[pos];
}
}
if you use reinterpret_cast, your short array containing say 20, 30, 40, will look like an array of chars containing 0, 20, 0, 30, 0, 40 (or possibly 20, 0, 30, 0, 40, 0, depending on architecture).
I want to write a a mix of int, char, real in void *data.
I am using a file pointer to run through the data block.
Now my question is that since the data type is void, I have to typecast it to int while writing integer and char for writing string.
While typecasting I used the following sample code:
*((int *)data+0) = 14; //writing int
*((int *)data+4) = 5; //writing int, left a space of 4 bytes for int
*((char *)data+8) = 'a'; //writing char
*((char *)data+9) = 'f'; //writing char
But then while reading the values back it didnt give the correct value.
cout<<*((int *)data+0);
cout<<*((int *)data+3);
cout<<*((char *)data+8);
Is the way my code is written correct? I am doubtful about it as data is void.
*((int *)data+4) = 5; // writing 4th int
cout<<*((int *)data+3); // but reading third one
And just in case, ((int *)data+4) points to 4th integer (that is, 16th byte given int size = 4), not to 4th byte. That is, you code overwrites bytes 0-3, then 16-19, then 8th, then 9th. What you probably meant is: *(int *)( (char*)data + X )
Edited to correct mistake pointed out by MSalters
Apart from the typo that others have mentioned (data+3 instead of data+4), you also need to change e.g.
*((int *)data+4)
to
*((int *)data+1)
because adding 4 to an int * doesn't add 4 to the address, it adds 4 * sizeof (int).
If you need to write to an offset that is not a multiple of sizeof(int) (say, 7), you need:
*(int *)((char *)data+7)
For this reason, it might be better to make data a char * to start with, so you can just say
*(int *)(data+7)
Use a class or a struct.
Here, pointer arithmetics is misleading you. When you add 4 to an int * you are adding actually four times sizeof int.
If your data has a constant layout, why don't you just use a struct such as
struct MemoryLayout {
int _first;
int _second;
char _c1;
char _c2;
};
?
You're writing the second one with:
*((int *)data+4) = 5; // offset = 4
and reading it back with:
cout<<*((int *)data+3); // offset = 3
In addition, the (int*) cast is binding to data, not data+4 so that your 4 is scaled up by the size of an int.
If you really want to do this (and a struct is not a possibility due to variances in the data formats), you should cast data to a char*, then add the number of bytes to get the char offset, then cast that to your desired type.
That would be something like:
*((int*)((char*)(data + 0)) = 14; //writing int
*((int*)((char*)(data + 4)) = 5; //writing int
*((char*)data + 8) = 'a'; //writing char
*((char*)data + 9) = 'f'; //writing char
int Data;
//char Data;
//float Data;
FILE *File = fopen("File.txt","wb");
fwrite((char *)&Data,sizeof(Data),1,File);
fclose(File);
File = fopen("File.txt","rb");
fread((char *)&Data,sizeof(Data),1,File);
fclose(File);
...
int DataInt1 = 200;
char DataChar1 = 'N';
FILE *File = fopen("File.txt","wb");
fwrite((char *)&DataInt1,sizeof(DataInt1),1,File);
fwrite((char *)&DataChar1,sizeof(DataChar1),1,File);
fclose(File);
int DataInt2 = 0;
char DataChar2 = 0;
File = fopen("File.txt","rb");
fread((char *)&DataInt2,sizeof(DataInt2),1,File);
fread((char *)&DataChar2,sizeof(DataChar2),1,File);
fclose(File);
printf("%d %d!\n",DataInt2,DataChar2);
I simply allocate some memory for a character and wanna do then some pointer arithmetic.
In this case I wanna write '\x0a' to byte 32 as follows:
#define HDR_SIZE 32
int size = 52;
unsigned char *readXPacket = (unsigned char *) malloc (size * sizeof (unsigned char));
*readXPacket + HDR_SIZE = '\x0a';
When I try doing that I get the following error message: non-value in assignment. Anyone an idea what is wrong here?
Thanks
Change your assignment to:
*(readXPacket + HDR_SIZE) = '\x0a';
What's wrong with the obvious:
readXPacket[HDR_SIZE] = '\x0a';
which is both shorter and clearer. and as you are using C++, why not say:
unsigned char * readXPacket = new unsigned char[size];
Or better still:
std::vector <unsigned char> readXPacket( size );
and have C++ manage the memory for you.
Try...
*( readXPacket + HDR_SIZE ) = '\x0a';