Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사],
조회: 18937
글쓴 사람
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)
3583노영우7/20/201519951iisnode 를 클래식 파이프라인 모드에서 호스팅하기 [2]
3582황희성7/17/201521999동영상 캡쳐에 관련해서 궁금하것이 있습니다. [6]파일 다운로드1
3581popo7/15/201518220[WPF] DependencyProperty에서 callback 처리시 문의 입니다. [1]
3580국왕님7/14/201518395어셈블리 서명 pfx 파일의 가져오기시 암호를 확인할 수 있을까요? [1]
3579고훈용7/13/201518299스마트클라이언트 윈도우8.1 실행오류 해결 방법 문의 [1]파일 다운로드1
3578이성환7/9/201521062WPF Multi Dispatcher 사용 시 hang 발생 [4]파일 다운로드1
3577초보개발자7/9/201519948C# SHDocVw.InternetExplorer 관련 도움좀 부탁드리겠습니다. [1]
3576솔솔7/6/201518601zip압축시! [2]
3575염기돈6/29/201519330책을 보다가 익명 메서드 관련해서 질문거리가 생겼습니다. [2]파일 다운로드1
3574김기술6/26/201517457프로그램 사용중 USB 경로 관련 해서 문의 드립니다 [4]
3573gagl...6/24/201520523IIS 의 특정 페이지들에서 20초의 딜레이가 있습니다. [2]
3572프란치스코6/24/201517972책을 구입하고 싶은데, [5]
3571로손6/22/201518459.Net COM dll 관련 사용시 Event 부분에 관한 문의 입니다. [5]파일 다운로드1
3570나그네6/11/201522362안녕하세요.. ^^ 궁금한게 있어서 혹시 조언을 얻을수 있을까 해서.. [2]
3569김지용6/9/201524216FFmpeg.exe 를 이용한 C# 동영상 인코더 예제보고 질문 드립니다. [9]
3568유동근6/5/201518929C# TTS 오류 입니다 도와주세요.파일 다운로드1
3567김보경5/26/201519516c#이용한 음성인식에 질문드립니다. [1]
3566로손5/26/201521571Http 파일 업로드시 한글파일명 관련 [1]
3565김태훈5/20/201517111AxWebBrowser 파일 다운로드 관련하여 문의드립니다. [3]
3564이강산5/20/201517222MD5 인코딩 관련 문의 입니다 [1]
2564CatO...5/13/201518367AppDomain 문제로 삽질중입니다. [2]
2563솔솔5/13/201517016custom search!! [1]
2562안녕하세요5/13/201517898안녕하세요.. C# 구조체 관련. [1]
2560솔솔5/6/201517846zip파일생성시. [1]
2561솔솔5/6/201518268    답변글 [답변]: zip파일생성시. [5]파일 다운로드1
2558로손5/4/201521301VB6.0의 ActiveX(OCX) 와 .Net에서 만든 COM DLL의 차이점 ?? [1]
... 46  47  48  49  50  [51]  52  53  54  55  56  57  58  59  60  ...