Data formating using C programm with Hex deciamal 'x0d'

:b:Guys,

Can some body throw some light on this please.....

sprintf(req_line1, "%c%s%c", '\x0b',"TESTING1",'\x0d');
sprintf(req_line2, "%s%c", "TESTING2", '\x0d');
sprintf(req_line3, "%s%c", "Testing3", '\x0d');
sprintf(req_line4, "%s%c%c%c", "Testing4", '\x0d', '\x1c', '\x0d');

sprintf(req_msg, "%s%s%s%s", req_line1,req_line2,req_line3,req_line4);

while doing this i am not able to store everything into the req_msg variable becuase of the '\0xd' (Carriage return) in every req_line1..4.

can somebody please tell me how do i format this data.. .i need line is a segment here and each segment mush end with the '\0xd' and send everything together for processing...

Pelase kindly share your thoughts on this.

  • thank you
    Cheers :slight_smile:
    Sudharma.

Your program works fine:

$ ./line | hexdump -C
00000000  0b 54 45 53 54 49 4e 47  31 0d 54 45 53 54 49 4e  |.TESTING1.TESTIN|
00000010  47 32 0d 54 65 73 74 69  6e 67 33 0d 54 65 73 74  |G2.Testing3.Test|
00000020  69 6e 67 34 0d 1c 0d 0a                           |ing4....|
00000028
$ ./line | wc
      1       4      40
$

Everything all in one line with carriage returns in the middle. How are you verifying what data is printed? Keep in mind that if you print it to a console, a carriage return sends the cursor to the beginning of the line!

Thank you so much for your reply. Yes, right when i print using the printf it messages is being overwritten to the begining of the line. Not sure how to fix this.

As you said i want every thing in one line with carriage return in the middle. Can you hlep me with the 'C' source code.. or just brief me.

thank you

Cheers:)
S

Sorry!!! Looks ok. I will have to test it know.

Thank you so much again for your time.

Cheer :slight_smile:
Sudharma.

Hi,

when doing this..

sprintf(req_line1, "%c%s%c", '\x0b',"TESTING1",'\x0d');
sprintf(req_line2, "%s%c", "TESTING2", '\x0d');
sprintf(req_line3, "%s%c", "Testing3", '\x0d');
sprintf(req_line4, "%s%c%c%c", "Testing4", '\x0d', '\x1c', '\x0d');

sprintf(req_msg, "%s%s%s%s", req_line1,req_line2,req_line3,req_line4);

There are few extra characters are getting appended into the req_msg variable. When i print out the hex decimal values (using printf ("%0X", req_msg[i])) for this. here is what it is printing...

[D] [1C] [D] [7F] [45] [4C] [46] [1] [2] [1]

My last line ends with '\0xD', '\0x1C' and '\0XD' print here correctly. But appends with some junk values at the end [7F] [45] [4C] [46] [1] [2] [1] not sure where how it's printing this values. Can somebody please help me...

can somebody pelase help me .....

thanks
Cheers:)
Sudharma.

Once again your program works absolutely fine:

$ ./a.out | hexdump -C
00000000  0b 54 45 53 54 49 4e 47  31 0d 54 45 53 54 49 4e  |.TESTING1.TESTIN|
00000010  47 32 0d 54 65 73 74 69  6e 67 33 0d 54 65 73 74  |G2.Testing3.Test|
00000020  69 6e 67 34 0d 1c 0d                              |ing4...|
00000027
$

Yes, checking beyond the end of your string may show invalid values -- you never set it to anything else, so its contents could be anything. So what? sprintf will always end its strings in a NULL, preventing any of the garbage from being used.

Or, if your program does not place the null correctly, there may be something funny going on with buffer sizes. Make sure all your character arrays are large enough to hold their strings.

try with bzeroing req_msg before following statement.

  • Dheeraj