Exforsys
+ Reply to Thread
Results 1 to 3 of 3

hex data formatting

This is a discussion on hex data formatting within the C and C++ forums, part of the Programming Talk category; In our application header and trailor to be added to message and sent of fixed length. The header and trailor ...

  1. #1
    subathira is offline Junior Member Array
    Join Date
    Jun 2005
    Answers
    1

    hex data formatting

    In our application header and trailor to be added to message and sent of fixed length. The header and trailor are to be sent as hex constant values. The message they read it as ascii.


    Tried formatting by assigning hex values to char array.but
    it is not properly interpreted on other end. In addition null gets added to length and they receive one byte addition to expected
    length and error out.But when do strlen(buffer) this shows the right length
    please provide how this can be done avoiding the addition of null at End of string ..can this be done with memcpy...tried byt getting different values..

    we have the struct defining the message fields

    {
    char MesgLen [3]; //2 bytes
    char Reserve[3] // 2bytes
    .
    .
    .

    Data[23] //22 bytes
    ..
    trailor [3] // 2bytes
    }

    In my program as anandt specified have populated the fields with char array

    like MesgLen[0]=0x00;
    MesgLen[1]=0xE7;
    and so on....
    we are to sent the message of 28 bytes length..expected out on other end
    00E70002AB.....Data.....1C1A The header and trailor have to be hex .Most of the fields gets populated properly...but now the output on other end becomes
    20202000.......data...1C1A00

    Regarding the mesg length expected number of bytes on other end was 28 bytes but they receive 29 bytes.On my mesg above u can se at the end 00-representing null field.when i get bufferlen on my side it is 28 but i think because of string format null gets added.
    Can anything be done to avoid the above problem with memcpy..or any other suggestions ....hope i am clear...please help me with valuble suggestions

    Last edited by subathira; 02-18-2006 at 06:41 PM.

  2. #2
    Arun123 is offline Junior Member Array
    Join Date
    Mar 2006
    Answers
    1
    Here is something you can try

    strings are defined as an array of characters or a pointer to a portion of memory containing ASCII characters. A string in C is a sequence of zero or more characters followed by a NULL ()character:

    The strncat(), strncmp,() and strncpy() copy functions are string restricted version of their more general counterparts. They perform a similar task but only up to the first n characters. Note the the NULL terminated requirement may get violated when using these functions, for example:


    char *str1 = "HELLO WELCOME";
    char *str2;
    int length = 2;


    (void) strcpy(str2,str1, length); /* str2 = "HE" */

    str2 is NOT NULL TERMINATED!!


  3. #3
    priyaraji is offline Member Array
    Join Date
    Apr 2006
    Answers
    52
    Hi,
    In the character array, You check for '/0' and then print the output until it is reached. Once the '/0' is reached skip from printing.This will solve your problem.

    Sripriya

    Last edited by admin; 02-16-2012 at 09:11 AM.

    •    Sponsored Ads



Latest Article

Network Security Risk Assessment and Measurement

Read More...