Hello,

I typically hang out in the C# forum, however I hope someone here can help.
I am converting a C++ program to C#, and I need to know how to handle a couple things: IOW I know almost nothing about C++, and this code is coming from a non dot-net version of C++

//char m_Command[30]; // <- is a class declared variable and is populated with data.
int nLength=0;
char checkSum[5];
long Total=123;

for(i=0;i<5;i++)
{
	checkSum[i]=0;	
}

sprintf(checkSum,"%04X",Total);

Can someone tell me what happens to checkSum with this sprintf method ?
I think I can formulate the string.Format syntax on my own once I know what this method is doing, but if you have a C# suggestion, I am open to that too.

Thanks in advance...

This method is an example of memory I/O. If you want to write some data into byte array or stream;

for instance,

int a=10;
float b=20.40f;
char name[]="Mr. Rajesh";

char buff[100];

sprintf(buff,"%d %f %s",a,b,name);

Values - 10, 10.20f, and "Mr. Rajesh" are copied into the buff - string variable.

Read: http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/

>>Can someone tell me what happens to checkSum with this sprintf method
checkSum is a c-string( array of chars) its value is set by Total's hex value converted to c-string.
In short, after this command, Total will be converted into its hex and in form of a string and will be stored in checksum.

Thanks for the quick response & the link.

So in my example, I would expect the 123 long to be formated as follows:

checkSum = 01230
or checkSum = 007B0

See my delima ? checkSum is a char[5], and the sprintf format suggests that it will insert a value that is 4 in length, left padded with 0's and will be X hex format ?

In C#, if I start with char[5] I end up with char[5], So I assume the last char will remain as an untouched 0.

So which value does checkSum become ?

// Jerry

the string pointed to by checkSum will contain the numerical value of Total is expressed as a hexadecimal string due to the 'X' specifier. specifically, any letters (A-F) will be uppercase letters rather than lowercase due to the fact that the 'X' specifier is uppercase

furthermore the string will be at least four characters in width, and will be zero-padded if the resulting hex representation is less than four characters. this is due to the '04' in the format specifier.

in this specific example, the string of chars pointed to by checkSum will be 007B and terminated by a NULL character \0 the memory location starting at the address of checkSum will be 0x30 0x30 0x37 0x42 0x00 .

>>See my delima ? checkSum is a char[5], and the sprintf format suggests that it will insert a value that is 4 in length, left padded with 0's and will be X hex format ?

There is no need to worry. The checkSum will be filled till the fourth subscript and the fifth subscript will be null character.
In C-strings, if you defined the array with char arr[k] then string you can store in arr should be of maximum length k-1 since the last element should be a null character '\0'

After the execution of the sprintf the checkSum will look like this:

checkSum:
index:     |  0  |  1  |  2  |  3  |  4   |
values:    |  0  |  0  |  7  |  B  | '\0' |

"no need to worry", as long as 'value' contains a value that is less than 65536, otherwise the hex string is going to be 5 or more characters in length before the NULL is considered,

at which point the behavior is undefined, and will most certainly be bad.

also the for loop that initializes checkSum is unnecessary and serves only to indicate the programmer has a lack of understanding of sprintf()

Thanks Guys, this is a major help.... I sure I will be back with more questions.
In C# the conversion was string xyz = Total.ToString("X4");

or if I still want an array then:

int Total = 123;
char[] checkSum = new char[5]; 
Array.Copy(Total.ToString("X4").ToCharArray(), checkSum, 4);

// Thanks, I have marked this as Solved, great job guys..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.