Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사],
조회: 11460
글쓴 사람
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)
850박한주12/31/200913357COM과 C#간 권한 문제에 관해 여쭤봅니다. [5]
848날쌘돌이12/22/200914226C#으로 개발하는 ActiveX 디버깅 문제 [5]
847최승문12/16/200913011winform에서 웹서비스를 이용할때, 객체가 전송안되는 문제... [2]
846이강구12/14/200912052ATL COM 제작 문의 [3]
845김재영12/9/200914745ClickOnce에서 WinForm을 배포시 변동되는 Args값을 넘길 수 있나요? [3]파일 다운로드1
843김재영12/2/200912470Hyper-V 사용에 대해여 질문이 있습니다. [2]
842한귀순12/1/200915390Attempted to read or write protected memory [2]
841장근배11/30/200916540첨부 파일 Download시 한글 Name [2]
844장근배12/3/200911411    답변글 [답변]: 첨부 파일 Download시 한글 Name파일 다운로드1
840박영민11/20/200911135웹 애플리케이션 질문입니다. [1]
837left...10/21/200912109아래 게시글 관련하여 추가 질문 [3]
836left...10/19/200913206ActiveX 상에서 암시적 dll 로드 [1]
832김우진10/15/200910177C# 에서 swa + security 구현 방법이 없을까요?
833정성태10/15/200913896    답변글 [답변]: C#에서 swa + security 구현 방법이 없을까요? [1]
834김우진10/15/200910283        답변글 예제 메시지 올려드릴께요. [2]파일 다운로드1
838이영우10/30/200910196            답변글 국세청 전자세금계선서에 관하여 문의드립니다.
831채승수10/13/200912342스마트 클라이언트 GAC 등록 문제 여쭤봅니다... [2]
829박찬용10/9/200924797WebBrowser 컨트롤 사용시 새창을 띄울시 세션/쿠키 공유가 왜 안될까요?? [1]파일 다운로드1
827송창훈9/24/200910032[질문]CAS 설정
828정성태9/28/200913301    답변글 [답변]: [질문]CAS 설정 [1]
825김재영9/18/200910706프레임워크별 비트 지원에 대해서 문의를 드립니다. [2]
821채승수8/27/200910328클릭원스를 비스타에서 실행시 [1]
822채승수8/28/200914959    답변글 [답변]: 클릭원스를 비스타에서 실행시 [2]
818개발돌이8/14/200912278ActiveX개발한 dll을 임베디드 할때 UI에 대한 질문 [1]파일 다운로드1
817채승수8/13/200911647클릭원스 관련 질문드립니다.~ [1]
816박진오7/29/200910739다국어 사이트의 컨텐츠 저장 방식에 대해.. [2]
... 61  62  63  64  65  66  67  [68]  69  70  71  72  73  74  75  ...