C# binary encoding

I want to prepare an order package for the parameters in the 2nd picture according to the data types in the 1st picture, but I could not set the rules correctly, I could not make the correct conversion and arrangement. How do I set up bundle creation correctly?

According to this code, when I transmit the package to the server, the server closes the connection and does not return a response because I set the package incorrectly.

1. Picture
2. Picture

Some fix values:

  • Order Token: 101
  • Order book ID: 3437892
  • Side girin (B/S/T): B
  • Quantity girin: 1
  • Price girin (Decimal formatında): 2.9
  • Client-Account girin: DE-102001
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>private static void SendOrder()
{
try
{
Console.WriteLine("Order Token girin:");
string orderToken = Console.ReadLine().PadRight(14);
Console.WriteLine("Order book ID girin:");
int orderBookId = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Side girin (B/S/T):");
string side = Console.ReadLine();
Console.WriteLine("Quantity girin:");
int quantity = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Price girin (Decimal formatında):");
decimal price = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("Client-Account girin:");
string clientAccount = Console.ReadLine().PadRight(16);
// Diğer sabit alanlar
string customerInfo = "".PadRight(15);
string exchangeInfo = "".PadRight(32);
byte openClose = 0; // 0 = Default for the account
// Veriyi baytlara dönüştürme
byte[] orderTokenBytes = Encoding.ASCII.GetBytes(orderToken);
byte[] orderBookIdBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(orderBookId));
byte[] quantityBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(quantity));
byte[] priceBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(Convert.ToInt32(price * 10000))); // Fiyatı 4 bayt olarak ayarlama
byte[] displayQuantityBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(0L)); // 8 baytlı ve 0 olarak ayarlama
// Paket oluşturma
byte[] orderPacketBytes = new byte[114];
orderPacketBytes[0] = (byte)'O';
orderTokenBytes.CopyTo(orderPacketBytes, 1);
Array.Copy(orderBookIdBytes, 0, orderPacketBytes, 15, 4); // 4 baytlık Order book ID
orderPacketBytes[19] = (byte)side[0];
Array.Copy(quantityBytes, 0, orderPacketBytes, 20, 4); // 8 baytlık Quantity
Array.Copy(priceBytes, 0, orderPacketBytes, 28, 4); // 4 baytlık Price
orderPacketBytes[32] = (byte)'0'; // Time In Force (0 = Day)
orderPacketBytes[33] = openClose; // Open Close (0 = Default)
Encoding.ASCII.GetBytes(clientAccount).CopyTo(orderPacketBytes, 34);
Encoding.ASCII.GetBytes(customerInfo).CopyTo(orderPacketBytes, 50);
Encoding.ASCII.GetBytes(exchangeInfo).CopyTo(orderPacketBytes, 65);
displayQuantityBytes.CopyTo(orderPacketBytes, 97); // Display Quantity
orderPacketBytes[105] = (byte)'1'; // Client Category (1 = Client)
orderPacketBytes[106] = (byte)'0'; // OffHours (0 = Normal hours)
Encoding.ASCII.GetBytes("".PadRight(7)).CopyTo(orderPacketBytes, 107); // Reserved
byte[] packet = CreateOrderPacket(orderPacketBytes);
_networkStream.Write(packet, 0, packet.Length);
LogMessage("Sipariş girme isteği gönderildi.");
LogMessage("Giden: " + BitConverter.ToString(packet));
LogMessage("Giden (Metin): " + Encoding.ASCII.GetString(packet));
Task.Run(() => BeginReadingOrderResponse(_cancellationTokenSource.Token));
}
catch (Exception ex)
{
LogMessage("Sipariş girme isteği gönderilirken hata: " + ex.Message);
}
}
private static byte[] CreateOrderPacket(byte[] messageBytes)
{
byte[] lengthBytes = BitConverter.GetBytes((ushort)(messageBytes.Length + 1));
if (BitConverter.IsLittleEndian)
Array.Reverse(lengthBytes);
byte[] packet = new byte[lengthBytes.Length + messageBytes.Length + 1]; // Length + Type + Payload
lengthBytes.CopyTo(packet, 0);
messageBytes.CopyTo(packet, lengthBytes.Length);
packet[packet.Length - 1] = (byte)'n'; // Terminating newline for SoupBinTCP
return packet;
}
private static async Task BeginReadingOrderResponse(CancellationToken token)
{
try
{
byte[] buffer = new byte[4096];
while (_isConnected && !token.IsCancellationRequested)
{
int bytesRead = await _networkStream.ReadAsync(buffer, 0, buffer.Length, token);
if (bytesRead == 0)
{
LogMessage("Sunucu bağlantıyı kesti.");
Disconnect();
break;
}
ProcessOrderPacket(buffer, bytesRead);
}
}
catch (OperationCanceledException)
{
LogMessage("Okuma işlemi iptal edildi.");
}
catch (Exception ex)
{
LogMessage("Yanıt okunurken hata: " + ex.Message);
Disconnect();
}
}
</code>
<code>private static void SendOrder() { try { Console.WriteLine("Order Token girin:"); string orderToken = Console.ReadLine().PadRight(14); Console.WriteLine("Order book ID girin:"); int orderBookId = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Side girin (B/S/T):"); string side = Console.ReadLine(); Console.WriteLine("Quantity girin:"); int quantity = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Price girin (Decimal formatında):"); decimal price = Convert.ToDecimal(Console.ReadLine()); Console.WriteLine("Client-Account girin:"); string clientAccount = Console.ReadLine().PadRight(16); // Diğer sabit alanlar string customerInfo = "".PadRight(15); string exchangeInfo = "".PadRight(32); byte openClose = 0; // 0 = Default for the account // Veriyi baytlara dönüştürme byte[] orderTokenBytes = Encoding.ASCII.GetBytes(orderToken); byte[] orderBookIdBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(orderBookId)); byte[] quantityBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(quantity)); byte[] priceBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(Convert.ToInt32(price * 10000))); // Fiyatı 4 bayt olarak ayarlama byte[] displayQuantityBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(0L)); // 8 baytlı ve 0 olarak ayarlama // Paket oluşturma byte[] orderPacketBytes = new byte[114]; orderPacketBytes[0] = (byte)'O'; orderTokenBytes.CopyTo(orderPacketBytes, 1); Array.Copy(orderBookIdBytes, 0, orderPacketBytes, 15, 4); // 4 baytlık Order book ID orderPacketBytes[19] = (byte)side[0]; Array.Copy(quantityBytes, 0, orderPacketBytes, 20, 4); // 8 baytlık Quantity Array.Copy(priceBytes, 0, orderPacketBytes, 28, 4); // 4 baytlık Price orderPacketBytes[32] = (byte)'0'; // Time In Force (0 = Day) orderPacketBytes[33] = openClose; // Open Close (0 = Default) Encoding.ASCII.GetBytes(clientAccount).CopyTo(orderPacketBytes, 34); Encoding.ASCII.GetBytes(customerInfo).CopyTo(orderPacketBytes, 50); Encoding.ASCII.GetBytes(exchangeInfo).CopyTo(orderPacketBytes, 65); displayQuantityBytes.CopyTo(orderPacketBytes, 97); // Display Quantity orderPacketBytes[105] = (byte)'1'; // Client Category (1 = Client) orderPacketBytes[106] = (byte)'0'; // OffHours (0 = Normal hours) Encoding.ASCII.GetBytes("".PadRight(7)).CopyTo(orderPacketBytes, 107); // Reserved byte[] packet = CreateOrderPacket(orderPacketBytes); _networkStream.Write(packet, 0, packet.Length); LogMessage("Sipariş girme isteği gönderildi."); LogMessage("Giden: " + BitConverter.ToString(packet)); LogMessage("Giden (Metin): " + Encoding.ASCII.GetString(packet)); Task.Run(() => BeginReadingOrderResponse(_cancellationTokenSource.Token)); } catch (Exception ex) { LogMessage("Sipariş girme isteği gönderilirken hata: " + ex.Message); } } private static byte[] CreateOrderPacket(byte[] messageBytes) { byte[] lengthBytes = BitConverter.GetBytes((ushort)(messageBytes.Length + 1)); if (BitConverter.IsLittleEndian) Array.Reverse(lengthBytes); byte[] packet = new byte[lengthBytes.Length + messageBytes.Length + 1]; // Length + Type + Payload lengthBytes.CopyTo(packet, 0); messageBytes.CopyTo(packet, lengthBytes.Length); packet[packet.Length - 1] = (byte)'n'; // Terminating newline for SoupBinTCP return packet; } private static async Task BeginReadingOrderResponse(CancellationToken token) { try { byte[] buffer = new byte[4096]; while (_isConnected && !token.IsCancellationRequested) { int bytesRead = await _networkStream.ReadAsync(buffer, 0, buffer.Length, token); if (bytesRead == 0) { LogMessage("Sunucu bağlantıyı kesti."); Disconnect(); break; } ProcessOrderPacket(buffer, bytesRead); } } catch (OperationCanceledException) { LogMessage("Okuma işlemi iptal edildi."); } catch (Exception ex) { LogMessage("Yanıt okunurken hata: " + ex.Message); Disconnect(); } } </code>
private static void SendOrder()
{
    try
    {
        Console.WriteLine("Order Token girin:");
        string orderToken = Console.ReadLine().PadRight(14);

        Console.WriteLine("Order book ID girin:");
        int orderBookId = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Side girin (B/S/T):");
        string side = Console.ReadLine();

        Console.WriteLine("Quantity girin:");
        int quantity = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Price girin (Decimal formatında):");
        decimal price = Convert.ToDecimal(Console.ReadLine());

        Console.WriteLine("Client-Account girin:");
        string clientAccount = Console.ReadLine().PadRight(16);

        // Diğer sabit alanlar
        string customerInfo = "".PadRight(15);
        string exchangeInfo = "".PadRight(32);
        byte openClose = 0; // 0 = Default for the account

        // Veriyi baytlara dönüştürme
        byte[] orderTokenBytes = Encoding.ASCII.GetBytes(orderToken);
        byte[] orderBookIdBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(orderBookId));
        byte[] quantityBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(quantity));
        byte[] priceBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(Convert.ToInt32(price * 10000))); // Fiyatı 4 bayt olarak ayarlama
        byte[] displayQuantityBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(0L)); // 8 baytlı ve 0 olarak ayarlama

        // Paket oluşturma
        byte[] orderPacketBytes = new byte[114];
        orderPacketBytes[0] = (byte)'O';
        orderTokenBytes.CopyTo(orderPacketBytes, 1);
        Array.Copy(orderBookIdBytes, 0, orderPacketBytes, 15, 4); // 4 baytlık Order book ID
        orderPacketBytes[19] = (byte)side[0];
        Array.Copy(quantityBytes, 0, orderPacketBytes, 20, 4); // 8 baytlık Quantity
        Array.Copy(priceBytes, 0, orderPacketBytes, 28, 4); // 4 baytlık Price
        orderPacketBytes[32] = (byte)'0'; // Time In Force (0 = Day)
        orderPacketBytes[33] = openClose; // Open Close (0 = Default)
        Encoding.ASCII.GetBytes(clientAccount).CopyTo(orderPacketBytes, 34);
        Encoding.ASCII.GetBytes(customerInfo).CopyTo(orderPacketBytes, 50);
        Encoding.ASCII.GetBytes(exchangeInfo).CopyTo(orderPacketBytes, 65);
        displayQuantityBytes.CopyTo(orderPacketBytes, 97); // Display Quantity
        orderPacketBytes[105] = (byte)'1'; // Client Category (1 = Client)
        orderPacketBytes[106] = (byte)'0'; // OffHours (0 = Normal hours)
        Encoding.ASCII.GetBytes("".PadRight(7)).CopyTo(orderPacketBytes, 107); // Reserved

        byte[] packet = CreateOrderPacket(orderPacketBytes);

        _networkStream.Write(packet, 0, packet.Length);
        LogMessage("Sipariş girme isteği gönderildi.");
        LogMessage("Giden: " + BitConverter.ToString(packet));
        LogMessage("Giden (Metin): " + Encoding.ASCII.GetString(packet));

        Task.Run(() => BeginReadingOrderResponse(_cancellationTokenSource.Token));
    }
    catch (Exception ex)
    {
        LogMessage("Sipariş girme isteği gönderilirken hata: " + ex.Message);
    }
}

private static byte[] CreateOrderPacket(byte[] messageBytes)
{
    byte[] lengthBytes = BitConverter.GetBytes((ushort)(messageBytes.Length + 1));
    if (BitConverter.IsLittleEndian)
        Array.Reverse(lengthBytes);
    byte[] packet = new byte[lengthBytes.Length + messageBytes.Length + 1]; // Length + Type + Payload
    lengthBytes.CopyTo(packet, 0);
    messageBytes.CopyTo(packet, lengthBytes.Length);
    packet[packet.Length - 1] = (byte)'n'; // Terminating newline for SoupBinTCP
    return packet;
}

private static async Task BeginReadingOrderResponse(CancellationToken token)
{
    try
    {
        byte[] buffer = new byte[4096];
        while (_isConnected && !token.IsCancellationRequested)
        {
            int bytesRead = await _networkStream.ReadAsync(buffer, 0, buffer.Length, token);
            if (bytesRead == 0)
            {
                LogMessage("Sunucu bağlantıyı kesti.");
                Disconnect();
                break;
            }
            ProcessOrderPacket(buffer, bytesRead);
        }
    }
    catch (OperationCanceledException)
    {
        LogMessage("Okuma işlemi iptal edildi.");
    }
    catch (Exception ex)
    {
        LogMessage("Yanıt okunurken hata: " + ex.Message);
        Disconnect();
    }
}

seniro dev created the code but it didn’t work

New contributor

ysrkdl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật