Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사],
조회: 18499
글쓴 사람
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)
5134이정호3/12/201913920SampleProfiler 예제를 이용한 Class 객체의 접근 문의 [1]파일 다운로드1
5133ㅇㅇ3/12/201915081멀티코어사용 방법에 궁금한것이 있습니다. [3]
5132존기미3/3/201915338Physical Disk Access문의 [1]
5131강용규2/25/201914777c# 개발자입니다. 타이머 질문드립니다. [1]
5130노인코래방2/25/201919025C#에서 가장 좋은 성능을 보이는 파일 읽고 쓰는 방법이 무엇인가요? [2]
5129진우2/23/201918577닷넷 32비트 기반에서 메모리 부족으로 프로그램이 죽는경우 문의 [2]
5128게스트2/23/201915868안녕하세요. 초보개발자입니다. [3]파일 다운로드1
5127c#2/20/201914746책에 예제 문의드립니다. [2]
5125게스트2/19/201914633delegate를 활용한 event 를 적절히 불러오고 싶습니다. [2]
5124정근화2/12/201914520윈도우 서버2003 환경 오류 [2]
5123김주현2/8/201914558MS LUIS 에 대한 소개 하실 계획이 있으신가요? [1]
5122jaka...2/1/201916924Clickonce 배포 후 Command 실행 [2]파일 다운로드1
5121엔벌이1/31/201916407C# DataGridView의 MDB파일 함수? ArrayList? [1]파일 다운로드1
5120임우진1/30/201917389웹에서 응용프로그램 바로 실행하기 관련 브라우저에서 파라미터가 넘어오지 않습니다.ㅜㅜ [2]
5119guest1/29/201919050교재에 오탈자 있어 알려드리려 합니다 [1]
5118WPF꿈...1/26/201917273GetHashCode 메서드에 대해서 [1]
5117하주형1/25/201917080List<int>에 대한 이해가 잘안됩니다. [5]
5116게스트1/24/201915271asp.net 관련 gridview webform 질문 드립니다. [1]파일 다운로드1
5115Soul...1/24/201915699투명 패널 질문드립니다. [2]
5114박현일1/20/201916267WPF DataContext 관련 초보 질문을 드려봅니다.^^ [5]
5113하주형1/20/201915522안녕하세요 시작하세요 C# 인코딩관련 질문드립니다. [1]
5112손성배1/19/201924806안녕하세요 cp949 인스톨시 오류입니다... 너무 힘들어요 [5]
5111게스트1/10/201916056암호화 라이센스 관련 문의 드립니다. [1]
5110WPF꿈...1/9/201915509Thread Abort 함수 사용시 [2]
5109닷넷개발1/9/201915151thread 관련 질문 예제.. [2]파일 다운로드1
5108닷넷개발1/9/201916334thread 관련 질문 드립니다.. [4]
... [31]  32  33  34  35  36  37  38  39  40  41  42  43  44  45  ...