Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사],
조회: 11253
글쓴 사람
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)
3667김치사발면12/15/201510792템플릿 설정 추가 질문 [1]파일 다운로드1
3666노태현12/15/201510204사용자별로 권한을 주고 볼 수 있는 데이터를 제한하려면 어떤 방법으로 접근하는게 좋을까요? [2]
3665김치사발면12/14/201513296C# 템플릿 설정이 이상해요ㅜㅜ [3]파일 다운로드1
3664KuLu12/10/201510590멀정하던 사이트 중 일부메뉴가 응답없음이 되어버립니다. [3]
3663Sung...12/10/201511986UWP 서버 프로그래밍 작업 중 [5]
3662Sung...12/9/201511822uwp앱에서 접속한 클라이언트 주소값을 받아서 표시하고 싶은데 어떤 메소드를 써야 할까요? [7]
3661김무진12/9/201511183Oracle 환경에서 데이터를 조회할때 한글이 ? 이렇게 표시가됩니다. [1]
3660질문자12/7/201513114OpenFileDialog 호출시 hang걸리는 문제 [4]
3659Sung...12/4/201512743UWP 앱에서 textBox로 클라에서 받은 값을 나타내고 싶은데 안되고 있습니다. [5]
3658강준12/3/201511990Visual Studio (Xamarin) vs Eclipse [2]
3656DEVY...12/1/201510894MasterPageFile 사용시 다국어 처리 질문입니다. [1]
3653DEVY...11/26/201512345ds:Signature 질문입니다. [7]
3654윤용한11/27/201512098    답변글 [답변]: ds:Signature 질문입니다. [3]
3657윤용한12/1/201514370    답변글 [답변]: ds:Signature 질문입니다. [1]
3651노태현11/20/201545089MariaDB - ASP.NET오류의 원인조차 못 찾고 있습니다.. [2]파일 다운로드2
3652노태현11/20/201511960    답변글 [답변]: MariaDB - ASP.NET오류의 원인조차 못 찾고 있습니다.. [3]
3649kokon11/17/201512365예제 파일 실행이 안 되네요 [5]
3647Sang...11/15/201510855Part 3 목차? [5]
3646힘찬도약11/13/201515705c# mscorlib System.IO IOException [8]파일 다운로드2
3644힘찬도약11/11/201514837c# user.config파일 [2]
3645spow...11/13/201511827    답변글 [답변]: c# user.config파일 - Json.NET을 이용한 설정파일 처리 [1]파일 다운로드1
3643힘찬도약11/11/201513643C# 함수의 processing time과 재호출 [14]
3642.net11/10/201512318c# 으로 작성된 com+ 에 대한 문제입니다. [2]
3641힘참도약11/9/201512743c# log file 관련해서 질문드립니다. [5]
3638윤창선11/4/201513320사설IP가 부여된 무선라우터간 영상전송 관련 문의 [8]
3634Hyun...11/2/201510898c# 에서 webkit browser에서 webgl을 이용하는 사이트에 접속이 안됩니다. [1]
... 46  [47]  48  49  50  51  52  53  54  55  56  57  58  59  60  ...