Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사],
조회: 11261
글쓴 사람
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]

... 46  47  48  49  50  [51]  52  53  54  55  56  57  58  59  60  ...
NoWriterDateCnt.TitleFile(s)
1556김응규4/22/201512220    답변글 [답변]: WCF net.tcp 관련해서 질문 드립니다. [1]파일 다운로드1
1554(삭제)4/19/201512980안드로이드 에뮬레이터에서 특정 앱 구동불가할때! [2]
1552spow...4/18/201518902마이크로세컨드 단위의 Sleep이 필요한데 찾질 못하겠습니다. [5]
1553spow...4/18/201512174    답변글 [답변]: 마이크로세컨드 단위의 Sleep이 필요한데 찾질 못하겠습니다. [1]
1551인디언기...4/16/201511572iisnode에 대해서 글 올리셨던데 질문이 있습니다 [1]
1549김선아4/15/201512028.net framework 4.5와 c++ dll에 관한 질문입니다 [1]
1550김선아4/15/201510445    답변글 [답변]: .net framework 4.5와 c++ dll에 관한 질문입니다 [2]
1547문명식4/3/201512464IWebBrowser Key Event 관련 [2]
1545나종식3/25/201511429internet explorer dns cache flush [2]
1543이용원3/23/201511431hyper-v 질문드려요! [1]
1544이용원3/24/201510554    답변글 한가지만 더 여쭤볼게요! [1]
1541임기성3/20/201517788Microsoft.Office.Core dll관련 문제 [1]
1539펜플3/17/201511114asp.net 질문입니다. [1]
1540113/17/201511116    답변글 [답변]: asp.net 질문입니다. [1]
1538열공합시닷3/13/201511883중계서버 질문드립니다 [1]
1537김재영3/11/201512954asp.net의 빌드 유휴시간 이후의 리빌드 작업을 컨트롤 할 수 있습니까? [2]
1536솔솔3/9/201511395mac address얻으려고 하는데요.. [1]
1535Stud...3/4/201511948C#엔 MFC의 theApp 과 같은 기능을 하는것이 없나요? [3]
1534김동진3/4/201511388C#에서 BHO로 Windows Explorer에서의 동작이 가능할지요 [2]
1533김동현3/3/201511661컴파일 된 프로그램에 코드 삽입(?) 관련입니다. [1]
1532반가워요3/3/201514262C# 소켓연결에 packet송수신... [8]
1531강현수3/2/201512904C# com 등록 시 GUID 변경 관련 문의 [1]
1529김민규2/15/201512778C++ 64bit/32bit [3]파일 다운로드1
1528김개똥2/14/201510272좀 관련없는 질문이지만, 정말 답답하고 찾는데 명확한 해답 찾지못해 질문 올립니다. <Windows 사용자 인증> [1]
1527방문자1/27/201511326System Timer의 Tick에서 Backgroundworker를 호출하면 스레드 문제가 있을까요? [1]
1526황상대1/26/201513141C# 마샬링 관련 질문이 있습니다. [1]
... 46  47  48  49  50  [51]  52  53  54  55  56  57  58  59  60  ...