Issue #1 closed

How to hexlify the serialized data?

liweijian
created an issue

Hi, I just read the post http://voidstarzero.ca/post/137911090363/getting-started-with-protocol-buffers-in-racket , and I know we could serialize and deserialize by both racket:

$ racket serialize.rkt | racket deserialize.rkt

I was wondering how could I serialize in Racket and deserialize in another language like python?

For example, in python

$ cat personinfo.proto
syntax = "proto2";
package personinfo;

message Person {
    required string name = 1;
}

And using python to serialize it

import personinfo_pb2
import binascii

person = personinfo_pb2.Person()
person.name = "Holy"
packed_data = person.SerializeToString()
print binascii.hexlify(packed_data)

Is there function like SerializeToString of python?

Comments (5)

  1. liweijian reporter

    En, but actually it just show Holy"", but not 0a04486f6c79 as expected.

    #lang racket
    (require (planet murphy/protobuf))
    (require "personinfo.rkt")
    (require file/sha1)
    
    (define p (person* #:name "Holy"))
    
    (bytes->hex-string
     (call-with-output-bytes
      (λ (out) 
        (serialize p))))
    

    With personinfo.rkt

    #lang racket/base
    ;; Generated using protoc-gen-racket v1.1
    (require (planet murphy/protobuf:1/syntax))
    
    (define-message-type person ((required primitive:string name 1)))
    
    (provide (all-defined-out))
    
  2. Thomas Chust repo owner

    Creating a byte string output port but not writing to that port is, of course, not very helpful.

    Scheme I/O procedures usually accept a port to operate on as an optional argument and use the standard input or output port if no port is given explicitly. serialize is no exception and here it should be used like this:

    (bytes->hex-string
     (call-with-output-bytes
      (λ (out) 
        (serialize p out))))
    

    Or like this:

    (bytes->hex-string
     (with-output-to-bytes
      (λ () 
        (serialize p))))
    

    Of course, spending a minute to actually read some basic documentation would have prevented this kind of mistake :-/

  3. Log in to comment