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

... 61  62  63  64  65  66  67  68  69  70  71  72  73  [74]  75  ...
NoWriterDateCnt.TitleFile(s)
670조성택7/3/200710328.Net에서 Timer클래스.. [1]
667홍석헌6/29/20079842[c#] 윈도우 활성화에 관한 질문
669정성태6/30/200710317    답변글 [답변]: [c#] 윈도우 활성화에 관한 질문
666오야6/25/20079267System.Transactions 에서 트랜잭션 실패를 체크하는 방법 [1]
665김성태6/20/20078665안녕하세요^^
668정성태6/30/20079780    답변글 [답변]: 안녕하세요^^
663모름이6/19/20078530Microsoft.Framework 에서 OLE DB 사용하려면.. [1]
664모름이6/20/20079217    답변글 [추가질문]: 다른 OLE DB 를 사용하려면..
662조성택6/13/20079859C#에서 string클래스.. [1]
660신동열6/11/200711146ClickOnce 업데이트시 이전 버젼 제거오류 [1]
659한귀순6/11/200712490윈폼 프로그램 및 디자인. [2]
661한귀순6/12/200710025    답변글 [추가질문]: 윈폼 프로그램 및 디자인. [3]
657정해봉6/8/200720185요청 시간 초과 오류 [2]
655이방은5/28/200710815성태님 UML 도구 뭐 쓰십니까??? [1]
654임정훈5/23/20079844카드스페이스 관련질문
658정성태6/9/200710337    답변글 [답변]: 카드스페이스 관련질문 [1]
653사다수스5/22/200731154아래한글(HwpCtrl) ocx 메모리 릭 문제입니다. [4]파일 다운로드1
645송원석5/15/200712461ASP 의 ByVal 과 ByRef 특성
646정성태5/15/200712335    답변글 [답변]: ASP 의 ByVal 과 ByRef 특성
647송원석5/16/200712664        답변글 [답변]: [답변]: ASP 의 ByVal 과 ByRef 특성 [1]
648정성태5/16/200711417            답변글 [답변]: [답변]: [답변]: ASP의 ByVal과 ByRef 특성 [1]
649장윤태5/16/200710588                답변글 [답변]: [답변]: [답변]: [답변]: ASP 의 ByVal 과 ByRef 특성
650정성태5/16/20078757                    답변글 [답변]: [답변]: [답변]: [답변]: [답변]: ASP의 ByVal과 ByRef 특성 [2]
643해운대끓...5/15/20078412Singleton 패턴 테스트중 하나의 윈폼만 열기 오류파일 다운로드1
644정성태5/15/20079040    답변글 [답변]: Singleton 패턴 테스트중 하나의 윈폼만 열기 오류 [1]
642조성택5/10/20079928스레드와 WinINet? [1]
... 61  62  63  64  65  66  67  68  69  70  71  72  73  [74]  75  ...