Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사]
조회: 11079
글쓴 사람
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)
4745힘찬도약7/27/201612034.NET 자식창 데이터를 부모창에 전달시 오류 질문드립니다. [4]파일 다운로드1
4744변찬연7/27/201611769안녕하세요 그 edge를 이용하는 데 조금 불편함이 있어서 문의드립니다 [1]
4743딸랑구아빠7/26/201610662IE 사용 시 인증 정보 계속 보내기? [1]
4741차가워7/20/201612243UWP 에서 COM 참조 불가능한가요? [6]
4740luna...7/19/201612654clickonce manifest 파일 질문 [1]
4739윤똘씨7/19/201612112정말 감동적입니다... [7]
4738닷넷초보7/7/201610999안녕하세요. C#의 개체 전달방식(참조)에 관련해서 질문 있습니다. [3]파일 다운로드1
4737beau...7/6/201613330python embedding 한 c++ 프로그램의 배포에 관해서 질문드리고 싶습니다. [1]
4736spow...7/6/201610195Property Lambda에 대한 심층 분석을 의뢰합니다 [7]
4735힘찬도약7/5/201611338.Net/Web.config에서 MSSQL을 쓰는데, MysqlSiteMap 참조오류 [3]파일 다운로드1
4734spow...6/28/201610913Visual Studio 2015 + Windows Forms 환경에서 컴파일 할 때 드물게 개발환경이 Crash 됩니다 [1]
4733feel...6/15/201611377타 서버로 소스 이전 중 [3]파일 다운로드1
4732구봉근6/14/201610129안녕하세요 [1]파일 다운로드1
4731초급개발자6/7/201612667Windows Form Application는 사라질까요? [2]
4730김시현6/4/201610735책으로 공부하던 도중 생성자에 대해서 궁금한게 생겼는데 혹시 답변좀 해주실수 있나요? [1]
4729Jeah...6/3/201612190C#.Net 프로그램에서 C++로 만들어진 dll 파일 이용관련 문의 하나 드립니다. [3]
4727링거5/30/201610836WPF MediaElement 파일 실행 후 삭제시 오류 문의 [1]파일 다운로드1
4728링거5/30/201611210    답변글 [답변]: WPF MediaElement 파일 실행 후 삭제시 오류 문의 [2]파일 다운로드1
4726spow...5/25/201610654덧글을 달고 나서 F5를 눌렀을 때 똑같은 덧글이 달리는 현상 고쳐주세요 [4]
4725Futu...5/23/201611117책 잘보고 있습니다. Task관련 질문을!!! [1]
4724학생15/23/201612398웹소켓과 소켓 관련해서 질문 드립니다. [2]
4722지나가던...5/15/201614223Part 3 pdf파일 그림 재수정 문의 [1]
4723지나가던...5/16/201611475    답변글 [답변]: Part 3 pdf파일 그림 재수정 문의 [1]
4721김종희4/30/2016116333D 시뮬레이션 개발 [1]
4720DC4/29/201611030C# 6.0 델리게이트 인스턴스화 관련 질문~ [2]
4719이민구4/29/201611972[시작하세요! C# 프로그래밍] 4.5.1.6 열거형 마지막 예제 질문입니다. [4]파일 다운로드1
... 31  32  33  34  35  36  37  38  39  40  41  42  43  [44]  45  ...