tags:

views:

160

answers:

1

I have the following code in a project that write's the ascii representation of packet to a unix tty:

int written = 0; 
int start_of_data = 3; 
//write data to fifo 
while (length) { 
        if ((written = write(fifo_fd, &packet[start_of_data], length)) == -1) 
{ 
                printf("Error writing to FIFO\n"); 
        } else { 
                length -= written; 
        } 
} 

I just want to take the data that would have been written to the socket and put it in a variable. to debug, I have just been trying to printf the first letter/digit. I have tried numerous ways to get it to print out, but I keep getting hex forms (I think).

The expected output is: 13176 and the hex value is: 31 33 31 37 36 0D 0A (if that is even hex)

Obviously my C skills are not the sharpest tools in the shed. Any help would be appreciated.

update: I am using hexdump() to get the output

+2  A: 

These are the ASCII codes of characters: 31 is '1', 33 is '3' etc. 0D and 0A are the terminating new line characters, also known as '\r' and '\n', respectively. So if you convert the values to characters, you can print them out directly, e.g. with printf using the %c or %s format codes. As you can check from the table linked, the values you posted do represent "13176" :-)

Péter Török
Thanks Peter, using %s worked like a charm.-Jud
Jud Stephenson