2013-11-21 4 views
0

문자 시퀀스를 3 비트로 압축하는 기능을 사용하고 있습니다. 내 알파벳에는 ATGCN이라는 글자가 들어 있습니다. 테스트 문자열을 입력하고 올바른 값을 가진 응답을 얻고 있지만 예상치 못한 일부 값을 얻고 있습니다. 나는 F 년대 모두가 어디에서 오는지 확실 해요압축 알고리즘이 제대로 작동하지 않습니다.

40ffffffa052 

:

#include <iostream> 
#include <fstream> 
#include <string> 
#include <iomanip> 
using namespace std; 

#define A1 0x00 //0000-0 000 
#define T1 0x01 //0000-0 001 
#define G1 0x02 //0000-0 010 
#define C1 0x03 //0000-0 011 
#define N1 0x04 //0000-0 100 

void bitcompress(int value, int bits, int end_flag); 
int getHex(const char letter); 

int main(int argc, const char * argv[]) 
{ 
    string test = "GATGATGG";//compresses to 0x40a052 with my definitions 
    for (int i=0; i<test.size(); i++) { 
     int val = getHex(test.at(i)); 
     bitcompress(val, 3, 0); 
    } 

    return 0; 
} 

void bitcompress(int value, int bits, int end_flag) 
{ 
    static char data = 0; 
    static int bitsused = 0; 

    int bytesize = 8; 
    int shift = bytesize - bitsused - bits; 

    //cout << "bitsused = " << bitsused << endl; 
    //cout << "shift = " << shift << endl << endl; 

    if(shift >= 0) { 
     data  |= (value << shift); 
     bitsused += bits; 
     if(bitsused == bytesize) { 
      cout << hex << setw(2) << setfill('0') << (int)data; 
      data  = 0; 
      bitsused = 0; 
     } 
    } 

    else { 
     data |= (value >> -shift); 
     cout << hex << setw(2) << setfill('0') << (int)data; 
     data = 0; 
     shift = bytesize + shift; 

     if(shift >= 0) { 
      data |= (value << shift); 
      bitsused = bytesize - shift; 
     } else { 
      data |= (value >> -shift); 
      cout << hex << setw(2) << setfill('0') << (int)data; 
      data  = 0; 
      shift = bytesize + shift; 
      data |= (value << shift); 
      bitsused = bytesize - shift; 
     } 
    } 

    if(end_flag && bitsused != 0) 
     cout << hex << setw(2) << setfill('0') << (int)data; 
} 

int getHex(const char letter) { 
    if (letter == 'A') 
     return (int)A1; 
    else if (letter == 'T') 
     return (int)T1; 
    else if (letter == 'G') 
     return (int)G1; 
    else if (letter == 'C') 
     return (int)C1; 
    else 
     return (int)N1; 
} 

내가 0x40a052을 기대하고 있지만 출력 : 여기 내 코드입니다. if 문 다음에있는 모든 couts을 주석 처리하고 앞의 주석을 주석 처리하지 않으면 시프트 및 비트 사용 값이 올바른지 알 수 있습니다. 그러나 모두 주석 처리되지 않은 경우 "shift"값에 -2 대신 fffffffe가 지정됩니다 (if 문 아래의 couts을 주석 처리하여 볼 수 있음). 문제가 스트림으로 출력하는 것과 관련이있는 것 같지만 확실하지 않습니다. 어떤 도움이라도 대단히 감사하겠습니다!

+0

당신의 정수가 0보다 작지 않다는 것을 안다면'unsigned char'을 사용할 것을 제안합니다. –

답변

1

data의 유형을 char에서 unsigned char으로 변경하십시오. 어떤 점에서 data은 음수 값을 가지므로 인쇄하려면 int으로 캐스팅하면 1로 채워집니다.

+0

내 문제가 해결되었습니다. 감사합니다. –

관련 문제