Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사],
조회: 11252
글쓴 사람
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)
1279(non...6/23/201411514(글쓴이의 요청으로 삭제합니다.) [17]
1278이상식6/19/201412744.net DLL 내 자바스크립트를 수정 또는 재정의 할 수 있을까요? [3]
1277김솔지6/18/201410437silverlight에서 datagrid, listbox질문이여 [2]
1276정우석6/16/20149949쿠키 [1]
1274김솔지6/10/201414441배포 페이지 url을 얻고 싶습니다. [8]
1272이훈모6/7/201410003정말 어려운 상황에 직면했습니다. [1]
1270Jong...6/2/201419473C#과 C++을 이용한 Image 처리. [13]
1269김아영5/29/201410176InitializeComponent 함수 호출 지연 현상 [5]
1268솔솔5/27/20149820smart client [1]
1266김솔지5/22/201411908clickonce 수정에 대해 알고싶습니다. [2]
1265이은아5/22/201415512DataGridView 헤더를 두줄이상으로 하고싶습니다. [1]파일 다운로드1
1264김인호5/18/201413091소스코드 및 예제그림 zip 파일 [1]
1263이영종5/15/201411545159페이지 오타인것 같습니다 [5]
1262(non...5/4/201412435(글쓴이의 요청으로 삭제합니다.) [10]
1261이근주5/4/201410744다시 한번 질문드릴께요. [2]
1259이근주5/1/20149787도서 오류인 것 같네요.. [1]
1258최세정4/28/201412198안녕하세요~php module 오류로 고민하다가 여기까지 왔네요..ㅜㅜ [2]
1252popo4/21/201411455바인딩 질문입니다. [2]
1251(non...4/20/201415588(글쓴이의 요청으로 삭제합니다.) [11]
1249홍용규4/17/201415549app.config 파일 관련 질문 있습니다. [2]
1246(non...3/30/201411902(글쓴이의 요청으로 삭제합니다.) [1]
1245POPO3/26/201411312Http 프로토콜 관련 질문 입니다. [1]
1244(non...3/26/201411419(글쓴이의 요청으로 삭제합니다.) [1]
1241(non...3/22/201415213(글쓴이의 요청으로 삭제합니다.) [4]
1240이석주3/21/201418096인터넷 익스플로러가 hang이 걸리는 현상 문의 [1]파일 다운로드1
1238(non...3/13/201411952(글쓴이의 요청으로 삭제합니다.) [2]
... 46  47  48  49  50  51  52  53  54  [55]  56  57  58  59  60  ...