Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사],
조회: 19055
글쓴 사람
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)
3647Sang...11/15/201518586Part 3 목차? [5]
3646힘찬도약11/13/201524342c# mscorlib System.IO IOException [8]파일 다운로드2
3644힘찬도약11/11/201523112c# user.config파일 [2]
3645spow...11/13/201519571    답변글 [답변]: c# user.config파일 - Json.NET을 이용한 설정파일 처리 [1]파일 다운로드1
3643힘찬도약11/11/201521076C# 함수의 processing time과 재호출 [14]
3642.net11/10/201520398c# 으로 작성된 com+ 에 대한 문제입니다. [2]
3641힘참도약11/9/201520086c# log file 관련해서 질문드립니다. [5]
3638윤창선11/4/201522590사설IP가 부여된 무선라우터간 영상전송 관련 문의 [8]
3634Hyun...11/2/201519324c# 에서 webkit browser에서 webgl을 이용하는 사이트에 접속이 안됩니다. [1]
3633힘찬도약10/31/201519575mysql insert where not exists [6]
3632힘찬도약10/27/201519426C# Lock 관련해서 질문드립니다. [6]
3655iwc11/30/201517601    답변글 [답변]: C# Lock 관련해서 질문드립니다.
3631강준10/26/201521523iis 8.5 preload 기능에 대해 질문이 있습니다. [9]
3630김정훈10/25/201520006몬티홀 게임 관련 질문 [1]
3629pooq10/23/201520262리플렉션 관련해서 질문 입니다. [3]
3628최영민10/22/201518487스마트 클라이언트 로딩속도 문의입니다. [3]
3627양주호10/22/201518335C#으로 컨버팅 하려고 하는데요... [1]
3626조성진10/21/201519212책보고 첫번째 예제부터 문제가 생기네요 ^^; [4]파일 다운로드1
3623Bere...10/19/201520070질문이라기 보단... [2]
3625Bere...10/20/201518605    답변글 [답변]: 질문이라기 보단... [2]파일 다운로드1
3621힘찬도약10/18/201518890[C# 6.0]multi threading과 ui control [9]
3624힘찬도약10/19/201519070    답변글 [답변]: [C# 6.0]multi threading과 ui control [6]파일 다운로드1
3620popo10/13/201517501WPF의 datagrid, listview 컨트롤 관련 질문 입니다. [1]
3619링크의 ...10/12/201522300OCX 로드 관련 질문입니다. [5]파일 다운로드1
3616수요일밥...10/7/201523517몇 가지 오류 (2) [6]
3615김응규10/7/201517957다시한번 질문 드립니다. (이번엔 자세하게 기술했습니다.) [1]
... 46  47  48  [49]  50  51  52  53  54  55  56  57  58  59  60  ...