# converting c-string to byte-count-string

This topic is 2504 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hello !
i am still preparing for an c++ test and i am trying to convert a c-string to a byte-count-string.

so if i have a string like:
"Hello"

I have to convert it to:
"5Hello"

However the problem i am facing is that i can't convert the calculated count of the string to an char value.
it should be very straight forward since this excercise gives only very little points in the test

#include "stdafx.h" #include "iostream"; using namespace std; char* c2b(char* bc) { int b = strlen(bc) +1; char* temp = new char; int i; for(i = 1; i < b; i++) temp = bc[i-1]; b -= 1; char test = static_cast<char>(b); //I want to cast the Integer to a char temp[0] = test; //temp = '\0'; //Not needed for the Byte-Count-String bc = temp; return bc; } int _tmain(int argc, _TCHAR* argv[]) { char bc[] = "String"; cout << c2b(bc); char f; cin >> f; return 0; } 

##### Share on other sites
Are you sure you want to have the byte count as text, as it would make more sense if the byte code could be read by other code to get the lenght (as if you put it in as a string and take away the null from the end, code no longer can tell when it stops i think...)

##### Share on other sites

However the problem i am facing is that i can't convert the calculated count of the string to an char value.
it should be very straight forward since this excercise gives only very little points in the test

For C, see itoa(...); Or printf/sprintf.

For C++, see streams, stringstream or cout for example.

If all you need to do is print the result to console, then solution takes 1 line. If you need string, it will take about 5.

##### Share on other sites
Are you sure the length of the string is in printable text? Consider that if the byte count is a single numeric character, then you can only have strings that are 0-9 characters long*. If you are implementing a Pascal string converter, then the length should be put into the first byte, not in a text encoding.

[size=2]* Even using the full range of the first byte, you only get 0-255 characters max. If you are developing a general purpose converter function, you should probably think about what should happen if the text is longer than the maximum length allowed.

##### Share on other sites
A length-prefixed string (more commonly referred to as a 'pascal string'), stores the length in the first byte on the string. However, it isn't an ascii byte - it is just a literal numeric value of type unsigned char (thus allowing strings up to 255 characters in length).

In other words, what you are doing there is correct, but if you try to print it out, the first character will appear as a random character (and itis likely not to be a printable ascii character at all).

##### Share on other sites
hmm..i tried it with "string" and i actually get "string" as output compined with a hardware-bleeping sound but not the numeric size value in front ^^
I am really depressed by a lot of the excercises that happens in test. Most tests are made that you can get through if you are just as fast as copying all the answers from a solution paper on a blank one...but that's another issue :/

thank you very much for your replys, see you later ! ;)

##### Share on other sites

hmm..i tried it with "string" and i actually get "string" as output compined with a hardware-bleeping sound but not the numeric size value in front ^^

You are getting the exact output I described.

Specifically, you are trying to print a starting byte with the value of 0x7 (i.e. a literal 7, not the ascii character '7'). And 0x7 is the non-printing bell character, so your terminal is outputting a beep when you print it.

##### Share on other sites

hmm..i tried it with "string" and i actually get "string" as output compined with a hardware-bleeping sound but not the numeric size value in front ^^
I am really depressed by a lot of the excercises that happens in test. Most tests are made that you can get through if you are just as fast as copying all the answers from a solution paper on a blank one...but that's another issue :/

What does the assignment actually say?

Is it about strings, or does it say: take a string and print out same text with number of characters in front?

I find Pascal style strings to be a very unusual topic of excercises or tests.

If I had to guess, I'd say the assignment wants this:int main(int argc, char **argv) { const char * s = argv[1]; // or const char * s = "Hello"; printf("%d%s\n", strlen(s), s); }

##### Share on other sites
it says: Write a function ctob(), which converts a c-string (given as an argument) in a equal stringchain in byte.count-format

there are also 2 pics like that:

C - String: | S | t | r | i | n | g | \0 |
Byte-Count-String: | 6 | S | t | r | i | n | g |

@Antheus :t think so, too...the excecise isn't really usual but i have to deal with stufff like that...but i have still time till next friday.

@swiftcoder: thank you for the deeper explenation !

##### Share on other sites
For strings that are 0 to 9 characters, the conversion from integer to ASCII digit is fairly simple:
int c = ... if (c >= 0 && c < 10) { s[0] = '0' + c; } else { // can't store }

It takes the ASCII value of character for zero ('0') and adds count to it. It works since in ASCII, digits 0..9 are represented sequentially.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633702
• Total Posts
3013450
×