Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사],
조회: 18563
글쓴 사람
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)
1245POPO3/26/201418441Http 프로토콜 관련 질문 입니다. [1]
1244(non...3/26/201418199(글쓴이의 요청으로 삭제합니다.) [1]
1241(non...3/22/201421753(글쓴이의 요청으로 삭제합니다.) [4]
1240이석주3/21/201424596인터넷 익스플로러가 hang이 걸리는 현상 문의 [1]파일 다운로드1
1238(non...3/13/201418576(글쓴이의 요청으로 삭제합니다.) [2]
1237(non...3/11/201418556(글쓴이의 요청으로 삭제합니다.) [2]
1236(non...3/11/201419471(글쓴이의 요청으로 삭제합니다.) [2]
1235(non...3/10/201418238(글쓴이의 요청으로 삭제합니다.) [2]
1234(non...3/10/201421972(글쓴이의 요청으로 삭제합니다.) [3]
1233(non...3/9/201419693(글쓴이의 요청으로 삭제합니다.) [4]
1232(non...3/8/201418617(글쓴이의 요청으로 삭제합니다.) [2]
1231(non...3/7/201419691(글쓴이의 요청으로 삭제합니다.) [9]
1230POCO3/7/201420070쓰레드 안에서 DependencyProperty get, set시 또 다른 스레드 오류.. [1]
1229(non...3/6/201420961(글쓴이의 요청으로 삭제합니다.) [11]
1228POCO3/6/201418882안녕하세요. 질문이 있습니다. [1]
1226김형진3/4/201429050안녕하세요 windows azure에 관해 질문했던 사람입니다. [2]
1224(non...3/3/201424513(글쓴이의 요청으로 삭제합니다.) [11]
1223sadf...3/3/201418875아래 질문에 답변 감사드립니다. 한가지 더 궁금한점이 있어 질문드립니다. [1]
1222(non...3/2/201419213(글쓴이의 요청으로 삭제합니다.) [4]
1221(non...3/1/201419711(글쓴이의 요청으로 삭제합니다.) [2]
1220Until2/28/201418095질문드립니다. [1]
1219이성환2/28/201417450string.Join()과 Enumerable.Aggregate()의 차이가 궁금합니다. [2]파일 다운로드1
1218김형진2/25/201419357안녕하세요. window azure에 대해서 질문이 있어서 문의 드립니다 [4]
1217(non...2/23/201419925(글쓴이의 요청으로 삭제합니다.) [1]
1215아리수2/20/201423402C# 공부하면서 WPF에 대한 질문. [2]
1214조광훈2/20/201421427IIS8 응용프로그램 풀 관련 질문 드립니다. [2]파일 다운로드1
... 46  47  48  49  50  51  52  53  54  55  56  [57]  58  59  60  ...