Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사],
조회: 11257
글쓴 사람
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]

... 46  47  48  49  50  51  52  53  54  55  [56]  57  58  59  60  ...
NoWriterDateCnt.TitleFile(s)
1238(non...3/13/201411952(글쓴이의 요청으로 삭제합니다.) [2]
1237(non...3/11/201412070(글쓴이의 요청으로 삭제합니다.) [2]
1236(non...3/11/201412962(글쓴이의 요청으로 삭제합니다.) [2]
1235(non...3/10/201412083(글쓴이의 요청으로 삭제합니다.) [2]
1234(non...3/10/201414099(글쓴이의 요청으로 삭제합니다.) [3]
1233(non...3/9/201412234(글쓴이의 요청으로 삭제합니다.) [4]
1232(non...3/8/201411325(글쓴이의 요청으로 삭제합니다.) [2]
1231(non...3/7/201412619(글쓴이의 요청으로 삭제합니다.) [9]
1230POCO3/7/201412968쓰레드 안에서 DependencyProperty get, set시 또 다른 스레드 오류.. [1]
1229(non...3/6/201412705(글쓴이의 요청으로 삭제합니다.) [11]
1228POCO3/6/201411235안녕하세요. 질문이 있습니다. [1]
1226김형진3/4/201419780안녕하세요 windows azure에 관해 질문했던 사람입니다. [2]
1224(non...3/3/201415899(글쓴이의 요청으로 삭제합니다.) [11]
1223sadf...3/3/201411515아래 질문에 답변 감사드립니다. 한가지 더 궁금한점이 있어 질문드립니다. [1]
1222(non...3/2/201412351(글쓴이의 요청으로 삭제합니다.) [4]
1221(non...3/1/201412392(글쓴이의 요청으로 삭제합니다.) [2]
1220Until2/28/201410936질문드립니다. [1]
1219이성환2/28/201410595string.Join()과 Enumerable.Aggregate()의 차이가 궁금합니다. [2]파일 다운로드1
1218김형진2/25/201412048안녕하세요. window azure에 대해서 질문이 있어서 문의 드립니다 [4]
1217(non...2/23/201412514(글쓴이의 요청으로 삭제합니다.) [1]
1215아리수2/20/201416061C# 공부하면서 WPF에 대한 질문. [2]
1214조광훈2/20/201414289IIS8 응용프로그램 풀 관련 질문 드립니다. [2]파일 다운로드1
1213김태훈2/17/201411181가상화 프로그램 질문입니다. [1]파일 다운로드1
1212조광훈2/13/201410335ISAPI 필터에서 커스텀 헤더 정보 추가 [1]파일 다운로드1
1211조광훈2/12/201413766isapi 필터 로드 오류 [2]
1208박지호2/9/201417096[오타] 시작하세요 C# 프로그래밍 p.267 ~ 350 [1]
... 46  47  48  49  50  51  52  53  54  55  [56]  57  58  59  60  ...