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

1  2  [3]  4  5  6  7  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
5885guest4/11/20233032c#으로 드림위버같은 거 만들어볼려는데요 [6]
5884궁금이4/11/20232908부모 클래스에서 예외 발생시 힙 영역에 할당 ? [2]
5883코딩초짜4/9/2023282610c언어 usleep 에 대해서 요 [2]
5882조은현4/7/20232839선생님 안녕하세요! wpf의 성능 개선에 대해서 질문드려요! [1]파일 다운로드1
5881guest4/6/20232984static method - <에러메시지 Extension method must be defined in a non-generic static class> [4]
5880유비4/4/20232859WPF DataGrid CollectionView, IEditableCollectionView 관련 문의 [1]
5879guest4/4/20233074Async method의 에러 표시 [3]
5878guest4/3/20233168C#으로 CMOS 설정 변경가능한지요? [4]
5875guest4/2/20233580성태님 책을 완독 하고 Static [7]
5874민성4/1/20232980안녕하세요 질문 하나만 드릴깨요~ [1]
5873guest3/31/20233230제어판에서 삭제불가 MS Edge ---> 레지스트리 편집기에서도 안보임 [6]파일 다운로드1
58723/31/20232878web config 파일 확인부탁드려요 [6]
58713/31/20232790web config 파일 수정이요 [2]파일 다운로드1
5870guest3/30/20233276.NET Core SDK 삭제 시 주의 사항 [4]파일 다운로드1
5869guest3/30/20233497Dictionary의 Update 그리고 Foreach [7]
5868guest3/29/20232970Speech Recognition과 Form1 그리고 정확도 [4]파일 다운로드1
5866월급쟁이3/28/20233063cmake 크로스 컴파일 관련하여 질문이 있습니다 [1]
5865guest3/28/20232936Github Copilot과 코딩실력 향상? [1]
5864guest3/27/20233362System.NullReferenceException - 개체참조가 개체의 인스턴스... [6]파일 다운로드1
5863guest3/24/20233490이벤트 핸들러 사라짐 현상 - Button [4]
5862guest3/21/20233565세계최초 hts와 싱글스레드 [8]
5861다크파이썬3/21/20233729WPF를 사용하려고 하려고 도서 문의합니다. [2]
5860guest3/21/20233012인텔코어 i5 CPU와 스레드 [4]
5859guest3/21/20232854개발 일지 어떻게 관리하시나요? 이런 프로그램 없나요? [3]
5858김태원3/18/20232912안녕하세요! [5]
5857guest3/17/20232986귀도 반 로썸을 보고 [4]
1  2  [3]  4  5  6  7  8  9  10  11  12  13  14  15  ...