答案 :
可以運用int的特性,以兩個極端的例子來說 packet_size #1=96 bytes, packet_size #2=97bytes
依照判斷pkt #1是切成2個訊框,pkt #2是切成3個訊框,應該怎麼寫呢?
C語言寫法如下:
frame_number = (int) (packet_size + 47)/48即可得到上面所需的答案囉!!
另外,也可以把它寫成函數的方式來呈現
#define PKT_SIZE 48 /* the maximum packet size */ /* calculate the number of packets */ int cal_pkt_number(int packet_size) { return (int) (packet_size+PKT_SIZE-1)/PKT_SIZE } int main(void) { int curr_pkt_size = 47; int pkt_no; pkt_no = cal_pkt_number(curr_pkt_size); printf("The number of packets is %d\n", pkt_no); }
No comments:
Post a Comment