Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사],
조회: 18556
글쓴 사람
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)
5517wunsy6/18/202114848winform에서 Button 활성화, 비활성화 [4]
5516ocm6/15/202113728mips 어셈블리 연산 다시 질문드려요 (첨부파일 갱신됨) [2]파일 다운로드1
5515ocm6/14/202115677mips 어셈블리 연산 [6]파일 다운로드1
5514jongs6/11/202116566GethashCode와 String대한 질문 [2]
5513labe...6/11/202115819C# Winform 에서 Label에 동일한 Color를 넣었을 때 처리방법이 궁금합니다. [2]
55126/11/202116213xlwings 가 실행조차 되지 않습니다. ㅠㅠ [7]
5511Syong6/9/202114245User Control에 string array 속성 추가하는 방법 [4]
5510jay6/8/202113823string 문자열에 쌍따옴표(")를 넣고 싶습니다. [1]파일 다운로드1
5509한예지 donator6/3/202113657ExeccuteNonQuery 질문있습니다! [2]
5508최재남6/3/202114954마우스 이벤트 관련 질문 좀 드리겠습니다. [6]
5507한예6/2/202113329static과 스택오버플로우 질문있습니다. [5]파일 다운로드1
5506이창석6/1/202115164센서의 값을 받아서 PC를 통해 모니터링 하는 것을 만들고 있습니다. [1]
5505kss5/31/202113605.net5에서 소멸자가 어떻게 바뀐건가요? [1]
5503xing...5/24/202114167xingapinet 에 수정 요청 부탁 드립니다. [1]
5501한예지 donator5/23/202114610IProgress 사용법이 궁금합니다. [2]
5500한예ㅈ5/23/202116459비동기 코드 흐름 질문있습니다. [3]
5498곰장어5/21/202114283List에 static 변수를 추가했을때의 궁금증 [3]파일 다운로드1
5497지평선5/20/202116187윈도우 배율을 알 수 있을까요? [1]
5496cs린이5/20/202113101C# 8.0 질문입니다. [2]파일 다운로드6
5495Natie5/13/202112752객체를 생성과 동시에 초기화 하는 방법 [1]
5494지예예지5/13/202115099비동기 코드 흐름이 궁금합니다! [2]
5493xing...5/6/202112834xing api XQCSPAT00600 질문입니다 [4]파일 다운로드1
5492한예지 donator5/5/202112997FromCurrentSynchronizationContext 관련 코드 질문있습니다! [2]
5491조우성5/4/202118524WinForm과 WPF의 성능차이 비교한 자료가 있을까요? [4]
5490한예지 donator5/3/202114280UI 스레드의 Invoke 질문있습니다. [4]
5489저누4/28/202114760시작하세요 C# 9.0, 225페이지 구조체 관련 질문드립니다. [2]
... 16  17  [18]  19  20  21  22  23  24  25  26  27  28  29  30  ...