Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사],
조회: 11265
글쓴 사람
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)
1526황상대1/26/201513143C# 마샬링 관련 질문이 있습니다. [1]
1525Ji-y...1/19/201511759시작하세요 C# 프로그래밍 도서에 관한 질문입니다. [1]
1523임종복12/31/201419143windows 2008 r2에서 ms-sql 2008 서버사용시 tls문제점 [3]
1524임종복1/4/201514072    답변글 [답변]: windows 2008 r2에서 ms-sql 2008 서버사용시 tls문제점 [2]
1521윤대욱12/19/201411628C++ 에서 서버와 클라이언트를 나누어서 구조체 전달에 대해서 궁금합니다. [1]파일 다운로드1
152012/10/201411959datetime관련해서요 [1]
1516aTo12/1/201411435wpf 배포관련. [3]
1514얄미운제리11/17/201412702[C# Mono]Mono Chart 개발 문의드립니다. [1]
1355미나리나물11/13/201410243RDP 접속 방식 질문드리고 싶습니다. [1]
1353송용국11/10/20149399TeamFoundationServer 에 소스를 Target서버로 옮기는(내려주는) 방법 있나요? [1]
1351blue...11/7/201414213c# webbrowser 모바일환경 접속 [1]
1350에메11/7/201413685WCF namedpipe 퍼블리싱 충돌 문제 [3]파일 다운로드1
1349Chun...11/6/201418506Microsoft. Net Framework 액세스 거부 오류...... [2]
1348김준석11/6/201414359 Mini dump 기록 안되는 현상 [2]파일 다운로드1
1346개미11/5/201428756https페이지에서 http서버에 있는 이미지 파일 호출시 보안 오류 [8]
1345장구니11/4/201412270AWS 인스턴스를 만들었는데 [1]
1343h11/2/201412171안녕하세요 [1]
1342왕초보11/1/201417199C#에서의 함수포인터 [13]
1347왕초보11/5/201412363    답변글 [답변]: C#에서의 함수포인터 [1]
1341영택10/29/201413144MFC Amazon S3 사용관련하여 질문드립니다. [2]
1344영택11/4/20149904    답변글 [답변]: MFC Amazon S3 사용관련하여 질문드립니다. [2]
1340감사합니다10/28/201414304c# wpf shdocvw internet explorer를 윈도우프레임으로 집어 넣을 수 있는지 궁금합니다. [3]파일 다운로드1
1338헬프미10/28/201415245tcp 소켓의 SYN_RECV 상태에 대하여 질문있습니다. [3]
1336이소정10/27/201410766filestream seek에 대해 질문있습니다. [1]
1337이소정10/28/201410737    답변글 [답변]: filestream seek에 대해 질문있습니다.파일 다운로드1
1339이소정10/28/201413635        답변글 [답변]: [답변]: filestream seek에 대해 질문있습니다. [2]파일 다운로드2
... 46  47  48  49  50  51  [52]  53  54  55  56  57  58  59  60  ...