Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사],
조회: 19600
글쓴 사람
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]

... 76  77  78  79  80  81  [82]  83  84  85  86  87  88  89  90  ...
NoWriterDateCnt.TitleFile(s)
423이방은7/9/200615158리소스 임베디드 질문요.. [1]파일 다운로드1
422guest7/6/200614656COM+에서 풀링을 사용할 경우... [1]파일 다운로드1
424guest7/10/200615459    답변글 소스 코드 입니다. [1]
425guest7/11/200614628        답변글 다시 올립니다.파일 다운로드1
428정성태7/11/200614470            답변글 [답변]: 다시 올립니다.
421정보문7/6/200615189액티브X 설치 [1]
1354허재호11/12/201415785    답변글 [답변]: 액티브X 설치
418임대진7/4/200614375이럴적 있으세요?
419정성태7/4/200614474    답변글 [답변]: 이럴적 있으세요?
417이방은6/29/200614230[질문]2.0에서 웹리소스에 대해서 [2]
416박영일6/29/200614520CAS 설정여부 [1]
415김인학6/28/200615080스마트 클라이언트에서 웹서비스를 호출하고 있습니다. [1]
413김성호6/28/200614688IE프로그램문제... [2]
411이진형6/27/200615073어셈블리 네임 알아내기 [2]
414정성태6/28/200614042    답변글 [답변]: 어셈블리 네임 알아내기
410이재원6/26/200614214자바스크립트 스마트클라이언트 이벤트 호출 [3]
412이재원6/27/200614368    답변글 [답변]: 자바스크립트 스마트클라이언트 이벤트 호출
409정준명6/22/200615379윈폼 기반 스마트클라이언트 속도 문제에 대해서 의견을 구합니다. [2]
406이진형6/21/200615704처음에 스마트클라이언트 로딩하는 속도 [1]
4036/20/200614471Invoke 관련 문의 드립니다. [2]
4086/22/200613247    답변글 invoke 관련하여...
401김용국6/19/200614392SmartClient에서 동적 어셈블리 관련 문의 드립니다 [1]
402김용국6/20/200613407    답변글 [답변]: SmartClient에서 동적 어셈블리 관련 문의 드립니다 ... 다시 문의 드립니다! [1]
404김용국6/20/200613452        답변글 [답변]: [답변]: SmartClient에서 동적 어셈블리 관련 문의 드립니다 ... 다시 문의 드립니다! [1]
405김용국6/21/200614005            답변글 잘 되네요~~ ^^ 고맙습니다!
407김용국6/21/200613242                답변글 [답변]: 다시한번 문의드립니다!
... 76  77  78  79  80  81  [82]  83  84  85  86  87  88  89  90  ...