Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사]
조회: 11176
글쓴 사람
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)
5591유지킴12/24/20215110outofmemory in 32bit [2]파일 다운로드1
5590kss12/23/20214563포인터 메모리 에러 [1]
5589초급12/22/20214762c# -> 라즈베리파이(db 접속)시 에러 발생 [7]
5588김지신12/21/20214712안녕하세요 String 변수 참조 주소 질문입니다. [2]
5587이완호12/17/20215814C# SharpDX 화면 캡쳐 관련해서 질문 드립니다. [1]
5586조미김12/16/20214987윈폼에서 메인 스레드와 UI 스레드의 차이점 질문드립니다 [4]
5585김준희12/16/20215750C# 윈폼 TCP/IP 데이터 연속으로 보낼때 [1]
5583난인간이다12/10/20214781.net 6 dynamic pgo 활성화 및 예상되는 문제 [1]
5582김준희12/9/202112020C# TCP/IP 통신시 연결 끊김 에러 [1]
5581김시준12/9/20215331닷넷에서 파일 delete 함수는 왜 비동기가 없는 것인가요? [2]
5580카짜프로...12/7/20216551패턴매칭 -튜퓰비교에 관한 오류사항과 궁금증 [1]파일 다운로드1
5579카짜프로...12/6/2021583011.12 메서드 중복정의에 대한 질문 [1]
5577감사합니...11/30/20214860visual studio 2015 update 3를 다운받을려고 하는데 x64/x86 차이점이 뭘까요? [2]
5576노홍구11/29/20214729C# 으로 USB 스캐너 프린터 리셋하기 입니다. [1]
5575베라11/23/20214804event handler 관련 문의 [2]
5574박원웅11/22/20215111닷넷 프레임워크 산출물의 배포시 해당 환경에 프레임워크 버전이 설치되어 있지 않는 경우를 고려한 배포방법은? [3]
5573mijin11/21/20214937System.NullReferenceException 에 대한 질문 [1]
5572김현진11/21/20215104C# list.Clear() 호출에 대한 문의 드립니다. [4]
5571한예지 donator11/18/20215048무설치 프로그램 원리가 궁금합니다. [2]
5570초보11/16/20215559주식데이터 초당 수신 건수를 구하고 있는데 처리속도가 느려서요 [2]
5569카짜프로...11/14/20216428381페이지 UTC에대한 언급이 그리니치 천문대 시간으로 되어있는게 맞나요? [1]
5568카짜프로...11/14/20216491그림 5.20, 그림 5.22 언급 오류 [1]
5567Edun11/2/20215414쿼리문을 코드로 어떻게 처리할 수 있을까요? [2]
5566민성10/26/20215240Linq에 관해서 [1]
5565pass...10/25/20216493wpf에서 Cefsharp를 AnyCPU로 작업 했는데 실행이 되지 않습니다. [4]
5564초보10/21/20214736오버라이드 관련 질문드립니다 [2]
1  2  3  4  5  6  7  8  9  10  11  12  13  [14]  15  ...