2

I [think I] need to do a base 64 encoding in C# with a custom character set, but I can't figure out how.

Specifically, I'm trying use the Mixpanel HTTP spec - https://mixpanel.com/docs/api-documentation/http-specification-insert-data - from within a Unity3D project. Mixpanel says that it has a custom "flavor" of Base64 encoding, and indeed, when I encode the same JSON with a web service that lets me specify the character set (http://www.motobit.com/util/base64-decoder-encoder.asp), Mixpanel reports success.

(Thus, I know that the failure is in the encoding, not the data being encoded)

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Narfanator
  • 5,595
  • 3
  • 39
  • 71
  • It uses the same encoding as .NET. Maybe the padding, try this: http://stackoverflow.com/a/12963614/17034 – Hans Passant Feb 03 '13 at 00:18
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Feb 03 '13 at 01:30

2 Answers2

0

The example provided by the page for a URL does not match the example for a JSON structure.

Here is a working example:

var json = "{\"event\": \"game\", \"properties\": {\"ip\": \"123.123.123.123\", \"token\": \"e3bb4100330c35722740fb8c6f5abddc\", \"time\": 1245613885, \"action\": \"play\"}}";

var result = Convert.ToBase64String(Encoding.UTF8.GetBytes(json));

// result == "eyJldmVudCI6ICJnYW1lIiwgInByb3BlcnRpZXMiOiB7ImlwIjogIjEyMy4xMjMuMTIzLjEyMyIsICJ0b2tlbiI6ICJlM2JiNDEwMDMzMGMzNTcyMjc0MGZiOGM2ZjVhYmRkYyIsICJ0aW1lIjogMTI0NTYxMzg4NSwgImFjdGlvbiI6ICJwbGF5In19"
dtb
  • 213,145
  • 36
  • 401
  • 431
-1

Since it's encoding to Base64, but being used for web, it's custom set is based on that. See: C# Byte[] to Url Friendly String

Edit: Perhaps not... it seems they allow '+' and '/'. Strange.

Community
  • 1
  • 1
Wally Young
  • 198
  • 1
  • 1
  • 10