开发者

Loop through hex variable in C

开发者 https://www.devze.com 2022-12-31 14:54 出处:网络
I have the following code in a project that write\'s the ascii representation of packet to a unix tty:

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).

Th开发者_运维百科e 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


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" :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消