Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사],
조회: 11251
글쓴 사람
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)
3578이성환7/9/201513251WPF Multi Dispatcher 사용 시 hang 발생 [4]파일 다운로드1
3577초보개발자7/9/201512176C# SHDocVw.InternetExplorer 관련 도움좀 부탁드리겠습니다. [1]
3576솔솔7/6/201510998zip압축시! [2]
3575염기돈6/29/201511886책을 보다가 익명 메서드 관련해서 질문거리가 생겼습니다. [2]파일 다운로드1
3574김기술6/26/201510049프로그램 사용중 USB 경로 관련 해서 문의 드립니다 [4]
3573gagl...6/24/201512792IIS 의 특정 페이지들에서 20초의 딜레이가 있습니다. [2]
3572프란치스코6/24/201511275책을 구입하고 싶은데, [5]
3571로손6/22/201511347.Net COM dll 관련 사용시 Event 부분에 관한 문의 입니다. [5]파일 다운로드1
3570나그네6/11/201514164안녕하세요.. ^^ 궁금한게 있어서 혹시 조언을 얻을수 있을까 해서.. [2]
3569김지용6/9/201517095FFmpeg.exe 를 이용한 C# 동영상 인코더 예제보고 질문 드립니다. [9]
3568유동근6/5/201511562C# TTS 오류 입니다 도와주세요.파일 다운로드1
3567김보경5/26/201512032c#이용한 음성인식에 질문드립니다. [1]
3566로손5/26/201514367Http 파일 업로드시 한글파일명 관련 [1]
3565김태훈5/20/201510481AxWebBrowser 파일 다운로드 관련하여 문의드립니다. [3]
3564이강산5/20/201510329MD5 인코딩 관련 문의 입니다 [1]
2564CatO...5/13/201511127AppDomain 문제로 삽질중입니다. [2]
2563솔솔5/13/201510122custom search!! [1]
2562안녕하세요5/13/201510664안녕하세요.. C# 구조체 관련. [1]
2560솔솔5/6/201510598zip파일생성시. [1]
2561솔솔5/6/201511086    답변글 [답변]: zip파일생성시. [5]파일 다운로드1
2558로손5/4/201513824VB6.0의 ActiveX(OCX) 와 .Net에서 만든 COM DLL의 차이점 ?? [1]
2559로손5/6/201511408    답변글 [답변]: VB6.0의 ActiveX(OCX) 와 .Net에서 만든 COM DLL의 차이점 ??
2557Guest4/24/201510352Serialize , DeSerialize 관련 질문입니다! [3]
1558민식짱4/23/201510618FFmpeg.exe 를 이용한 C# 동영상 인코더 예제를 보고 질문드립니다 [1]파일 다운로드1
1555김응규4/22/201514879WCF net.tcp 관련해서 질문 드립니다. [2]
1556김응규4/22/201512220    답변글 [답변]: WCF net.tcp 관련해서 질문 드립니다. [1]파일 다운로드1
... 46  47  48  49  [50]  51  52  53  54  55  56  57  58  59  60  ...