dev.gamez.lv Forum Index dev.gamez.lv
Latvian Game Developers Community
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups 

Binary Buffer

 
dev.gamez.lv Forum Index -> Programmēšana
View previous topic :: View next topic  
Author Message
Tenjou



Joined: 22 Nov 2005
Posts: 275

PostPosted: Thu Jan 11, 2007 12:09 am    Post subject: Binary Buffer

Ideja ir tada, kad viena funkcija ieraksta 2byte integer byte-buffer`i[1024] un otra atgriež no buffera abus byte kopa kā 2byte int`u.
Cik saprotu atgriezt var ar bitwise operatora & palīdzību sakombinejot buffer[byteIndex] & [byteIndex+1]?


Code:
typedef unsigned char byte;

byte buffer[1024];
int byteIndex;
...
void binaryBuffer::writeByte(byte value) {
        buffer[byteIndex] = value;
        byteIndex += 1;
}


void binaryBuffer::writeShort(???) {
       ???
}

...

int binaryBuffer::readByte(bool increment) {
   if(increment) {
      byteIndex += 1;
      return buffer[byteIndex-1];
   }
   else {
      return buffer[byteIndex];
   }
}

int binaryBuffer::readShort(bool increment) {
       ???
}
Back to top
View user's profile
bubu
Indago Uzvarētājs
Indago Uzvarētājs


Joined: 23 Mar 2004
Posts: 3223
Location: Riga

PostPosted: Thu Jan 11, 2007 12:47 am    Post subject:

Nu nē, tu pamatīgi kļūdies.
AND jau ir kā reizināšana tb 0&0=0&1=1&0=0, 1&1=1. Tāpēc pazudīs visi tev vieninieku, kur pretī ir 0.
Tev jau vajag saskaitīt - bitwise OR: 0|0=0, 0|1=1|0=1|1=1
(tb shorts = baits1*256 + baits2, tātad baits1*256 | baits2)
Tātad:
readShort() == (byte[x] << 8) | byte[x+1]
vai
readShort() == byte[x] | (byte[x+1] << 8)
atkarībā no tā, kādu endianess gribi lietot (big/little).
Back to top
View user's profile Send e-mail
Vecais_Dumais_Laacis
Guru
Guru


Joined: 29 Jan 2004
Posts: 800

PostPosted: Thu Jan 11, 2007 2:04 am    Post subject:

ja uz vienaada tipa endian boxiem darbojaas tad

void binaryBuffer::writeShort(short data) {
memcpy(buffer+byteIndex, &data, sizeof(short));
byteIndex+=sizeof(short);
}

un taapat readShort
{
short retval;
memcpy(&retval, buffer+byteIndex, sizeof(short));
return retval;
}
_________________
...un ja bites buutu laachi...
Back to top
View user's profile
Tenjou



Joined: 22 Nov 2005
Posts: 275

PostPosted: Thu Jan 11, 2007 6:35 am    Post subject:

Paldies, abiem, baigi izpalīdzējāt Smile!
Back to top
View user's profile
Display posts from previous:   
dev.gamez.lv Forum Index -> Programmēšana All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group