C++:C-Style String?

I'm writing a program that encodes. It must first read the data from the file into a large c-style string. Then it must iterate through the string and convert the ASCII characters to 5 or 6 digit numbers.
Thank you!
Last edited on
A tip :
int DecodeChar(char c)
{

return (int(c - 32));

}


1
2
3
4
5
6
inline unsigned int DecodeChar(char c)
{

return (unsigned int(c - 32));

}


#define DecodeChar(c) ((unsigned int)c - 32)

HTH
Last edited on
C style string is also called Null-terminated string, which means if you make a C style string, it should be end with NULL character, which is 0 in number, at the end. You can simply iterate through all of them by start from the first element and check one by one if the element that you are currently looking at is NULL or not.

 
char* s = "Hello";


This actually takes up 6 bytes: {'H', 'e', 'l', 'l', 'o', NULL} in sum 6 characters. One of the things you can do to iterate through is this:

1
2
3
4
5
6
7
8
int i = 0;
while( s[i] != NULL )
/* while( s[i] ) is also OK because NULL is 0 in number */
{
  // Do something with s[i]

  ++i;
}


If s is not C-style string, it has no NULL character in it. Most likely it will crash the program because there is no way to get out of the loop.
Last edited on
Topic archived. No new replies allowed.