Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사]
조회: 11138
글쓴 사람
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)
5251kss11/1/20196905c#문법질문입니다. [1]
5250이대희10/31/20196999시작하세요 C# 7.3 프로그래밍 책이 절판이네요. [1]
5249황태관10/24/20197470c# 폼안에 특정 프로그램 불러오기.. [1]파일 다운로드1
5248황태관10/23/20198333C언어로 만든 dll 를 참조해서 c#으로 만들려면.. [3]
5245민성10/21/20197480안녕하세요 Ui 스레드 접근에 대해서 질문드리도록 하겠습니다. [1]
5244hbli...10/20/20196881두개의 쓰레드에서 하나의 Queue 리스트 사용시 질문드립니다. [2]
5243최선호 donator10/18/20196454Screen Capture 와.. Overlay 질문 좀 드릴게요 ㅠ.ㅠ ( 장문 주의..... ) [2]
5241C#초보10/16/20198492C# USB 통신 구현 관련 질문 드립니다! [1]
5240김동규10/15/20198439런타임 시 Encoding.Default를 UTF-8로 설정할 수 있을까요? [3]
5239밤톨이10/13/20196692시놀리지 DSM에 asp.net core 2 웹서버 구축이 가능한가요? [1]
5238권석헌10/2/20197239C# - C++ 간의 Memorymapped file 관련 Data 질문좀 드립니다 선생님 [1]
5237유호성10/1/20196975사용자 정의 컨트롤 VS IDE에서만 Exception 발생 [2]
5234koko9/25/20199223안녕하세요. 64bit WPF -> 32bit C++ DLL Import사용 문의 드립니다. [3]
5232fox3...9/24/20197072C# Memorymappedfile 재 업로드 드립니다.(실행순서 및 질문내용 정리) [7]파일 다운로드1
5231fox3...9/24/20197469C# 메모리맵드파일 관련 질문드립니다. 최소한의 재현가능코드 첨부후 재질문 드립니다. [2]파일 다운로드1
5230질문9/23/20197131C++ 로 만든 dll 을 C#에서 사용할 때 질문 [2]
5229fox3...9/23/20199506C# 메모리맵드파일 관련 질문드립니다. [2]
5227세퉁9/23/20198182WPF Textblock 폰트 크기에 따라 글자 색이 깨지는 현상이 있습니다. [3]파일 다운로드1
5226김대훈9/23/20197827정말 황당한 경우입니다.. [2]
5223김태균9/19/20197561책 소개 링크가 7.1버전판으로 이어집니다. [1]
5222냥냥이9/14/20198094프로그래밍 논리력이 많이 부족합니다 [3]
5219티지레몬9/9/20199564c# PCB 자동화 프로그램(윈도우 폼 위주로 작업) 제작 준비 [3]
5218민성9/9/20197624안녕하세요 WPF에서 xaml 안에 다른 xaml을 넣고 싶습니다. [1]파일 다운로드1
5216WPF9/8/20198709WPF에서 XAML Islands를 사용하여 Win2D를 사용하니 그래픽 품질이 저하됩니다. [2]파일 다운로드1
5215허송세월9/5/20198523중복실행 방지 관련 문의 [2]파일 다운로드1
5214Jang...9/4/20198126[DB 테이블의 데이터 변경에 대한 알림 처리] SQL-Server말고 MySQL은 불가능하겠죠? [1]
... 16  17  18  19  20  21  22  23  24  25  [26]  27  28  29  30  ...