How to convert a int to 4 byte (DWORD) hex values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Roger Cauvin

    #1

    How to convert a int to 4 byte (DWORD) hex values

    A graphics company requires that the byte count of a string I need to be build be included in the message sent to it. I am building the message like this..

    Code:
    string StartBytes = "" + (char)STX + (char)0x00 + (char)0x00 + (char)0x00 + (char)TotalByteCount + (char)Command;
    string EndBytes = "" + (char)0x00 + (char)0x00 + (char)0x00 + (char)TotalByteCount + (char)ETX;
    
    string SendMsg = StartBytes.Trim() + ObjectList.Trim() + EndBytes.Trim();
    The four chars after the STX needs to represent the total byte count.
    In documentation, they show this as ...

    BC (Byte Count): Packet total size including Start Code and End Code
    Transmit in DWORD (4byte) type

    Ex) Packet size = 0x00001234
    0x00
    0x00
    0x12
    0x34

    I am building this all as a string and using the following to convert to bytes..
    Code:
    byte[] bytesSendMsg = new byte[SendMsg.Length];
    bytesSendMsg = ASCIIEncoding.ASCII.GetBytes(SendMsg);
    SendMsg is my string that I build...

    Appreciate any help any one can provide.
    Last edited by Frinavale; Oct 7 '10, 07:17 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags and formatted the question so that it is easier to read and understand.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    What exactly is the problem?
    Are you experiencing errors?
    Is the output not as you expect it?

    -Frinny

    Comment

    Working...