Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사],
조회: 17553
글쓴 사람
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)
594ygso...3/13/200714281ClickOnce를 이용하여 SmartClient를 배포할경우.. [2]
589김희택3/2/200713445리소스 추가 방법에 대한 아티클을 보던중에 질문요... [1]파일 다운로드1
588이성진2/24/200715702웹 -> 스마트클라이언트 -> 웹서비스 의 세션 공유 방법 ? [1]
583김영민2/22/200713890Vista에서 "관리자 권한으로 실행"을 통해 실행한 프로세스의 동작
584정성태2/22/200715377    답변글 [답변]: Vista에서 "관리자 권한으로 실행"을 통해 실행한 프로세스의 동작
582한귀순2/22/200713941sqlhelper 의 updatedataset
585정성태2/23/200713166    답변글 [답변]: sqlhelper 의 updatedataset [1]
579futu...2/16/200714666VS2005의 스마트 클라이언트에서 웹브라우저 예제 질문입니다. [1]
578정해봉2/16/200713894IE Embeded Assambly 방식에서 CAS 설정 방법 [1]
575박성민2/12/200714688COM에 데이터 보내기 질문입니다. [1]
571엔틱스2/7/200715406그냥... 질문은 아닙니다만... [2]
5682/6/200712628이런 오류 화면을 어떻게 찾아봐야 - 알아봐야 - 하는지요?파일 다운로드1
569정성태2/6/200714154    답변글 [답변]: 이런 오류 화면을 어떻게 찾아봐야 - 알아봐야 - 하는지요? [1]
570정성태2/6/200714026        답변글 [답변]: [답변]: 이런 오류 화면을 어떻게 찾아봐야 - 알아봐야 - 하는지요?
5732/8/200713021            답변글 [답변]: [답변]: [답변]: 이런 오류 화면을 어떻게 찾아봐야 - 알아봐야 - 하는지요? [1]파일 다운로드1
565한귀순2/5/200713584typed dataset 의 유용성
566정성태2/6/200715432    답변글 [답변]: typed dataset의 유용성 [1]
564정민영2/5/200713853혹시 이런 경우 보신적 있으신가 궁금합니다..^^; [2]
563창민이2/2/200713975Visual C++ COM Objects Returning Recordsets 사용에 대해.. [3]
562현석1/29/200713998C# 스마트응용장치에서 아이콘 움직이게하는거 질문요 ^^ [1]파일 다운로드1
559초보1/27/200715291급 질문 입니다. visual studio 자동 종료에 대한 질문입니다. [2]
558즈믄1/26/200714981.Net Framework v2.0에서 Winform의 Panel에 Excel파일 보여주기 [2]
556정재우1/26/200715066vista에서 smartclient의 System.Security.PermissionsRegistryPermission 에러 [1]
555dev....1/25/2007164562005 WebBrowser내에서 팝업 처리 문제 관련 질문입니다.
561정성태1/29/200719337    답변글 [답변]: 2005 WebBrowser 내에서 팝업 처리 문제 관련 질문입니다.
554sky1/23/200715126<급질문> interop 를 사용함에 있어 [2]
... 76  [77]  78  79  80  81  82  83  84  85  86  87  88  89  90  ...