Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사]
조회: 11219
글쓴 사람
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)
5756흰털너부리11/25/20223413asp.net core web api에서 json 특정 property 무시하는 방법 문의 드립니다. System.Text.Json 사용중입니다. [1]
5755문정환11/24/20223765싱글스레드 프로그램도 컨텍스트 스위칭이 생길 수 있나요? [4]
5754초급11/24/20223601c# 소켓통신 [1]
5753흰털너부리11/24/20223411List와 ObservableCollection을 비교 해서 다른 값 추출 FirstOrDefault 객체 비교 [4]파일 다운로드1
5752푸헐11/15/20223549app.config 에 connectionStrings를 aspnet_regiis로 enctyption [4]
5751차가워11/8/20224350vs2022 preview net7 AOT 콘솔 실행 성능 [7]
5749차가워11/4/20223767전처리 지시문 #if DEBUG 배포시 실행 여부 [1]
5748김기헌11/3/20224094안녕하세요 선생님 싱글톤 패턴을 꼭 이렇게 사용해야 하나요? [6]
5747김기헌11/2/20223495안녕하세요 선생님 네트워크 관련 용어 중 IP 주소가 왜 논리적 주소라고 표현되는 건가요? [2]
5746물냉면이...11/2/20223679서로 다른 클래스에 있는 동일 함수의 일괄 호출 방법에 대해 궁금합니다. [3]
5745흰털너부리11/1/20224025.net core web api 사용 제한에 관한 질문 입니다. [2]
5744차가워10/31/20224415윈폼 Console.WriteLine(); 연산 문의 [1]
5743흰털너부리10/27/20223735reflection, static, override 질문입니다. [1]
5742차가워10/27/20223641하나의 socket에 여러 스레드가 접근 하는 경우 [1]
5741조호상10/27/20223875OpenCVSharp4 구현 가능 문의 [1]
5740혜성10/26/20224711Visual Studio 2022 C# 콘솔 프로그램 기본 코드 변경된 이유는 무엇인가요? [2]
5739슬픈단잠10/25/20223672조언 주신 방법으로 해봤으나, 여전히 어디가 문제인지 파악을 못했습니다. [2]
5738슬픈단잠10/24/20224059C# 마샬링 관련 질문입니다. [2]
5737감사합니...10/20/20224699찾은 어셈블리의 매니페스트 정의와 어셈블리 참조가 일치하지 않습니다. [8]파일 다운로드1
5736초보 개...10/19/20223705c# winforms 프로그램을 setup 파일로 배포 시, 설정 문의 [2]
5735농상10/17/20224376싱글톤 공부중 질문이 있습니다. [8]
5734mins10/14/20224133델파이 dll을 c#에서 사용하기 관련해서 포인트 관련이라 질문을 올립니다. [2]
5733김경환10/12/20224136선생님 질문하나만드리겠습니다. [1]
5732kss10/8/20224428c# socket.poll 버그인가요? [2]파일 다운로드1
5731kss10/8/20223988c# socket.poll 버그인가요? [3]
5730김재환10/7/20224512WPF에서 디스플레이 배율이 100%가 아닌 경우, Window의 포지션 정보가 부정확해지는 문제 [2]
1  2  3  4  5  6  7  [8]  9  10  11  12  13  14  15  ...