Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello Friends,

Can we read data from a Buffer with its Address and Print that data on the Console Without knowing its DATATYPE??
e.g. I have an Address of a Buffer 89F25F30. Now I want to read data from this buffer. I don't know the datatype of data too. After that I want to print that data on the console. (I am working on windows platform)

Now tell me is it possible or not? If yes, how?
Please Help me out of this. Its just Last Step of my project.

Thank you all
Naveen Kumar Dushila
Posted
Updated 6-Sep-11 20:34pm
v3

Hi Naveen

You can print that buffer in binary or hex or ascii or deciman or any other format.
 
Share this answer
 
Comments
nk.dushila 7-Sep-11 2:49am    
Hi Ashish

okay but How??
i mean with which function??
nk.dushila 7-Sep-11 2:51am    
i dont know that i am right or wrong
but
when i was using " printf(" ",&89F25F30); "
than i got some Signed Long value.
Hi Naveen

You can print that buffer in binary or hex or ascii or deciman or any other format.

like:-
C++
int x = 0x12345678;
int buffLen = 4; // you can set it according to your buffer length.
unsigned char *p = (unsigned char *)&x; // you can use here your buffer's address instead of &x
for(int i = 0; i < buffLen; i++)
{
    printf("\n Value at location \t%X is\t%X", p, *p);
    p++;
}

// warning this may cause segmentation fault.
 
Share this answer
 
v2
Comments
nk.dushila 7-Sep-11 8:05am    
Thank a Lot Ashish
Ashish Tyagi 40 7-Sep-11 9:17am    
Only thanks, where are my 5 points and please accept my both solution.
Welcome to THE CODE PROJECT
Well, we can read data from a buffer, that's for sure :). What we need is buffer start address and its size. Just assigning buffer start address to void* or char* or byte* (using cast when needed) is enough.
Printing your buffer out is not that straightforward, however. You must make decision on the output format you would like to see. Would you like to interpret your buffer as array of chars in one of encodings (ASCII, Unicodes etc)? Or you don't care about your output having meaning at least sometimes? In the first case you're free to use printf() function or stdout. Otherwise... well, printf() can help you again: to write your buffer out in hex you can use it too, printing byte by byte, in a manner described here[^] (I guess you'd need to use something like
C++
printf("%X", (unsigned char)array_item[i]);

or
C++
printf("%hhX", (char)array_item[i]);

)
 
Share this answer
 
You could also try to provide
the serializing from/to a buffer for your data class
and its output as well (such class will know its members' types) :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900