Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사]
조회: 11134
글쓴 사람
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)
5509한예지 donator6/3/20215764ExeccuteNonQuery 질문있습니다! [2]
5508최재남6/3/20216598마우스 이벤트 관련 질문 좀 드리겠습니다. [6]
5507한예6/2/20215760static과 스택오버플로우 질문있습니다. [5]파일 다운로드1
5506이창석6/1/20217171센서의 값을 받아서 PC를 통해 모니터링 하는 것을 만들고 있습니다. [1]
5505kss5/31/20215742.net5에서 소멸자가 어떻게 바뀐건가요? [1]
5503xing...5/24/20216124xingapinet 에 수정 요청 부탁 드립니다. [1]
5501한예지 donator5/23/20215896IProgress 사용법이 궁금합니다. [2]
5500한예ㅈ5/23/20217779비동기 코드 흐름 질문있습니다. [3]
5498곰장어5/21/20215644List에 static 변수를 추가했을때의 궁금증 [3]파일 다운로드1
5497지평선5/20/20216888윈도우 배율을 알 수 있을까요? [1]
5496cs린이5/20/20215525C# 8.0 질문입니다. [2]파일 다운로드6
5495Natie5/13/20215049객체를 생성과 동시에 초기화 하는 방법 [1]
5494지예예지5/13/20217321비동기 코드 흐름이 궁금합니다! [2]
5493xing...5/6/20215225xing api XQCSPAT00600 질문입니다 [4]파일 다운로드1
5492한예지 donator5/5/20215306FromCurrentSynchronizationContext 관련 코드 질문있습니다! [2]
5491조우성5/4/20219741WinForm과 WPF의 성능차이 비교한 자료가 있을까요? [4]
5490한예지 donator5/3/20216107UI 스레드의 Invoke 질문있습니다. [4]
5489저누4/28/20216671시작하세요 C# 9.0, 225페이지 구조체 관련 질문드립니다. [2]
5488종범4/27/20216914C# 비동기 함수 async, await 와 Task의 관계에 대해 질문 드립니다. [6]
5487진우4/27/20216292C# 엑셀 자동화 성능 향상 문의 [2]
5486지나가던...4/26/20215844닷넷 구현 코드 관련 질문 [2]
5485이재원4/17/20216225교재 315페이지 내용 질문 [3]
5484Syong4/16/20219756윈폼 기반의 응용프로그램 dll 참조와 32,64bit 빌드 관련 문의 [4]
5483한예지 donator4/15/20215279익명 형식과 var 관계 질문 있습니다. [2]
5482질문4/13/20217066WPF를 위한 MVVM toolkit 선택과 관련한 문의드립니다. [4]
5480한예지 donator4/5/20217362GetHashCode 질문있습니다! [2]
... 16  [17]  18  19  20  21  22  23  24  25  26  27  28  29  30  ...