# Binary encode 7 (\[7\].pack("C")) as "\\007" instead of "\\a"

**URL:** <https://www.ruby-forum.com/t/binary-encode-7-7-pack-c-as-007-instead-of-a/192428>\
**Category:** Ruby\
**Created:** [July 30, 2010, 4:03am UTC](https://www.ruby-forum.com/t/binary-encode-7-7-pack-c-as-007-instead-of-a/192428 "2010-07-30T04:03:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![woahdae](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/51bf81/32.png) [@woahdae](https://www.ruby-forum.com/u/woahdae)\
**Post date:** [July 30, 2010, 4:03am UTC](https://www.ruby-forum.com/t/binary-encode-7-7-pack-c-as-007-instead-of-a/192428/1 "2010-07-30T04:03:14Z")

</div>

I have a problem that’s on the borders of ruby and erlang, but I’m  
beginning to think the solution won’t be from the erlang side of things.  
(and forgive me for my haziness with the terminology here…)

The basic problem is that ruby encodes the integer 7 as “\a”, while  
erlang only decodes it as “\007”. Ruby _will_ decode both “\007” and  
“\a” into 7, which makes me think there’s more than one opinion about  
how it can be encoded, and that ruby might be able to encode it as  
“\007” if I knew how to ask it.

The bigger problem is I’m trying to send data from ruby to erlang via  
BERT, and turns out it can’t handle data (broadly defined) of length 7  
(!).

Here’s some code examples:

ruby:

[7].pack(“C”) # =\> “\a”  
“\a”.unpack(“C”) # =\> 7  
“\007”.unpack(“C”) # =\> 7

erlang:

\<\<“\a”\>\>. % =\> \<\>  
\<\<“\007”\>\>. % =\> \<\<7\>\>

 

Not sure exactly what to google here, “erlang OR  
ruby binary 007” doesn’t really get me anywhere (suprise!), plus I’m  
not sure what exactly this encoding/decoding specification is called.  
I’d really like to understand why the design discrepancy between the  
languages, but I’d settle for a quick fix on the ruby side 🙂

 

Any help is appreciated,

 

-Woody

 

(PS, more details are at  
 [Issues · mojombo/bert.erl · GitHub](http://github.com/mojombo/bert.erl/issues#issue/3), although examples are  
bert-specific)

---

<div class="post-metadata">

**Author:** ![woahdae](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/51bf81/32.png) [@woahdae](https://www.ruby-forum.com/u/woahdae)\
**Post date:** [July 30, 2010, 7:38pm UTC](https://www.ruby-forum.com/t/binary-encode-7-7-pack-c-as-007-instead-of-a/192428/2 "2010-07-30T19:38:45Z")

</div>

> The basic problem is that ruby encodes the integer 7 as “\a”, while  
> erlang only decodes it as “\007”

Correction, this is actually a non-issue. The basic problem is that I  
was encoding binary data to its C escape sequences in my test (via  
to\_s), and erlang will automatically decode this back to binary, except  
for the number 7. Thus it _appeared_ like it was almost working, when in  
fact I was just doing it wrong. Sending the binary, not the escape  
sequences, is what I should have been doing. Apologies for the extra  
email, maybe this will save some poor confused soul someday…

---

<div class="post-metadata">

**Author:** ![woahdae](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/51bf81/32.png) [@woahdae](https://www.ruby-forum.com/u/woahdae)\
**Post date:** [July 30, 2010, 10:02pm UTC](https://www.ruby-forum.com/t/binary-encode-7-7-pack-c-as-007-instead-of-a/192428/3 "2010-07-30T22:02:16Z")

</div>

On Jul 30, 2010, at 10:38 , Woody P. wrote:

> > The basic problem is that ruby encodes the integer 7 as “\a”, while  
> > erlang only decodes it as “\007”
> 
> Correction, this is actually a non-issue.

Well, I think it was a non-issue regardless of what erlang is doing:

> > “\a” == “\007”  
> > =\> true
> 
> > “\007”[0]  
> > =\> 7
> 
> > “\a”[0]  
> > =\> 7

I’d guess you should be writing tests to verify instead of eyeballing  
it.

---

<div class="post-metadata">

**Author:** ![woahdae](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/51bf81/32.png) [@woahdae](https://www.ruby-forum.com/u/woahdae)\
**Post date:** [July 30, 2010, 11:26pm UTC](https://www.ruby-forum.com/t/binary-encode-7-7-pack-c-as-007-instead-of-a/192428/4 "2010-07-30T23:26:30Z")

</div>

> > > “\a” == “\007”  
> > > I’d guess you should be writing tests to verify instead of eyeballing  
> > > it.

In Ruby “\a” == “\007”, but when my integration test (incorrectly)  
converted the binary into escape sequences before sending it to erlang,  
_then_, in erlang, \<\<"\a"\>\> =/= \<\<"\007"\>\> (not-equal, and I have  
learned this is because Erlang uses the basic C set of character  
encodings, while Ruby uses the ANSI C encodings, which differ in at  
least this respect). So in my tests erlang -\> ruby would work, but ruby  
-\> erlang wouldn’t work, and tests confirmed this to be due to character  
encoding differences (how I knew to ask the question). Those differences  
are verifiable, although the whole point is moot (and I feel stupid for  
asking because I should have been testing via binary in the first place  
😕
