Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사],
조회: 17549
글쓴 사람
KDP (secpkd at gmail.com)
홈페이지
첨부 파일
 

하기 코드 c#으로 구현하고자 합니다. 혹시 컨버팅 해주실수 있는지요~

#include<stdio.h>
#include<stdint.h>

#define CRC16 0x8005

uint16_t gen_crc16(const uint8_t *data, uint16_t size)
{
    uint16_t out = 0;
    int bits_read = 0, bit_flag;


    // test
    printf("buffer in function %s\n", data);

    /* Sanity check: */
    if(data == NULL)
        return 0;

    while(size > 0)
    {
        bit_flag = out >> 15;

        /* Get next bit: */
        out <<= 1;
        out |= (*data >> bits_read) & 1; // item a) work from the least significant bits

        /* Increment bit counter: */
        bits_read++;
        if(bits_read > 7)
        {
            bits_read = 0;
            data++;
            size--;
        }

        /* Cycle check: */
        if(bit_flag)
            out ^= CRC16;

    }

    // item b) "push out" the last 16 bits
    int i;
    for (i = 0; i < 16; ++i) {
        bit_flag = out >> 15;
        out <<= 1;
        if(bit_flag)
            out ^= CRC16;
    }

    // item c) reverse the bits
    uint16_t crc = 0;
    i = 0x8000;
    int j = 0x0001;
    for (; i != 0; i >>=1, j <<= 1) {
        if (i & out) crc |= j;
    }

    return crc;
}

int main()
{
    char buf[]="123456789";
    int c , r;
    printf ("the buf has %s", buf);
    r = gen_crc16(buf,sizeof(buf)-1);
    printf("%04hx\n", r);

    return (0);
}








[최초 등록일: ]
[최종 수정일: 5/29/2017]


비밀번호

댓글 작성자
 



2017-05-29 10시19분
스스로 해보시면 어떨까요? ^^
정성태
2017-05-29 10시39분
[KDP] const UInt16 CRC16 = 0x8005;

        UInt16 gen_crc16(byte[] data, UInt16 size)
        {
            UInt16 _out = 0;
            int bits_read = 0;
            int bit_flag = 0;
            int index = 0;

            // test
            Console.WriteLine("buffer in function {0}\n", data.ToString());

            /* Sanity check: */
            if(data == null)
                return (UInt16)0;

            while(size > 0)
            {
                bit_flag = _out >> 15;

                /* Get next bit: */
                _out <<= 1;
                _out |= (UInt16)((data[index] >> bits_read) & 1); //`enter code here`

                /* Increment bit counter: */
                bits_read++;
                if(bits_read > 7)
                {
                    bits_read = 0;
                    index++;
                    size--;
                }

                /* Cycle check: */
                if(bit_flag > 0)
                    _out ^= CRC16;

            }

            // item b) "push out" the last 16 bits
            UInt16 i;
            for (i = 0; i < 16; ++i) {
                bit_flag = _out >> 15;
                _out <<= 1;
                if(bit_flag > 0)
                    _out ^= CRC16;
            }

            // item c) reverse the bits
            UInt16 crc = 0;
            i = 0x8000;
            UInt16 j = 0x0001;
            for (; i != 0; i >>=1, j <<= 1) {
                if ((i & _out) > 0) crc |= j;
            }

            return crc;
        }
[guest]

... 91  92  93  [94]  95  96  97 
NoWriterDateCnt.TitleFile(s)
111정성태3/19/20059739    답변글 [답변]: 닷넷 프레임워크 배포
107안연준3/17/20058977[-_-]스마트 클라이언트에 관련 된 질문[-_-]
110정성태3/19/20059827    답변글 [답변]: [-_-]스마트 클라이언트에 관련 된 질문[-_-]
116안연준3/21/20059379        답변글 [답변]: 친절한 답변 고맙습니다.
98김용국3/18/200511151정성태님... 연결이 준비됬습니다
99정성태3/2/200512021    답변글 [답변]: 정성태님... 연결이 준비됬습니다
100김용국3/18/20058855        답변글 [답변]: 죄송합니다! 급히올리느라 file path의 변경을 하지않고 그냥올렸네요....!
101정성태3/2/20059105            답변글 [답변]: [답변]: 죄송합니다! 급히올리느라 file path의 변경을 하지않고 그냥올렸네요....!
102김용국3/18/20059174                답변글 [답변]: 거듭죄송하네요..... 후~~~주소를 변경하고 빌드를 다시 했습니다....
103정성태3/2/20059034                    답변글 [답변]: [답변]: 거듭죄송하네요..... 후~~~주소를 변경하고 빌드를 다시 했습니다....
104김용국3/2/20059279                        답변글 [답변]: 네... 빨리 검토해 봐 주셔서 감사합니다~~
105정성태3/2/20058505                            답변글 [답변]: [답변]: 네... 빨리 검토해 봐 주셔서 감사합니다~~
106김용국3/2/20058987                                답변글 [답변]: 답변감사합니다!
91김용국2/28/200510816IE에 WindowsFormControl을 올려 실행하면 이런에러가 나네요???파일 다운로드1
92정성태2/28/200512109    답변글 [답변]: IE에 WindowsFormControl을 올려 실행하면 이런에러가 나네요???
93김용국2/28/200511047        답변글 [답변]: 답변감사합니다... 재질문을 드립니다
94정성태2/28/200511639            답변글 [답변]: [답변]: 답변감사합니다... 재질문을 드립니다
95김용국2/28/200511702                답변글 [답변]: IE주소창에서 해당주소로 실행을 해보니....디버깅 PopUp화면이...^
96정성태2/28/200510785                    답변글 [답변]: [답변]: IE주소창에서 해당주소로 실행을 해보니....디버깅 PopUp화면이...^
97김용국3/1/200511291                        답변글 [답변]: 준비되는데로 말씀드리겠습니다 ^^
88안지환2/22/200512823^^ 사이트 잘 들러보았습니다.
89정성태2/22/200512806    답변글 [답변]: ^^ 사이트 잘 들러보았습니다.
85한기열2/22/200511432정성태님 홈같은 부드러운 페이지 넘김?은 어떻게 구현하나요?
86정성태2/22/200512118    답변글 [답변]: 정성태님 홈같은 부드러운 페이지 넘김?은 어떻게 구현하나요? [2]
84김용국2/21/200512213Smart Client에 관한 문의 드립니다.
87정성태2/22/200512268    답변글 [답변]: Smart Client에 관한 문의 드립니다.
... 91  92  93  [94]  95  96  97