c - Filling malloc'd memory and converting into a char array -
i have data want send usart terminal accepts char array.
first create data , fill stuff:
uint32_t * testdata = malloc(60 * sizeof(uint32_t)); // 240 bytes on system memset(testdata,3,240); // fill
then create char
array , copy test data it
char* valueasstring = malloc(60 * sizeof(uint32_t) + 1);// 1 more 0-terminator snprintf( valueasstring, 240 + 1, "%d", testdata );
the function send data looks this:
static void sendrs232string(char message[], uint16_t messagelength ) { (uint8_t = 0; < messagelength; i++) { usart_putchar(usart_serial, message[i]); } }
all on output '88' ?
btw i'm using 32bit atmel microcontroller: atxmega128a1u
any ideas on how achieve this?
----------------------------------------update------------------------------------
i have re-worded question try , more specific:
i’m trying create block of memory 446 byte long, put data in , print block of memory terminal 1 byte @ time.
uint16_t * wordtosend = malloc(446); // create memory memset(wordtosend,0,446); //set 0 fill uint16_t test = 2; // on system uint16_t 2 bytes long memcpy(wordtosend,test,sizeof(test)); // copy test new memory starting @ begining // print out 1 byte @ time int i; for(i = 0; < 447; i++) { unsigned char valueasstring = ((char*)wordtosend)[i] ; usart_putchar(usart_serial, valueasstring); }
putchar takes:
enum status_code usart_putchar(usart_t *usart, uint8_t c)
the problem wordtosend full of zeros expect first 2 bytes 02. i’m not sure problem lies here either, whether i’m copying test in wrongly or whether it’s issue valueasstring assignment. if shed light on why happening or suggest better way of doing that’ll great.
fyi: when debugging, if @ memory location
wordtosend
it contains zeros whereas
*( wordtosend)
contains data.
based on comment "i expect output 240 x '3'", code is
char *testdata = malloc(240); memset(testdata, '3', 240); sendrs232string(testdata, 240);
if you're worried fact sendrs232string
function seems expect array, , you're passing pointer, that's not problem, due so-called "equivalence of arrays , pointers in c". (if don't understand this, ask, or google search on it.)
the other thing need understand there's huge, huge difference between 3
, '3'
. 3
integer 3. '3'
character 3, in ascii has value 51.
when called
memset(testdata,3,240);
you filling testdata
number 3, ends (again, in ascii) being equivalent character control-c.
i'm not sure trying accomplish snprintf
call. depending on you're trying do, may or may not need use snprintf
convert between characters , integers, or integers , hexadecimal representations, or something.
it sounds want work bunch of 4-byte integers. need decide how want transmit integers on serial port: raw, "binary" numbers (4 bytes each), or text representation (hex, decimal, or perhaps else).
based on comment "i need display each of 240 bytes, ideally in hex", code want might more like
int i; for(i = 0; < 60; i++) { char valueasstring[30]; snprintf(valueasstring, sizeof(valueasstring), "%x\n", testdata[i]); sendrs232string(valueasstring, strlen(valueasstring)); }
note i'm looping on values in testdata
, converting them 1 @ time hex using snprintf
, , calling sendrs232string
60 separate times. not creating 1 text buffer containing 60 formatted integers, or that.
if want each 4-byte represented 8 hexadecimal digits, change printf format %x
%08x
.
Comments
Post a Comment