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

... 16  17  18  [19]  20  21  22  23  24  25  26  27  28  29  30  ...
NoWriterDateCnt.TitleFile(s)
5452예지1/15/20216566var를 사용할 수 없는 이유가 궁금합니다! [3]
5451예지1/14/20215388for문의 초기식에 대해 질문드립니다. [3]
5450예지1/13/20215434Action 델리게이트 사용법 질문있습니다! [2]
5449김성민1/13/20216121Winform UserControl 상속 vs 감싸기? [2]
5448서형주1/13/20215863안녕하세요~~ DataGridView에 데이터를 표시하는 동작방법이 궁금합니다. [2]
5447종범1/11/20217517[WPF/OpenCV] 이미지->영상 저장에 대해서 질문 드립니다!! [5]파일 다운로드1
5446민우1/11/20215515닷넷 런타임을 dll 파일로 포함시킬수 있나요? [2]
5445정도현1/8/20215291directShow RenderFile 관련 재질문드립니다 [5]파일 다운로드1
5444정도현1/8/20215261directShow RenderFile 관련 질문드립니다 [3]
5443윤영호1/7/20215637xml 파일에서 데이터를 가지고 와서 list에 넣는 것을 질문드리고 싶습니다. [1]파일 다운로드1
5442진우1/4/20215445DB연결 객체나 파일 등은 GC 에서 관리해주지 않는 이유가 궁금합니다. [2]
5441한예지 donator1/4/20215869DB 연결 방법 질문 있습니다. [1]
5440한예지 donator1/1/20216490추상클래스로와 new [4]
5439이상호12/31/20208023VC 프로젝트 에서 _main 함수에서 참조되는 확인할 수 없는 외부 기호 [4]파일 다운로드1
5438김윤12/29/202010150C# winform using으로인한 메모리 해제 타이밍과 변수 복사 타이밍 [2]
5437한예지 donator12/25/20207224for문 안에 있는 지역변수의 생성 및 유지 기간에 대해 질문드립니다! [6]
5436영귤12/24/20205953fixed는 자동으로 stackalloc이 되는 건가요? [1]
5435한예지 donator12/24/20205776ArraySegment, Span, ReadOnlySpan 질문있습니다! [1]
5434한예지 donator12/23/20206335ToString 재정의 질문있습니다! [8]
5433한예지 donator12/23/20206311List<ArrarySegment<int>> 사용법 질문드립니다! [2]
5431한예지 donator12/17/20208126비동기 소켓 서버 질문 드립니다! [1]
5430종범12/16/20207277[WPF] Task 관련 재질문 드립니다. [2]파일 다운로드1
5429종범12/16/20207208[WPF] Task 관련 질문 드립니다. [1]파일 다운로드1
5428최선호 donator12/14/20206795성태님 ㅠ.ㅠ 소켓 서버 작동이 이상합니다 ㅠ.ㅜ [1]파일 다운로드1
542712/11/20207400LPBOOL Win32 마샬링 질문이 있습니다. [6]
5426하태12/11/20205662안녕하세요 정성태님! C# winform Calendar 컨트롤 질문 드리겠습니다!. [1]
... 16  17  18  [19]  20  21  22  23  24  25  26  27  28  29  30  ...