2011-09-06 160 views
0

對於這個問題(完全新手),我很抱歉,USPS網站提供以下函數用於生成條形碼。文檔顯示此: USPS_MSB_Math_CRC11GenerateFrameCheckSequence(016907B2A24ABC16A2E5C004B1(HEX))= 751(HEX)將字節數組傳遞給函數

我試過很多事情(經過十六進制數,前面puting 0X,通過二進制),但我能得到正確的結果,怎麼樣我是否願意將十六進制值傳遞給函數?任何想法/建議/更正請...非常感謝你!

/*Inputs: ByteArrayPtr is the address of a 13 byte array holding 102 bits which 
are right justified - ie: the leftmost 2 bits of the first byte do not 
hold data and must be set to zero. 

Outputs: Outputs:return unsigned short - 11 bit Frame Check Sequence (right justified) 
*/ 

extern unsigned short 
USPS_MSB_Math_CRC11GenerateFrameCheckSequence(unsigned char *ByteArrayPtr) 
{ 
unsigned short GeneratorPolynomial = 0x0F35; 
unsigned short FrameCheckSequence = 0x07FF; 
unsigned short Data; 
int    ByteIndex,Bit; 

/* Do most significant byte skipping the 2 most significant bits */ 
Data = *ByteArrayPtr << 5; 
ByteArrayPtr++; 

for (Bit = 2; Bit < 8; Bit++) { 
    if ((FrameCheckSequence^Data) & 0x400) 
     FrameCheckSequence = (FrameCheckSequence << 1)^GeneratorPolynomial; 
    else 
     FrameCheckSequence = (FrameCheckSequence << 1); 
    FrameCheckSequence &= 0x7FF; 
    Data <<= 1; 
} 

/* Do rest of the bytes */ 
for (ByteIndex = 1; ByteIndex < 13; ByteIndex++) { 
    Data = *ByteArrayPtr << 3; 
    ByteArrayPtr++; 
    for (Bit = 0; Bit < 8; Bit++) { 
     if ((FrameCheckSequence^Data) & 0x0400) 
      FrameCheckSequence = (FrameCheckSequence << 1)^GeneratorPolynomial; 
     else 
      FrameCheckSequence = (FrameCheckSequence << 1); 

     FrameCheckSequence &= 0x7FF; 
     Data <<= 1; 
    } 
} 

return FrameCheckSequence; 
} 

回答

4

你應該創建一個數組,然後傳入 - 就像頂部的註釋那樣。

對於示例:

unsigned char array[] = { 0x01, 0x69, 0x07, 0xB2, 0xA2, 0x4A, 0xBC, 
          0x16, 0xA2, 0xE5, 0xC0, 0x04, 0xB1 
         }; 
unsigned short myCRC = USPS_MSB_Math_CRC11GenerateFrameCheckSequence(array);