Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사]
조회: 11236
글쓴 사람
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)
5290베도빈2/15/20207238직접 제작한 사용자 정의 콘트롤 DLL, 실행파일에 포함시켜 배포할 수 있을까요? [1]
5288김성배2/14/20207659c# 참조추가 기능에서 DirectX 참조가 보이지 않아요.. DircetX 11 은 설치 되어 있습니다. [1]
5287C# 초...2/14/20206667프로그램 실행 질문 드립니다. [4]파일 다운로드1
5286gongs2/3/202014053윈도우 배율 및 레이아웃에 대한 질문입니다. [3]
5285강성욱1/27/20207770작업관리자에 표시되는 스레드 개수와 프로그램 내 스레드 개수 일치 [1]
5284이상준1/15/202013488출력창에 '스레드가 종료되었습니다' 문구 문의 [2]파일 다운로드1
5282봉봉이 1/13/202010251소켓 비동기 ReceiveTimeOut 구현 [9]파일 다운로드1
5281봉봉이 1/13/20208286소켓 비동기 ReceiveTimeOut 구현 [2]
5280sdd1/8/20206855데이터 저장, 배치파일 관련하여 질문드립니다. [4]
5279김창한12/27/20197821dsoframer ocx [3]
5278guest12/27/20196599CLR 스레드 풀 관련 질문 [1]
5277방문자12/26/20196427WindowsForm 질문드립니다. [3]
5276진우12/17/20198182c++/c# 에도 파이썬 가상환경 같은 기능 혹시 있는지 궁금합니다. [3]
5275송재필12/17/20197889아래 'c#8.0 프로그래밍 교제 예제 관련 문의 드립니다' 의 원인이 압축 파일의 문제인듯 합니다. [2]파일 다운로드1
5272정영훈12/13/20198056byte => string => byte 변환 문의드립니다. [5]파일 다운로드1
5270김세미12/3/20197805c#8.0 프로그래밍 교제 예제 관련 문의 드립니다. [4]
5269달려라라...12/2/20197939C# winform(framework) tabcontrol 에서 button 함수 관련 질문 [3]파일 다운로드1
5266엔벌잉11/26/20196881라즈베리파이 autostart관련 질문 [2]
5265해나11/25/20196970C# 6.0 책 내용 문의 [1]
5264김혜원11/18/20199409C# CodeSign에 대해 여쭤볼 것이 있습니다 [4]
5263정환맨11/17/20197554Task관련 질문입니다 [3]
5262닷린이11/14/20198959Dispatcher 와 synchronizationcontext의 관계가 궁금합니다. [6]
5260방문자11/13/20197919안녕하세요 도움받고 싶습니다 [1]
5259ming11/13/20197005string타입 파라메터로 넘길때 메모리 내부가 궁금합니다. [3]
5258C#초보11/13/20198786DCOM COM+ 등록 관련 문의드립니다. [7]
5257C#초보11/9/20197423파일 저장 방법에 대해 조언 부탁드립니다. [3]
... 16  17  18  19  20  21  22  23  24  [25]  26  27  28  29  30  ...