Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사],
조회: 11456
글쓴 사람
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)
753신동열3/12/200811419비스타의 권한 상승 관련 질문 있습니다. [2]
751김재영3/6/200811082스레드와 비스레드간의 대기상태 순서에 대해 질문이 있습니다. [2]파일 다운로드1
752정성태3/7/200810594    답변글 [답변]: 스레드와 비스레드 간의 대기 상태 순서에 대해 질문이 있습니다. [1]
750김용현3/3/20089973스마트 클라이언트로 파일 다운로드시 파일의 크기가 0인 경우가 발생합니다. [2]
749임영한2/28/2008250652003서버에서 msxml3.dll 오류 "80070005" 문제입니다. [2]
748류재상2/27/200814598Remoting , Tcp Channel 사용시.. 외부에서 접근 보안문제... [3]
747한귀순2/20/200813012WinForm - DataGridView 소계 등 [3]
746박상영2/20/200810899리모팅 관련해서 [3]파일 다운로드1
745조승현2/20/200811766Response.Charset ANSI로 설정..하는부분이 어려워서요.. [1]
744오야2/5/200810210멤버십, 웹파트등의 생성테이블과 업무테이블간의 관계를 어떻게.. [2]
743김기룡1/29/200812285동일 이미지 판단 방법 관련 [2]파일 다운로드1
740경혜원1/28/200813765C#에서 이런게 가능 할런지요?
742정성태1/29/200811794    답변글 [답변]: C#에서 이런게 가능 할런지요? [1]
738임경훈1/25/200812635웹서비스에서 오라클DB에 연결할 때
741임경훈1/28/200812212    답변글 [답변]: 웹서비스에서 오라클DB에 연결할 때 [1]
737단테1/25/200813505안녕하세요 정성태님. C Run-time Library의 배포에 대해서 질문있습니다. [3]
736김현기1/24/200811467XML 접두어 포함한 XmlElement 작성법? [1]
735정명원1/9/200811291ActiveX 사용시 ... [2]
734제영한12/17/200711219웹서비스에서 사용자 정의 예외 사용하기 [2]
732김기룡12/3/200714105WebRequest 요청시 일본어 인코딩 관련 문의파일 다운로드1
733김기룡12/3/200710600    답변글 일어가 깨졌네요..테스트 문자 추가합니다.^^;;파일 다운로드1
739adnim1/27/200811658    답변글 [답변]: WebRequest 요청시 일본어 인코딩 관련 문의 [1]
731우기11/27/20079200[질문] clickonce 배포시 .net framework 2.0, 3.0 설치 시간 줄일 수 있는 묘수없을까요?? [1]
730김재영11/10/20079775스레드에 사용되는 함수에 값을 같이 넘기는 방법이 있습니까?? [2]파일 다운로드1
727제영한10/24/20079103시스템 설계에 대한 문의드립니다.
728정성태11/3/200710002    답변글 [답변]: 시스템 설계에 대한 문의드립니다. [2]
... 61  62  63  64  65  66  67  68  69  70  [71]  72  73  74  75  ...