Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사]
조회: 11116
글쓴 사람
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)
5617Edun2/25/20224349ArgumentOutOrRangeException에러 발생 [2]파일 다운로드1
5616csha...2/24/20224261readonly struct로 선언된 구조체를 특정 클래스에서 멤버변수로 가지는 경우 [1]
5615장성욱2/22/20226449SetThreadAffinityMask를 이용한 쓰레드 지정하는 방법에 대해 궁금합니다. [4]
5614민우2/22/20225811SSL 통신 문의 [6]
5613김인태2/22/20224299서버와 PC 간의 어플리케이션 성능 차이 [1]
5612팬입니다2/20/20224116Kastrel 서버 관련 [1]
5611차가워2/19/20224336stopWatch 늘어짐 문의 [3]
5610차가워2/18/20224221Stopwatch 늘어짐 문제 [1]
5609cs린이2/15/20224556c# 함수의 호출 방식에 대해 궁금합니다! [2]
5608지호2/10/20225088시작하세요 C# 8.0 중 제네릭타입의 IEnumerable [3]파일 다운로드1
5607이로운2/10/20224309안녕하세요. 궁금한게 있어서 질문드립니다. [1]
5605강성봉2/10/20224310TCP PSH flag 패킷 수신 에러 [1]
5604LW2/9/20224443VISUAL STUDIO 2019 ==> 2020 설치시 오류가 생겨서 문의드립니다. [3]
5603김진명2/9/20225736C# 10.0 출간은 언제쯤 계획하고 계신가요? [1]
5602신갑영2/8/20224542윈폼에 대해서 질문 드립니다. [1]
5601김인태2/4/20224771setup project 관련 [7]
5600itkim2/3/20225643윈도우 서버 계정 패스워드 인증 문의 [5]
5599레드골드2/3/20226144c#으로 ms word 제어 가능할까요? [6]
5598jaew...2/2/20224824Dictionary는 참조형식인가요?? [1]
5597재원2/2/20225162c# 9.0에 대한 내용을 받을 수 있나요? [1]
5596kss1/26/20224879듀얼 모니터 환경에서 wpf 프로그램 크기 변경 [2]
5594mira...1/25/20224531안녕하세요 try~catch 와 SuspendLayout~ResumeLayout 간 호출 문의드립니다! [2]
5593C#스터디1/12/20225455TaskAwaiter 구현 질문 입니다. [1]
5591유지킴12/24/20215054outofmemory in 32bit [2]파일 다운로드1
5590kss12/23/20214501포인터 메모리 에러 [1]
5589초급12/22/20214699c# -> 라즈베리파이(db 접속)시 에러 발생 [7]
1  2  3  4  5  6  7  8  9  10  11  12  [13]  14  15  ...