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

... 76  77  78  [79]  80  81  82  83  84  85  86  87  88  89  90  ...
NoWriterDateCnt.TitleFile(s)
530독불장군12/20/200613024        답변글 [답변]: [답변]: 답변에 감사 드립니다.
523주낙현12/12/200612428healthMonitoring 구성섹션을 설정하는 방법 [1]
519이진형12/8/200612186스마트 클라이언트에서 부모 IE 컨트롤 하기
520정성태12/11/200613009    답변글 [답변]: 스마트 클라이언트에서 부모 IE 컨트롤 하기 [3]
517주낙현12/5/200612859Property 재정의가 가능한가요?? [1]
516양덕진11/29/200612464SSL인증서 질문? [1]
515경혜원11/27/200613781화면 캡춰 관련해서 문의 드립니다. [1]
513양덕진11/26/200616086SSL인증문제?
514양덕진11/27/200617897    답변글 [답변]: SSL인증문제? [1]
512윤창일11/24/200615375[ActiveX vs 가장기법] [1]
511방문자11/24/200614083스마트 클라이언트 질문입니다. [3]
509주낙현11/22/200615536스마트 클라이언트에서 WebBrowser 컨트롤사용? [4]
508임장현11/20/200614967서로다른 프로젝트간의 세션공유에 대해 질문드립니다.
510정성태11/22/200613374    답변글 [답변]: 서로다른 프로젝트 간의 세션 공유에 대해 질문드립니다.
504탑.11/10/200614037COM+ 구성 좀 봐주세요...
505정성태11/14/200613808    답변글 [답변]: COM+ 구성 좀 봐주세요...
506탑.11/15/200613092        답변글 [답변]: [답변]: COM+ 구성 좀 봐주세요... [2]
503sagi...11/10/200613022bho 폼 관련 질문입니다/ [1]
501안연준11/7/200613668(SmartClient) 프레임워크 1.1 과 2.0 은 호환이 안된다? [3]
507안연준11/17/200613379    답변글 [답변]: (SmartClient) 프레임워크 1.1 과 2.0 은 호환이 안된다? [1]
498쿠리마10/30/200613816Smart Client를 VC60 MFC Dialog 에서 사용하기
500정성태10/30/200615678    답변글 [답변]: Smart Client를 VC60 MFC Dialog 에서 사용하기 [3]
497이방은10/30/200615909질문 있습니다..ㅡ.ㅠ;
499정성태10/30/200615855    답변글 [답변]: 질문 있습니다..ㅡ.ㅠ; [2]
495엔틱스10/25/200613376안녕하세요. 세션에 관련해서 질문을 올립니다. [2]
493안연준10/25/200613997스마트클라이언트 배포에서 Config 내용이 이해가 안되요
... 76  77  78  [79]  80  81  82  83  84  85  86  87  88  89  90  ...