Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사],
조회: 11459
글쓴 사람
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)
943김기룡1/3/201117341닷넷 에러시 조치사항관련... [2]
942김기룡12/27/201012295Thread 안정성 관련 문의 드립니다. [2]
941최광욱12/20/201011981정성태님 올리신 글중에 [1]
940최광욱12/20/201013581Assembly Unloading 관련해서 [2]
939최광욱12/20/201012616IIS 로그 읽기 [1]
938날쌘돌이12/14/201012810자바로 asp.net 인증하기 [3]
935김기룡12/13/201023004c#에서 c++로 개발된 dll에 byte[] 전달 관련하여 문의 드립니다. [6]
934임동찬12/7/201011234System.Reflection.Assembly.GetTypes() 메서드에 대해 [1]
929김준호12/2/201011459안녕하세요 비주얼베이직 2005 닷넷 관련 문의입니다 [1]
928김준호11/30/201012096안녕하세요 비주얼베이직 2005 닷넷 관련 문의입니다 [1]파일 다운로드1
927임동찬11/30/201013186Windows\\Temp 폴더의 이름모를 .tmp 파일들에 대해서 [1]
926이승규11/26/201032895IP접속 시도시 ORA-12504 에러 [1]
925임동찬11/11/201011063다른 프로그램의 컨트롤 건드려보기_추가질문(2) [1]
924임동찬11/10/201011987다른 프로그램의 컨트롤 건드려보기_추가질문 [1]
923임동찬11/9/201014401다른 프로그램의 컨트롤 건드려보기 [1]
922박태근11/2/201012940html5의 shape파일 관련 [1]파일 다운로드1
921박태근11/1/201013654DataTable 의 Binary변환! [1]
920김재영10/26/201013727GAC에 등록된 어셈블리를 Visual Studio에서 참조 대화상자에 보이게 할려면 어떤 방법이 있습니까? [2]
919임동찬10/22/201012530IStream [1]
918임동찬10/21/201011822System.Runtime.InteropServices.ComTypes.IStream 관련 [1]
917한귀순10/20/201015848IIS 최초 loading 시 속도 [2]
916임동찬10/15/201012214file lock 관련 [2]
915오병태10/11/201010922바쁘신대 답변 감사드립니다. [1]
914오병태10/11/201011015감사드립니다. 염치없지만 또 한번 문의드립니다. [2]
912오병태10/11/201011224윈도우즈 인증서 관련해서 문의를 드립니다.
913정성태10/11/201014251    답변글 [답변]: 윈도우즈 인증서 관련해서 문의를 드립니다.
... 61  62  63  64  [65]  66  67  68  69  70  71  72  73  74  75  ...