Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사],
조회: 17596
글쓴 사람
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]

... 91  92  93  94  95  [96]  97 
NoWriterDateCnt.TitleFile(s)
54차광석8/26/200411512잘 지내시죠?
55정성태8/27/200410629    답변글 [답변]: 잘 지내시죠?
52김명원8/3/200411422질문이요..
53정성태8/19/200411293    답변글 [답변]: 질문이요..
50김상표7/5/200411090질문요...
51정성태7/6/200410923    답변글 [답변]: 질문요...
45송야5/29/200411063웹페이지 자동로그인..
46정성태5/31/200411934    답변글 [답변]: 웹페이지 자동로그인..
44반데라이차5/26/200411114안녕하세요~저 오크에요~~^^* [1]
43이윤복4/22/20049924윤보기 입니다.
48정성태5/31/200411310    답변글 [답변]: 윤보기 입니다.
40김대웅2/26/200412304메일 드렸는데.. 읽어보셨는지 모르겠네요.. 많이 바쁘신것 같아요..
41정성태2/29/200411281    답변글 [답변]: 메일 드렸는데.. 읽어보셨는지 모르겠네요.. 많이 바쁘신것 같아요..
42정성태3/6/200412487    답변글 [답변]: 메일로 답변을 드렸는데. 메일이 자꾸 반송되네요. 여기서 답변드립니다.
38문우영1/19/200410934ㅋㅋ 한글 아이디도 되는군요.!!
39정성태1/26/200411541    답변글 [답변]: 다행이네요. ^^;
33신종화1/9/200412345성태님 오랬만이군요 ^^ 새해복 많이 받으세요
35정성태1/17/200410525    답변글 [답변]: 성태님 오랬만이군요 ^^ 새해복 많이 받으세요
32김기영1/5/200411249질문인데요... 웹브라우저로 text를 전송할 수 있을까요??
34정성태1/17/200410798    답변글 [답변]: 질문인데요... 웹브라우저로 text를 전송할 수 있을까요??
28박소연10/30/200314732c언어를 이용한 가계부 프로그램
31정성태12/17/200316155    답변글 [답변]: c언어를 이용한 가계부 프로그램
27임영택9/17/200311318atl com에서 Window Handle 구하고 싶은데(바쁘시더라도 꼭 답변 해주세요!!)
30정성태12/17/200311731    답변글 [답변]: atl com에서 Window Handle 구하고 싶은데(바쁘시더라도 꼭 답변 해주세요!!)
26임영택9/15/200310821IE에서 submit 버튼을 누를때 발생하는 이벤트를 받을수 있나요?
29정성태12/17/200310836    답변글 [답변]: IE에서 submit 버튼을 누를때 발생하는 이벤트를 받을수 있나요?
... 91  92  93  94  95  [96]  97