Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사],
조회: 17587
글쓴 사람
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)
4693초보2/25/201615998카카오톡 오류 질문 [1]
3701강준2/19/2016253852개의 DataTable Join 결과 전체 컬럼을 DataTable 로 리턴하기 [5]
3700박성훈2/18/201617381시작하세요! C# 6.0 프로그래밍 105쪽 예제 질문 [1]
3699박성훈2/18/201616489공부 방향성 질문 [2]
3698강준2/16/201616707Entity Framework 에서 Select for Update 가 가능한가요??? [1]
3697ds2/16/201616439어느 정도 문법을 알고 나면 [6]
3695Bere...2/15/201619389호출당한 메서드가 호출한 메서드를 알 수 있는 방법이 일반적인 방법 말고도 있을까요? [2]
3693조영준2/11/201620379UWP 앱을 만들고 있습니다. 죄송하지만 몇 가지 질문 드립니다. [4]
3692후배2/8/201620031html/css/js를 이용한 winForm의 UI표현이 가능 한가요? [3]
3690김대석2/5/201617831문의 드립니다. [2]
3687허재영2/3/201619690web api 보안관련 질문입니다. [2]
3685조진우2/1/201619637메인 응용 프로그램에서 DLL을 실시간으로 접근하여 사용하는데 이것을 모니터링 할 수 있을 까요? [1]
3684나그네1/27/201618837안녕하세요..wcf 관련하여 질문있습니다. [4]파일 다운로드1
3683김태형1/25/201619308 안녕하세요 저작도서를 구매하려는데 한가지 문의 사항이 있어서 질문드립니다. [1]
3681이영균1/8/201622462IE브라우저 추가기능관리의 항목을 사용함으로 c#코드로 변경을 하려고 합니다. [8]
3680후배1/6/201622034추천할만한 소켓 라이브러리 있으신가요? [2]
3679초록물꼬기1/6/201621077Parallel.For 에서 동기화문제에 관한 질문입니다. [11]파일 다운로드1
3678김민우1/3/201619467IHS 오류에 대한 질문입니다. [2]파일 다운로드1
3677초록색우산12/31/201520908HTTP 통신 - WebClient 이용시 한글깨짐 현상 발생 [2]
3676서정열12/31/201520064안녕하세요 WebService SoapExtensionImporter 에 대해서 질문드립니다. [3]
3675차가워12/30/201518893윈폼, 폼인폼 상황에서 하위컨트롤 포커스 문제 [1]
3673Sung...12/24/201520092UWP예외 발생 : 'System.Runtime.InteropServices.COMException'(mscorlib.ni.dll) 해결방법 [2]
3672Sung...12/23/201518627global::System.Diagnostics.Debugger.Break();가 노랗게 표시될시 [2]
3671이상준12/22/201523490한글 키보드 입력에 대해서 질문이 있습니다. [3]
3670Sung...12/18/201519385UWP 주기적으로 신호를 보내고 클라이언트에서 신호가 오면 받는 프로그램을 하려고 합니다. [5]
3669다비드12/18/201518109인터넷 임시파일 삭제 관련 질문. [1]
... 46  [47]  48  49  50  51  52  53  54  55  56  57  58  59  60  ...