1

What I'm trying to do here is pack a byte like I could in c# like this:

string symbol = "T" + "\0";
byte orderTypeEnum = (byte)OrderType.Limit;
int size = -10;

byte[] packed = new byte[symbol.Length + sizeof(byte) + sizeof(int)]; // byte = 1, int = 4
Encoding.UTF8.GetBytes(symbol, 0, symbol.Length, packed, 0); // add the symbol
packed[symbol.Length] = orderTypeEnum; // add order type
Array.ConstrainedCopy(BitConverter.GetBytes(size), 0, packed, symbol.Length + 1, sizeof(int)); // add size

client.Send(packed);

Is there any way to accomplish this in q? As for the Unpacking in C# I can easily do this:

    byte[] fillData = client.Receive();
    long ticks = BitConverter.ToInt64(fillData, 0);
    int fillSize = BitConverter.ToInt32(fillData, 8);
    double fillPrice = BitConverter.ToDouble(fillData, 12);

    new 
    {
        Timestamp = ticks,
        Size = fillSize,
        Price = fillPrice
    }.Dump("Received response");

Thanks!

Roman Byshko
  • 8,591
  • 7
  • 35
  • 57
Rtrader
  • 917
  • 4
  • 11
  • 26

1 Answers1

2

One way to do it is

symbol:"T\000"
orderTypeEnum: 123 / (byte)OrderType.Limit
size: -10i;
packed: "x"$symbol,("c"$orderTypeEnum),reverse 0x0 vs size / *

UPDATE:

To do the reverse you can use 1: function:

(8 4 8; "jif")1:0x0000000000000400000008003ff3be76c8b43958 / server data is big-endian
("jif"; 8 4 8)1:0x0000000000000400000008003ff3be76c8b43958 / server data is little-endian
/ ticks=1024j, fillSize=2048i, fillPrice=1.234

*) When using BitConverter.GetBytes() you should also check the value of BitConverter.IsLittleEndian to make sure you send bytes over the wire in a proper order. Contrary to popular belief .NET is not always little-endian. Hovewer, an internal representation in kdb+ (a value returned by 0x0 vs ...) is always big-endian. Depending on your needs you may or may not want to use reverse above.

Igor Korkhov
  • 8,283
  • 1
  • 26
  • 31
  • Thanks! that worked perfectly, however would you know how to do the reverse as well? I have edited the question to reflect that as well – Rtrader Apr 11 '16 at 19:29