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

... 46  47  48  [49]  50  51  52  53  54  55  56  57  58  59  60  ...
NoWriterDateCnt.TitleFile(s)
3606guest9/20/201511069확장 클래스 네이밍 규칙은 어떻게 하시나요? [1]
3605spow...9/18/201510233익명형식을 다른 클래스의 인스턴스로 전달하고 싶습니다. [1]
3604popo9/11/201511403[C#] Singleton 을 사용하면서 궁금한점이 있어 질문 남깁니다. [2]
3603강준9/11/201510422xamarin 을 시작하려고 하는데 혹시 도움이 될만한 사이트나 정보 있으면 공유해 주세요~~ [1]
3602Mink...9/9/201511454aspx > 다른 확장자로 변경 후 디버깅 때문에 질문 드려 봅니다. [10]
3601edain9/8/201511641캐스팅...에 관해 궁금합니다. [5]
3600guest9/7/201514912C# 버전의 정의는 어떻게 구분하나요? [2]
3599김태훈9/4/201512566안녕하세요. C언어를 배우려고 하는 문과생입니다. [1]
3598미니8/29/2015131286.0은 전체소스? [1]
3597손성수8/28/201511659시작하세요 ! c# 프로그래밍 이책으로 배우고 있는데요 [2]
3596지나가는...8/26/201511217크로스플랫폼 [1]
3594guest8/25/201510886요즘 Windonws Form 기반 N Tier 어떻게 구성하나요?? [1]
3593개발자8/18/201513622현 시점에서 VS 버전은 뭐가 좋을까요? [8]
3592나그네8/15/201513005안녕하세요 질문이 있어서... [4]
3589spow...8/11/201514101Nancy + Razor 사용시 cshtml의 IntelliSense 및 참조 오류 [3]
3590spow...8/12/201513004    답변글 [질문]: (첨부 추가) [1]파일 다운로드1
3588재현8/8/201517790안녕하세요. C# 6.0 책을 보는 중에 의문이 생겨 질문드립니다. [4]
3587주문중8/4/201517814신간 출간 [7]
3586꿈꾸는개...7/29/201512793자바스크립트 checkbox 관련하여 ie10에서 호완이 안되는 부분이 있어 질문드립니다 [1]
3585유동근7/21/201511415TTS오류 [1]파일 다운로드1
3583노영우7/20/201511524iisnode 를 클래식 파이프라인 모드에서 호스팅하기 [2]
3582황희성7/17/201514405동영상 캡쳐에 관련해서 궁금하것이 있습니다. [6]파일 다운로드1
3581popo7/15/201511053[WPF] DependencyProperty에서 callback 처리시 문의 입니다. [1]
3580국왕님7/14/201511293어셈블리 서명 pfx 파일의 가져오기시 암호를 확인할 수 있을까요? [1]
3579고훈용7/13/201510718스마트클라이언트 윈도우8.1 실행오류 해결 방법 문의 [1]파일 다운로드1
3578이성환7/9/201513264WPF Multi Dispatcher 사용 시 hang 발생 [4]파일 다운로드1
... 46  47  48  [49]  50  51  52  53  54  55  56  57  58  59  60  ...