Thread: Passing an encoded string into a function to populate chars, uints etc

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    8

    Passing an encoded string into a function to populate chars, uints etc

    Hi Guys

    I have a bunch of encoded messages that I need to process. Whilst working on this in our test environment the messages where present in file. The file was binary, and I was reading the file a bit at a time using fread, and sticking the read bytes into structs. This was working great thanks to help from Salem and ZuK : )

    My problem is now that I need to process the same messages via a function but the messages are no longer in a file, they are coming into another process(a PERL program) that reads them of the network but cannot decode them. It can however pass them to a C function. My question is, where I was using fread before, what do I do now ?? If I pass them into the fucntion as a char array what then is the best way to deal with them ?? Here's my current attempt, which doesn't work :

    Code:
    char *decodeMsg(char *msg)
    {
        char messageType;
        char ProtoOrVer;
        uint16_t MsgLen;    
    
        sscanf(msg, "%c%c%hu", &messageType, &ProtoOrVer, &MsgLen);
        printf("messageType == %c\n", messageType);
        printf("ProtoOrVer == %c\n", ProtoOrVer);
        printf("MsgLen == %hu\n", MsgLen);
    Here's the output :

    Code:
    16:48:50 : 1265810793.133140 : J2t0D cEy]\ufffd\ufffd      0002900262O0NL0006445199NL0006275711
    
    received msg : J2
    messageType == J
    ProtoOrVer == 2
    MsgLen == 0
    Now I know that the MsgLen is actually 116 and is stored in a short unsigned int but sscanf isn't storing that in the unit "MsgLen".

    I'm a PERL programmer really, so sorry if this question sounds silly but I'm stuck !!

    Any pointers greatlly appreciated !!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What is the interface between the perl program and the C program - a Unix pipe?

    If the format is still the same "as if it were a file", you can always fread() from stdin in the C program if you wanted to.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by stevehicks View Post
    I'm a PERL programmer really, so sorry if this question sounds silly but I'm stuck !!
    Nb. that there is nothing you can do in C here that you cannot also do in perl using pack() and unpack().

    Code:
    #!/usr/bin/perl -w
    use strict;
    
    # string containing value
    my $str = "64000";
    my $uint16 = pack('v',$str);
    
    open(FH,">test.dat");
    print FH $uint16;
    close(FH);
    
    # now backward
    $uint16 = "";
    open(FH,"<test.dat");
    $uint16 .= $_ while(<FH>);    # pseudo slurp
    
    $str = unpack 'v',$uint16;
    print "$str\n";
    'v' is a little endian unsigned short integer as determined by the local C implimentation. If you check, test.dat is a two byte file which you could read directly into a C uint16_t.
    Last edited by MK27; 02-16-2010 at 11:59 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    8
    Hi

    Thanks very much for your assistance : )

    I was originally doing it in perl with unpack...however, a field later on in the some of the messages(quote messages) was giving me trouble. Here's a couple of structs that contain most of the fields :

    Code:
        char messageType;
        char ProtoOrVer;
        uint16_t MsgLen;    
        uint32_t MsgSeqNum;
        uint64_t ClOrdID;
        char OnBehalfOfCompID[11];
        char Rule80A;
        char Account[12];
        char TechnicalOrdType;
        char ClearingFirm[8];
        char ClientID[8];
        char FreeText[18];
        char OpenClose;
        char ClearingHandlingType;
        char Filler1;
        uint16_t NoQuoteEntries;
    the 64 bit field "ClOrdID". 64bit values(quads) are only available when using a version of PERL that supports them. I have to use a 32 bit version without support for quads since there are some PERL modules that I have to use here that contain 32bit only blibs.

    That's the reason why I decided to do it in C. It's worked very well so far when reading from a file but in production the feed of messages will be consumed on a socket by the PERL program - I was hence hoping to pass the messages directly to a c function for decoding, turn them into a string and pass them back.

    The interface between the PERL program and the UNIX pipe is a custom PERL module I have crafted using SWIG(XSUB) that wraps a C library containing the function that decodes the message. It works very well.

    I could break out the C function into a seperate program and communicate between the two using some form of IPC but it's simpler(which is usually better) to do it this way - well, if I can get this working !!

    Is it possible to pass the encoded string into the function and read the passed string into variables of the types mentioned using something that can work on encoded strings like fread does ??

    Thanks again for your help : )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Passing an array into a function
    By Ryston in forum C++ Programming
    Replies: 4
    Last Post: 08-29-2006, 05:20 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM