Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++

Help in strings please

Altera_Forum
Honored Contributor II
1,285 Views

Hi, I'm having problems with using strings on NIOS II IDE. Basically I plan to connect a RFID reader in my DE2 board, RFID sends 12 characters so I made codes to read them one by one and then store it in a string. Here's the C code for that part. I declared the "message" as a 12 characters. So I tried sending signals through Hyperterminal like 123456789012, which is 12 characters. When i print "message", it will print this instead "123456789012[][] a". The [] represents a box. So, what do you guys think I shall do? 

unsigned char message; while(1) { // Read character from hyperterminal printf("Reading...\n"); unsigned char k; for(k=0;k<12;k++) { while((IORD_ALTERA_AVALON_UART_STATUS(UART_0_BASE) & 0x080) != 0x080) // Wait for read-ready { } message = IORD_ALTERA_AVALON_UART_RXDATA(UART_0_BASE) ; // Read character printf("Read: %c\n",message); } printf("Read Signal: %s\n",message);
0 Kudos
3 Replies
Altera_Forum
Honored Contributor II
333 Views

C strings are terminated by a zero byte.

0 Kudos
Altera_Forum
Honored Contributor II
333 Views

In other words, extend your string by one more character (make it 13 instead of 12) and set the last byte equal to the terminating character before trying to print out the string.

0 Kudos
Altera_Forum
Honored Contributor II
333 Views

In general determine what the maximum number of characters your character array will hold and then add one more for the NULL character at the end of the string. More info: http://en.wikipedia.org/wiki/null-terminated_string 

 

Often when working with a stream of data (characters being an example of that) the APIs you call will document what type of termination is needed. You require a NULL character at the end because printf doesn't know ahead of time how many characters are in 'message' when you are printing the entire string using %s. For the individual character printing the NULL character isn't needed because it already know how many characters to print when you use %c.
0 Kudos
Reply