Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사],
조회: 18502
글쓴 사람
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)
5434한예지 donator12/23/202014885ToString 재정의 질문있습니다! [8]
5433한예지 donator12/23/202015050List<ArrarySegment<int>> 사용법 질문드립니다! [2]
5431한예지 donator12/17/202016258비동기 소켓 서버 질문 드립니다! [1]
5430종범12/16/202015205[WPF] Task 관련 재질문 드립니다. [2]파일 다운로드1
5429종범12/16/202015779[WPF] Task 관련 질문 드립니다. [1]파일 다운로드1
5428최선호 donator12/14/202015584성태님 ㅠ.ㅠ 소켓 서버 작동이 이상합니다 ㅠ.ㅜ [1]파일 다운로드1
542712/11/202016210LPBOOL Win32 마샬링 질문이 있습니다. [6]
5426하태12/11/202013832안녕하세요 정성태님! C# winform Calendar 컨트롤 질문 드리겠습니다!. [1]
5425정유경12/10/202014187[왕초보] (4) [1]
5423정유경12/8/202014274[왕초보] (3) [2]
5422이승준12/7/202019584VisualStudio 2019의 인텔리센스가 느려지는 경우가 있나요? [4]
5421정유경12/6/202016090[왕초보] (2) [4]
5420정유경12/5/202016102[왕초보] 랜덤 숫자와 배열에 관한 질문 [1]
5419종범12/4/202016357[WPF] Threadpool사용 시 크리티컬 섹션 대응 질문 입니다. [4]
5418한예지 donator11/27/202016872클래스, 인터페이스 크기를 구하고 싶은데 어떻게 해야 될까요? [1]
5417한예지 donator11/27/202014562인터페이스와 추상클래스에 대해 궁금증이 있습니다. [1]
5416한예지 donator11/27/202014451Object 질문 있어요. [1]
5415한예지 donator11/25/202014031교재 213쪽 예제 4.25 질문드립니다. [1]
5414한예지 donator11/23/202015439제네릭 리스트 출력하는 방법이 궁금합니다. [1]
5413민석11/20/202016474C# minidump를 프로그램이 중단 될 때 만들고 싶습니다. [1]파일 다운로드2
5411원격11/20/202014431visualstdio로 웹 사이트로 만들었을때 원격 디버깅이 가능한가요? [1]
5410최성재11/16/202014892vcpkg로 GDCM 내려받을 때 USE_VTK 설정하는 방법-2번째 질문 [1]파일 다운로드1
5409민성11/16/202018093혹시 다른 질문이긴 한데요 [1]
5408최성재11/16/202014706vcpkg로 GDCM 내려받을 때 USE_VTK 설정하는 방법 [1]
5407민성11/11/202013375안녕하세요 yield return에 대해서 [1]
5406질문자11/10/202014376안녕하세요 wcf nettcpbinding의 timeout에 관해서 질문이 있습니다. [2]
... 16  17  18  19  20  [21]  22  23  24  25  26  27  28  29  30  ...