Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사]
조회: 11118
글쓴 사람
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)
5425정유경12/10/20205665[왕초보] (4) [1]
5423정유경12/8/20206025[왕초보] (3) [2]
5422이승준12/7/20209449VisualStudio 2019의 인텔리센스가 느려지는 경우가 있나요? [4]
5421정유경12/6/20207098[왕초보] (2) [4]
5420정유경12/5/20206620[왕초보] 랜덤 숫자와 배열에 관한 질문 [1]
5419종범12/4/20207051[WPF] Threadpool사용 시 크리티컬 섹션 대응 질문 입니다. [4]
5418한예지 donator11/27/20207283클래스, 인터페이스 크기를 구하고 싶은데 어떻게 해야 될까요? [1]
5417한예지 donator11/27/20205954인터페이스와 추상클래스에 대해 궁금증이 있습니다. [1]
5416한예지 donator11/27/20205790Object 질문 있어요. [1]
5415한예지 donator11/25/20205887교재 213쪽 예제 4.25 질문드립니다. [1]
5414한예지 donator11/23/20206759제네릭 리스트 출력하는 방법이 궁금합니다. [1]
5413민석11/20/20207290C# minidump를 프로그램이 중단 될 때 만들고 싶습니다. [1]파일 다운로드2
5411원격11/20/20205813visualstdio로 웹 사이트로 만들었을때 원격 디버깅이 가능한가요? [1]
5410최성재11/16/20206171vcpkg로 GDCM 내려받을 때 USE_VTK 설정하는 방법-2번째 질문 [1]파일 다운로드1
5409민성11/16/20209194혹시 다른 질문이긴 한데요 [1]
5408최성재11/16/20206454vcpkg로 GDCM 내려받을 때 USE_VTK 설정하는 방법 [1]
5407민성11/11/20205787안녕하세요 yield return에 대해서 [1]
5406질문자11/10/20206241안녕하세요 wcf nettcpbinding의 timeout에 관해서 질문이 있습니다. [2]
5405민성11/9/20206979안녕하세요 이번에도 또 어려운 질문 같습니다. [1]
5404박진우11/6/20207334안녕하세요. SqlParameter 생성자 관련 질문 있습니다. [1]
5403민성11/5/20207410그리고 한가지만 죄송하지만 더 질문 드리겠습니다. [1]
5402민성11/5/20207608안녕하세요 책을 보고 질문하나만 드릴깨요 [2]
5401민성11/3/20206680안녕하세요 이번에도 질문 하나만 드리겠습니다. [2]
5400진우10/29/20206362SQL Server 관련 몇가지 문의 [2]
5399Wpf개...10/21/20206565Binding 된 항목의 갱신 시 간헐적 끊어짐 발생 문제. [2]
5397나그네10/15/20206063.net Core 3.1 에서 Entity Framework 와 ADO.NET 선택에 관해 여쭤봅니다. [2]
... 16  17  18  19  [20]  21  22  23  24  25  26  27  28  29  30  ...