Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사]
조회: 11174
글쓴 사람
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)
5127c#2/20/20196885책에 예제 문의드립니다. [2]
5125게스트2/19/20196970delegate를 활용한 event 를 적절히 불러오고 싶습니다. [2]
5124정근화2/12/20196850윈도우 서버2003 환경 오류 [2]
5123김주현2/8/20196912MS LUIS 에 대한 소개 하실 계획이 있으신가요? [1]
5122jaka...2/1/20198770Clickonce 배포 후 Command 실행 [2]파일 다운로드1
5121엔벌이1/31/20198233C# DataGridView의 MDB파일 함수? ArrayList? [1]파일 다운로드1
5120임우진1/30/20198733웹에서 응용프로그램 바로 실행하기 관련 브라우저에서 파라미터가 넘어오지 않습니다.ㅜㅜ [2]
5119guest1/29/201910122교재에 오탈자 있어 알려드리려 합니다 [1]
5118WPF꿈...1/26/20198061GetHashCode 메서드에 대해서 [1]
5117하주형1/25/20198700List<int>에 대한 이해가 잘안됩니다. [5]
5116게스트1/24/20197471asp.net 관련 gridview webform 질문 드립니다. [1]파일 다운로드1
5115Soul...1/24/20197636투명 패널 질문드립니다. [2]
5114박현일1/20/20197987WPF DataContext 관련 초보 질문을 드려봅니다.^^ [5]
5113하주형1/20/20197243안녕하세요 시작하세요 C# 인코딩관련 질문드립니다. [1]
5112손성배1/19/201916214안녕하세요 cp949 인스톨시 오류입니다... 너무 힘들어요 [5]
5111게스트1/10/20197869암호화 라이센스 관련 문의 드립니다. [1]
5110WPF꿈...1/9/20197511Thread Abort 함수 사용시 [2]
5109닷넷개발1/9/20197480thread 관련 질문 예제.. [2]파일 다운로드1
5108닷넷개발1/9/20198628thread 관련 질문 드립니다.. [4]
5107우코아1/4/201911224WPF에서 로딩중 이미지를 구현 - Project [5]파일 다운로드1
5106우코아1/3/20198877WPF에서 로딩중 이미지를 구현 - Source [1]
5104우코아1/1/201910141WPF에서 로딩중 이미지를 구현 [4]
5103이혜성12/31/20181072532bit .net 으로 만들어진 dll파일 [5]
5102돌고래12/18/20189491자료구조와 알고리즘 도서 관련 질문입니다. [4]
5101세퉁12/17/20187814안녕하세요 wpf 공부중인데 질문있습니다. [4]파일 다운로드1
5100돌고래12/16/20187180도서 추천 부탁드립니다. [1]
... 16  17  18  19  20  21  22  23  24  25  26  27  28  29  [30]  ...