thee_esa

Hello,
if my unsigned short ist 1234 i want to have a char array with 1234 as result.

any idea i tried with

unsigned short test = 1234;
char* array = (char*)test;

and wrote the array to a file, the result are 4 squares.








Re: Visual C++ General convert unsigned short to char array in embedded visual c++

einaros

Casting is *not* a lexical conversion, so that will give you nothing but garbled data. See http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1910410&SiteID=1.





Re: Visual C++ General convert unsigned short to char array in embedded visual c++

thee_esa

evc doesnt know sstream, doesnt find ostringstream in std either.





Re: Visual C++ General convert unsigned short to char array in embedded visual c++

einaros

Then use itoa or similar.





Re: Visual C++ General convert unsigned short to char array in embedded visual c++

thee_esa

Ok, this worked:
char[10] buffer;
int i = 3445;
_itoa( i, buffer, 10 );

I have another problem. The numbers Im going to use dont have a fixed number of digits.
When I write the buffer to a file the "empty" places from the buffer are represented with squares and some symbols...

How can I cut the buffer where the digits end





Re: Visual C++ General convert unsigned short to char array in embedded visual c++

einaros

Fill the buffer with nulls, such as
char buffer[10] = {0};





Re: Visual C++ General convert unsigned short to char array in embedded visual c++

thee_esa

I dont know how much digits Im going to have.




Re: Visual C++ General convert unsigned short to char array in embedded visual c++

einaros

Well if you know you'll have at most a full 32 bit integer, that's 10 digits. Make your buffer 11 bytes long and you'll even hold the terminating null.





Re: Visual C++ General convert unsigned short to char array in embedded visual c++

thee_esa

Yes, but the space after the digits is somehow shown in the text-file anyways, where im writing. Filled with squares.







Re: Visual C++ General convert unsigned short to char array in embedded visual c++

Simple Samples

You must decide how to write and read data that varies in size. You can write the string in many ways and it depends on what you need to do and/or prefer to do. You must decide.

You can write the data so it occupies a fixed size, in which the fixed size will be the maximum size for the data. You can pad using space characters or with zero characters (the character zero). You can decide to pad in front (leading) or in back (trailing).

If you write a varying amount of data (varying length) it is your descision how the length is indicated. If you write a varying length string then you will need to use a delimiter to indicate the end of the string (the number converted to a string).






Re: Visual C++ General convert unsigned short to char array in embedded visual c++

einaros

thee_esa wrote:
Yes, but the space after the digits is somehow shown in the text-file anyways, where im writing. Filled with squares.


Space after the digits Did you actually fill it with nulls The string written should under no circumstances include whitespace characters, and nulls should not be interpreted as such. How are you writing the text back out