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

... 61  62  63  64  65  66  67  68  69  [70]  71  72  73  74  75  ...
NoWriterDateCnt.TitleFile(s)
785정성우12/16/200811650Vista 환경에서 VB6로 개발한 어플리케이션이 IE 통해서는 런칭이 안됩니다.. [4]
783서광원11/20/200812377.NET 어셈블리를 COM에서 사용하도록 변경 했을때 배포는 어떻게 해야 하나요? [3]
782김용환10/14/200812713웹서비스에서 캐시를 사용하고 싶습니다. [3]
781이수정9/5/200810949visual studio 2008 관련 질문입니다. [2]
776신현창8/5/200811255VS2005로 실행시 갑자기 COM+ 등록이 안됩니다. [1]
775땡초7/21/200811480조언을 구하고저 합니다. [2]
774남호진7/19/200811853Visual Studio 2005나 2008에서 서버탐색기에연결된 데이터에서 정보를 끌어... [2]
773지워니7/17/200814089웹서비스로 db2의 데이터가 오질 않아요. [1]
984이영구7/16/201114063    답변글 [답변]: 웹서비스로 db2의 데이터가 오질 않아요.
772다자래7/15/200811366스레드와 이벤트를 이용해서 mssql 서버의 저장프로시져의 레코드 진행률을 표현하는 프로그래스바를 구현하려고하는데....막막 [1]
771guest6/26/200811176[WCF] Self host 만드는 방법. [2]파일 다운로드1
770조조5/27/200810922스마트클라이언트에서 stand-alone 형태를 임베디드형태로 변환시 문제.. [1]
769제영한5/20/200810930배포프로젝트에 대한 조언을 구합니다. [2]
767김형중5/6/200811991RMclock 관련 문의 입니다. [1]
766정성태4/28/200812631[데브피아 Q&A 모음] 2008-04-21 ~ 2008-04-24
764정성태4/21/200813453[데브피아 Q&A 모음] 2008-04-14 ~ 2008-04-18 [2]
763단테4/17/200811171COM 객체 생성하는 방법
765정성태4/21/200811895    답변글 [답변]: CreateInstance를 부르시는 것이 추천됩니다.
762이민지4/16/200814933ClickOnce 실행 시 Internet Explorer 7.0에서 에러 메세지 출력 [1]
761이민지4/16/200811429ClickOnce로 배포를 하다가 생긴 문제에 대해 질문드립니다. [1]
760한귀순4/15/200812186DataSet 의 designer.cs [2]
759정성태4/14/200812434[데브피아 Q&A 모음] 2008-04-04 ~ 2008-04-12
7573/28/200899922008 Server, Vista 에서 RDS 지원? [4]
756손승휘3/24/200813711Microsoft Web Browser OCX 부분이 안전하지 않는 ACTIVEX로 IE에서 인식되어는 점 [1]
754단테3/19/200810984실행파일의 아이콘을 프로그램 적으로 변경할 수 있을까요? [1]
753신동열3/12/200811419비스타의 권한 상승 관련 질문 있습니다. [2]
... 61  62  63  64  65  66  67  68  69  [70]  71  72  73  74  75  ...