Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사],
조회: 17536
글쓴 사람
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)
3669다비드12/18/201518098인터넷 임시파일 삭제 관련 질문. [1]
3668김치사발면12/16/201517479템플릿이 자꾸 초기화? 되버리네요 [1]
3667김치사발면12/15/201517045템플릿 설정 추가 질문 [1]파일 다운로드1
3666노태현12/15/201516422사용자별로 권한을 주고 볼 수 있는 데이터를 제한하려면 어떤 방법으로 접근하는게 좋을까요? [2]
3665김치사발면12/14/201519700C# 템플릿 설정이 이상해요ㅜㅜ [3]파일 다운로드1
3664KuLu12/10/201516700멀정하던 사이트 중 일부메뉴가 응답없음이 되어버립니다. [3]
3663Sung...12/10/201518319UWP 서버 프로그래밍 작업 중 [5]
3662Sung...12/9/201518215uwp앱에서 접속한 클라이언트 주소값을 받아서 표시하고 싶은데 어떤 메소드를 써야 할까요? [7]
3661김무진12/9/201517718Oracle 환경에서 데이터를 조회할때 한글이 ? 이렇게 표시가됩니다. [1]
3660질문자12/7/201519777OpenFileDialog 호출시 hang걸리는 문제 [4]
3659Sung...12/4/201519553UWP 앱에서 textBox로 클라에서 받은 값을 나타내고 싶은데 안되고 있습니다. [5]
3658강준12/3/201518535Visual Studio (Xamarin) vs Eclipse [2]
3656DEVY...12/1/201517837MasterPageFile 사용시 다국어 처리 질문입니다. [1]
3653DEVY...11/26/201519565ds:Signature 질문입니다. [7]
3654윤용한11/27/201519572    답변글 [답변]: ds:Signature 질문입니다. [3]
3657윤용한12/1/201521150    답변글 [답변]: ds:Signature 질문입니다. [1]
3651노태현11/20/201551936MariaDB - ASP.NET오류의 원인조차 못 찾고 있습니다.. [2]파일 다운로드2
3652노태현11/20/201519729    답변글 [답변]: MariaDB - ASP.NET오류의 원인조차 못 찾고 있습니다.. [3]
3649kokon11/17/201519108예제 파일 실행이 안 되네요 [5]
3647Sang...11/15/201517518Part 3 목차? [5]
3646힘찬도약11/13/201522914c# mscorlib System.IO IOException [8]파일 다운로드2
3644힘찬도약11/11/201521724c# user.config파일 [2]
3645spow...11/13/201518352    답변글 [답변]: c# user.config파일 - Json.NET을 이용한 설정파일 처리 [1]파일 다운로드1
3643힘찬도약11/11/201520149C# 함수의 processing time과 재호출 [14]
3642.net11/10/201519338c# 으로 작성된 com+ 에 대한 문제입니다. [2]
3641힘참도약11/9/201519123c# log file 관련해서 질문드립니다. [5]
... 46  47  [48]  49  50  51  52  53  54  55  56  57  58  59  60  ...