How to pack a byte in q/kdb

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?

See http://code.kx.com/wiki/Reference/vs and http://code.kx.com/wiki/Reference/Datatypes
“f X is 0x0 and Y is a number, it returns the internal representation of Y, with each byte in hex”

e.g. 

q)0x00 vs 1h   / 2 bytes (short)
0x0001
q)0x00 vs 1i   / 4 bytes (int)
0x00000001

So in your example, if I use 0x03 for the order type (Limit):

q)payload:0N!(“x”$“T”; 0x00; 0x03; 0x00 vs -10i)    / “T” as byte; null-terminator; 0x03 Limit type; -10 as 4 byte int 
(0x54;0x00;0x03;0xfffffff6)
q)payload:0N!raze payload    / collapse into single byte array
0x540003fffffff6
q)count payload
7

I think a combination of a `byte$ and 0x0 vs will do it:<o:p></o:p>

<o:p> </o:p>

q)symbol:“T”;limitOrderType:10i;size:-10i;<o:p></o:p>

q)show b:(`byte$(symbol;0;limitOrderType),0x0 vs size)<o:p></o:p>

0x54000afffffff6<o:p></o:p>

<o:p> </o:p>

or perhaps clearer using raze:     raze`byte$(symbol;0;limitOrderType;0x0 vs size)<o:p></o:p>

<o:p> </o:p>

Then to decode something like:<o:p></o:p>

q){(char$y#x;int$x y+1;0x0 sv (y+2)_x)}[b;b?0x0]<o:p></o:p>

,“T”<o:p></o:p>

10i<o:p></o:p>

-10i<o:p></o:p>

<o:p> </o:p>

<o:p> </o:p>

From: personal-kdbplus@googlegroups.com [mailto:personal-kdbplus@googlegroups.com] On Behalf Of rtrader
Sent: Monday, April 11, 2016 7:57 AM
To: Kdb+ Personal Developers <personal-kdbplus@googlegroups.com>
Subject: [personal kdb+] How to pack a byte in q/kdb<o:p></o:p>

<o:p> </o:p>

What I’m trying to do here is pack a byte like I could in c# like this:<o:p></o:p>

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?<o:p></o:p>


Submitted via Google Groups

You probably need this:

-9!-8!`somesym

or you can just drop the header of the -8! result and get something similar to what you do in c#.

WBR, Andrey.

???, 11 ??? 2016 ?., 17:57:53 UTC+3 ??? rtrader ???:

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?