Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사],
조회: 20521
글쓴 사람
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)
694이호정8/29/200717088[CardSpace 관련 질문] 정리해 좋으신 글 잘 보았습니다. [4]
693장근배8/29/200716558매핑 구간이 열려 있습니다 [3]
692채영사랑8/29/200716101<질문> Ms stress tool 로 부하테스트중. [2]
690한귀순8/28/200716994ClickOnce 배포 추가질문 [4]
689clev...8/27/200716095SmartClient에서도 Exe COM과 같이 여러곳에서 변수 공유가 가능한가요? [2]
688한귀순8/24/200718633ClickOnce 배포 [1]
686이상욱8/21/200717413[질문] VS.NET 2008 - WCF 를 위한 디버깅 환경 개선 테스트 중에
687정성태8/21/200715588    답변글 [답변]: [질문] VS.NET 2008 - WCF 를 위한 디버깅 환경 개선 테스트 중에
685김동진8/19/200718044Vista에서 WSDL관련 IIs7세팅 질문입니다. [4]
683조스7/31/200717327스마트 클라이언트에서 다른 어셈블리(.NET component) 사용시 에러 [1]
682정해봉7/30/200718362비스타에서 CAS설정하기... 소스 첨부 [1]파일 다운로드1
684정해봉8/14/200719379    답변글 [답변]: 비스타에서 CAS설정하기... 소스 첨부 [3]
681박희진7/27/200716948caspol로 설정된 권한집합 웹에서 읽어올 수 있나요?? [1]
680창민이7/27/200717747COM+등록시 에러 부분 때문에.. [2]
678정해봉7/24/200716625비스타에서 CAS 설정하기... [1]
677첫사랑7/24/200715667SSL에 관한 질문입니다.
679정성태7/26/200716353    답변글 [답변]: SSL에 관한 질문입니다. [1]
676kiuk7/11/200716688웹폐이지상의 이미지 상태 저장을 하고싶습니다. [1]
675안연준7/6/200717190안녕하세요 ^^ [1]
674황태욱7/5/200719402utf-8 환경에서 파라미터 값에 한글이 있을 경우 깨지는 현상이.. [1]
672땡초7/4/200718542Remoting 객체를 호출하는 순간 Timer 의 Elapsed 이벤트가 멈추는 현상 [2]
670조성택7/3/200717872.Net에서 Timer클래스.. [1]
667홍석헌6/29/200717034[c#] 윈도우 활성화에 관한 질문
669정성태6/30/200717203    답변글 [답변]: [c#] 윈도우 활성화에 관한 질문
666오야6/25/200716346System.Transactions 에서 트랜잭션 실패를 체크하는 방법 [1]
665김성태6/20/200715928안녕하세요^^
... 61  62  63  64  65  66  67  68  69  70  71  72  73  [74]  75  ...