开发者

C++ API writes Varint in different fashion to file than to socket

I want to ask because I find that strange: the way varint is written is depend on the target.

My simple code can write to a file or to a socket. When I write to the file the hexdump shows

0000000 02ac

0000002

When I write to the socket the 开发者_如何学编程C# client that reads byte by byte shows

ac 02

the code resposible for that is:

C++ app

  if(connect(fd, (sockaddr*)&addr, sizeof(addr))<0) {
    perror("połączenie nieudane");
    return -1;
    }

  //int fd = open("myfile", O_WRONLY | O_TRUNC);
   ZeroCopyOutputStream* raw_output = new FileOutputStream(fd);
   CodedOutputStream* coded_output = new CodedOutputStream(raw_output);

   coded_output->WriteVarint32(300);

   delete coded_output;
   delete raw_output;
   close(fd);

C# app

var str = client.GetStream();
while(str.DataAvailable) {
                b = (byte)str.ReadByte();
                Console.Write("{0:x2}  ", b);
            } 

I thought there should be no difference. I can't explain that to myself. Do you know what is up?


I'm guessing you're using hexdump (or od) to show the file contents, and that you don't actually have a problem ;-)

Demo:

$ echo -n ab > file
$ hexdump file
0000000 6261          # notice this is 'ba'
0000002
$ hexdump -C file
00000000  61 62                                             |ab|
00000002

Without options, hexdump will interpret the data in 16bit chunks, not byte by byte. Use the -C to get a per-8bit quantity output in order.

You wouldn't have this (display) problem on big-endian machine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜