Merging bytes.

Hi!

I'm creating program, that gets bytes from server. I store them in array.

When they are printed out 1 by 1, it shows line of unreadable simbols.

So i have to merge those bytes to get readable info( Like 2 first elements of array merger together mean somekind of value).

Anyone can help me ?

p.s. i kinda tried with bitset, but it didn't help :(

Thanks!
There is no such thing as "merging bytes". All of memory is just a sequence of bytes, not bound to each other by anything other than linear order. If you put your bytes in the right order, you lust need to interpet them as the right type.

Example
1
2
3
4
5
6
#include <iostream>
 
int main() {
   char bytes[] = {5, 1, 0, 0};
   std::cout << *(int*)(bytes+0);//should print (1*256+5=) 231
}


Note how bytes are in an apparently wrong order. See http://en.wikipedia.org/wiki/Endianness
But reading one element gives unreadable simbol ...

i need to put together those 2 elements ...

so i need to make 2 8 bits in one 16 bits
00110101 + 10111001 = 0011010110111001


or sum those 2 8bits together ?
Did you run my example?

By being next to each other, two bytes are as "merged" as they can be. Types only exist in the programming language. Machine can't tell a char array from an int or anything else. Cast your pointers to appropriate type and it will work. If it doesn't, bad endian is likely to be the culprit.
i think it's about character set.

before you can actually print out something you have to choose the correct character set.
Which is unicode in your case?
Topic archived. No new replies allowed.