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

... 16  17  18  19  20  21  22  [23]  24  25  26  27  28  29  30  ...
NoWriterDateCnt.TitleFile(s)
5345질문요6/12/20205871idc vs 집 속도 [2]
5344하태6/2/20207011c++ .ilb에서 c# dll 호출 질문 드리겠습니다 (콜백함수 전달) [4]
5343하태6/2/20209939안녕하십니까! c# dll을 c++ .lib에서 호출 질문 드립니다. [6]
5342진우5/30/202011049c++ 에서 C# DLL 사용 문의 [2]
5341미나리5/28/20206830스레드 lock키워드 관련 질문드립니다 [3]
5339민성5/27/20206101WPF cmd을 실행을 할때 파라미터 넘기는 방법 [1]
5338서영준5/26/20207988.Net Core Blazor 서버에 Xing API를 이용한 통신 요청 [5]파일 다운로드1
5337ogos...5/26/20208125C# DB connection string 보호 방법에 대하여 [2]
5336saki5/21/20208004이벤트 뷰어 .NET Runtime 오류 [3]
5335민성5/21/20206296안녕하세요 C#으로 컴퓨터 시작프로그램 목록을 가져와서 사용안함으로 바꿀려면 [1]
5334민성5/19/20206641안녕하세요 WPF 콘솔창을 띠우면서 Ping이라는 명령어가 콘솔에 Write되게 할려면 어떻게 해야 하나요?? [1]
5333초보5/18/20206702공유 메모리 관련 문의 [2]
5332질문요5/15/20206578TcpListener TcpClient 문의 [4]
5331sdd5/13/20206392안녕하세요 Settings관련하여 질문드립니다. [2]파일 다운로드1
5330하태5/7/202010734안녕하세요! 질문 드리겠습니다! C# dll에서 c++ CLR프로젝트를 참조 추가 할 수 있나요? [2]
5329김태령4/21/20206306젠킨스에서 원격 머신에 있는 실행 파일을 실행하면 백그라운드로 뜹니다 [2]
5328crea...4/17/20206363안녕하세요! [3]파일 다운로드1
5327김동욱4/17/20206595HttpListener 사용시 HTTP/2 질문입니다. [2]
5323나그네4/15/20205878해결 115p 네임스페이스의 ConsoleApp1.exe는 netcoreapp3.1 폴더에 있었습니다. [1]파일 다운로드1
5322나그네4/14/20206840질문 115p 네임스페이스의 ConsoleApp1.exe 컴파일 또는 빌드방법 [2]파일 다운로드1
5321나그네4/14/20207794오타인가요? [2]
5320kiki...4/10/20209878C# Serial 통신 관련 질문 입니다. [5]
5319권대현4/10/20206148C++(UWP)Dll에서 C#(UWP)Dll 호출이 가능한가요? [1]
5318이승준4/9/20207011동영상 관련 업계 근황? 입니다. [1]
5317kskk...4/7/20209027OpenCV 이용 해상도 설정 질문 입니다.. [3]
5316윤현수4/7/20206251pipe 비동기방식 질문 [1]파일 다운로드1
... 16  17  18  19  20  21  22  [23]  24  25  26  27  28  29  30  ...