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

... 31  [32]  33  34  35  36  37  38  39  40  41  42  43  44  45  ...
NoWriterDateCnt.TitleFile(s)
5070진우10/17/20188303Visual Studio 서비스팩과 업데이트 차이 문의 [2]
5069감자10/12/20189040빌드 구성을 재설정하는 방법이 있을까요? [1]파일 다운로드1
5068누오10/10/20187820ASP Core 2.0 에서 dll안에 있는 뷰 읽어들이는 방법? [1]
5067김정민10/5/20187811다른 윈도우가 깨지는 현상을 막을 수 있을까요 [3]
5066로니브10/4/20188596ASP.NET MVC에서 View 파일 숨기는법? 보안처리 하는법? 관련 질문.. [1]
5065키모10/1/20189798문자 질문입니다. [3]
5064로니브10/1/20189131클래스 라이브러리에서 .cshtml파일을 추가하는 방법은 없나요? [3]
5063진우9/28/20188150ADO.net 과 Entity Framework 차이 문의 [2]
5062테스트9/27/20188687C# import file 의 구조체 배열 선언 및 호출에 대해 문의. [3]
5061안녕하세요9/13/20188678c# 프로그래밍 관련 문의 [1]
5060임민재9/8/20188156c# install 파일 생성 시 문제가 발생하였습니다 [1]파일 다운로드1
50599/7/20187845Winform TextBox 포커스 유지하는 방법 질문 [파일첨부] [1]파일 다운로드1
50589/5/201811256Winform TextBox 포커스 유지하는 방법 질문 [3]
5056박종윤8/30/201810592c# dll을 C++에서 사용 시 event 호출 [4]파일 다운로드1
5055초보자8/29/20189222asp.net 에서 다른 서버의 iis를 stop하는 batch file을 실행시키는데 동작하지 않습니다. [5]
5054사도신8/29/20187928[wpf] textbox insert overite 모드시에 [4]파일 다운로드1
5053엿장수8/26/20187297directshow filter 에서의 IMediaSample 의 시간에대한질문입니다 [1]
5052오명현8/26/20188181Tcp소켓 실습 Exeption 도와주세요! [4]파일 다운로드1
5049오명현8/23/20187552책 477페이지 내용 중 이해가 안가는 부분이 있어 질문드립니다. [1]
5048오명현8/23/20187249포트 관련 질문 하나더 있습니다. [1]
5047오명현8/22/20187916포트가 없을 경우를 가정한 내용에 대해 질문이 있습니다. 책468p. [1]
5046엿장수8/22/20187797다이렉트쇼 필터 추가하는데 [2]
5045임도진8/22/20188513c# opencv dll파일 로드 질문 [3]파일 다운로드1
5044엿장수8/20/20188217graphedit 에 등록되어있는 필터를 가져와서 사용하는방법을 알고싶습니다 [2]
5043hori...8/20/20187835Expression에 대한 책의 예제 관련하여 질문드립니다. [1]
5042황윤하8/20/20189567모드버스 TCP 관련 질문 [3]
... 31  [32]  33  34  35  36  37  38  39  40  41  42  43  44  45  ...