473,324 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Multiple substrings = Error

Hi, I need to manipulate a string by swapping pairs of 2 characters
around. I tried using the substr function to extract the characters,
but I get getting debug errors when it runs. Here is an example:

Input:
01110100
01 11 01 00

Swap:
00 01 11 01

Output:
10011001

My code:
flipString1 = binString.substr(0,2);
flipString2 = binString.substr(2,2);
flipString3 = binString.substr(4,2);
flipString4 = binString.substr(6,2);
binString = flipString4 + flipString3 + flipString2 + flipString1;

If it comment out all but one of the substr commands, the program runs
fine. If I try to do multiple, I get an error (Abnormal Program
Termination). I am using the Microsoft Visual C++ 6.0 compiler.

the flipString variables are declared as such:

string flipString1 = string();
string flipString2 = string();
string flipString3 = string();
string flipString4 = string();

I also tried to reference binString as a char array and concat them
together, but it did not work. Example:
string flipString1 = binString[0] + binString[1];

Any ideas?
Thanks,
-Elliot

Feb 15 '07 #1
8 1245
Pi********@gmail.com wrote:
Hi, I need to manipulate a string by swapping pairs of 2 characters
around. I tried using the substr function to extract the characters,
but I get getting debug errors when it runs. Here is an example:

Input:
01110100
01 11 01 00

Swap:
00 01 11 01

Output:
10011001

My code:
flipString1 = binString.substr(0,2);
flipString2 = binString.substr(2,2);
flipString3 = binString.substr(4,2);
flipString4 = binString.substr(6,2);
binString = flipString4 + flipString3 + flipString2 + flipString1;

If it comment out all but one of the substr commands, the program runs
fine. If I try to do multiple, I get an error (Abnormal Program
Termination). I am using the Microsoft Visual C++ 6.0 compiler.

the flipString variables are declared as such:

string flipString1 = string();
string flipString2 = string();
string flipString3 = string();
string flipString4 = string();

I also tried to reference binString as a char array and concat them
together, but it did not work. Example:
string flipString1 = binString[0] + binString[1];

Any ideas?

First, post a minimal, COMPILABLE example that exhibits the behavior in
question.

Second, VC6 is horrendously old, out of date, and non-Standard
compiliant. Upgrade to VC2005 (Express version available for free as in
beer), or to GCC (available free as in speech and beer).

Third, you don't need to do the "= string()" on the flipStringN
variables. They will be default constructed unless you provide some
other sort of initializer.

Feb 15 '07 #2
Pi********@gmail.com wrote:
Hi, I need to manipulate a string by swapping pairs of 2 characters
around. I tried using the substr function to extract the characters,
but I get getting debug errors when it runs. Here is an example:

Input:
01110100
01 11 01 00

Swap:
00 01 11 01

Output:
10011001

My code:
flipString1 = binString.substr(0,2);
flipString2 = binString.substr(2,2);
flipString3 = binString.substr(4,2);
flipString4 = binString.substr(6,2);
binString = flipString4 + flipString3 + flipString2 + flipString1;

If it comment out all but one of the substr commands, the program runs
fine. If I try to do multiple, I get an error (Abnormal Program
Termination). I am using the Microsoft Visual C++ 6.0 compiler.

the flipString variables are declared as such:

string flipString1 = string();
string flipString2 = string();
string flipString3 = string();
string flipString4 = string();

I also tried to reference binString as a char array and concat them
together, but it did not work. Example:
string flipString1 = binString[0] + binString[1];

Any ideas?
FAQ 5.8. Also, VC++ v6 has a very good debugger. Use it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 15 '07 #3
Pi********@gmail.com wrote:
Hi, I need to manipulate a string by swapping pairs of 2 characters
around. I tried using the substr function to extract the characters,
but I get getting debug errors when it runs. Here is an example:

Input:
01110100
01 11 01 00

Swap:
00 01 11 01

Output:
10011001

My code:
flipString1 = binString.substr(0,2);
flipString2 = binString.substr(2,2);
flipString3 = binString.substr(4,2);
flipString4 = binString.substr(6,2);
binString = flipString4 + flipString3 + flipString2 + flipString1;

If it comment out all but one of the substr commands, the program runs
fine. If I try to do multiple, I get an error (Abnormal Program
Termination). I am using the Microsoft Visual C++ 6.0 compiler.

the flipString variables are declared as such:

string flipString1 = string();
string flipString2 = string();
string flipString3 = string();
string flipString4 = string();

I also tried to reference binString as a char array and concat them
together, but it did not work. Example:
string flipString1 = binString[0] + binString[1];

Any ideas?
Thanks,
-Elliot
All the code you posted is fine. The problem is somewhere in the code
you didn't post. It usually is, which is why it is recommended to post
complete programs.

john
Feb 15 '07 #4
I can not post the entire code, but here is some more.

string str = string();
string binString = string();
string flipString1 = string();
string flipString2 = string();
string flipString3 = string();
string flipString4 = string();
string toReverse = string();
out << toEncrypt;
binString = out.str();

flipString1 = binString.substr(0,2);
flipString2 = binString.substr(2,2);
flipString3 = binString.substr(4,2);
flipString4 = binString.substr(6,2);

binString = flipString4 + flipString3 + flipString2 + flipString1;

cout << "after flip " << binString << endl;

Feb 15 '07 #5
Pi********@gmail.com wrote:
I can not post the entire code, but here is some more.

string str = string();
string binString = string();
string flipString1 = string();
string flipString2 = string();
string flipString3 = string();
string flipString4 = string();
As suggested before, you can safely drop all those '= string()'
parts. Strings are correctly initialised without that.
string toReverse = string();
This doesn't seem relevant at all.
out << toEncrypt;
What's the content of 'toEncrypt'?
binString = out.str();
Add

assert(binString.size() >= 8);

here
>
flipString1 = binString.substr(0,2);
flipString2 = binString.substr(2,2);
flipString3 = binString.substr(4,2);
flipString4 = binString.substr(6,2);

binString = flipString4 + flipString3 + flipString2 + flipString1;

cout << "after flip " << binString << endl;
If you don't want to post your actual code, it's fine with us,
but you have to realize that we're not mind readers.

If I have to guess, the error is in your 'toEncrypt' output. It
most likely does not produce long enough a string. Check the length
of 'binString'.

As a freebee, here is what your fragment should look like:

out << toEncrypt;
string binString = out.str();

string flipString1 = binString.substr(0,2);
string flipString2 = binString.substr(2,2);
string flipString3 = binString.substr(4,2);
string flipString4 = binString.substr(6,2);

string binString = flipString4 + flipString3
+ flipString2 + flipString1;

cout << "after flip " << binString << endl;

As you hopefully see, there is *absolutely* no need to define those
variables before they are going to be given some value.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 15 '07 #6
On Feb 15, 1:56 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Pistole...@gmail.com wrote:
I can not post the entire code, but here is some more.
string str = string();
string binString = string();
string flipString1 = string();
string flipString2 = string();
string flipString3 = string();
string flipString4 = string();

As suggested before, you can safely drop all those '= string()'
parts. Strings are correctly initialised without that.
string toReverse = string();

This doesn't seem relevant at all.
out << toEncrypt;

What's the content of 'toEncrypt'?
binString = out.str();

Add

assert(binString.size() >= 8);

here
flipString1 = binString.substr(0,2);
flipString2 = binString.substr(2,2);
flipString3 = binString.substr(4,2);
flipString4 = binString.substr(6,2);
binString = flipString4 + flipString3 + flipString2 + flipString1;
cout << "after flip " << binString << endl;

If you don't want to post your actual code, it's fine with us,
but you have to realize that we're not mind readers.

If I have to guess, theerroris in your 'toEncrypt' output. It
most likely does not produce long enough a string. Check the length
of 'binString'.

As a freebee, here is what your fragment should look like:

out << toEncrypt;
string binString = out.str();

string flipString1 = binString.substr(0,2);
string flipString2 = binString.substr(2,2);
string flipString3 = binString.substr(4,2);
string flipString4 = binString.substr(6,2);

string binString = flipString4 + flipString3
+ flipString2 + flipString1;

cout << "after flip " << binString << endl;

As you hopefully see, there is *absolutely* no need to define those
variables before they are going to be given some value.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Thanks for your help. I will address your questions in their order.

I understand that I don't need the = string(). It is just a habit,
and I will remove them.

string toReverse = string(); is a temporary string that I made in
order to do another string manipulation to. I have not posted the
code that uses that variable, so I can understand your confusion.

toEncrypt is the input string (8 binary characters) which is passed to
the encrypt() function.

what is the purpose of "assert(binString.size() >= 8);"? It does
correctly read the string using the way I coded it.

I can not post the entire code, since if I did, the encryption would
be pointless since someone could just look here. I thank you for
understanding that.

toEncrypt is a global variable which stores the unencrypted string of
binary. I am copying it into binString so that I am not directly
modifying toEncrypt. Both toEncrypt and binString have a length of 8
chars through each pass of the function. After the string is
encrypted, binString is returned to the calling function where it can
be written to a file.

Thanks for all your help.

Feb 15 '07 #7
On Feb 15, 5:06 pm, Pistole...@gmail.com wrote:
I can not post the entire code, but here is some more.
[snip]
>
If you don't want to post your actual code, it's fine with us,
but you have to realize that we're not mind readers.
[snip]

I can not post the entire code, since if I did, the encryption would
be pointless since someone could just look here. I thank you for
understanding that.
No one really wants you to post all the code. What everyone wants you
to do is provide the shortest piece of _compilable_ code that
reproduces the problem you are having. This requires you to do some
work - you need to reduce your long, complicated, possibly proprietary
program to something nice and short that illustrates the problem,
_but_ _that_ _we_ _can_ _compile_. Then we can cut and paste your
code, compile it, and see whether we have the same problem you do.
Granted, it takes some work on your part to reduce the code in this
manner, but for that effort, you get two major benefits:

1. If you actually post that short, complete, code example, you will
have someone actually figure out your problem, rather than playing
this current guessing game. Why? Because usually the problem is in
part of the code you didn't post before.

2. Most times, in the process of shortening the code to something you
can post in full, you figure out the problem on your own - typically
in an "aha" moment where you say to yourself, "I can't believe I
missed that."

Anyway, just wanted to explain why people keep saying "post actual
code."

Good luck.

Best regards,

Tom
Feb 15 '07 #8
On 15 Feb 2007 14:06:18 -0800, Pi********@gmail.com wrote:
>toEncrypt is the input string (8 binary characters) which is passed to
the encrypt() function.

what is the purpose of "assert(binString.size() >= 8);"? It does
correctly read the string using the way I coded it.
The assert is to check that the string returned from toEncrypt() is
indeed eight or more characters long. If, as seems probable, there is
a fault in toEncrypt() which causes it to return a string less than
eight characters long then this assert will catch it and halt your
program.
>
I can not post the entire code, since if I did, the encryption would
be pointless since someone could just look here. I thank you for
understanding that.
This is probably a mistake. Either your encryption is reliant on the
secrecy of the algorithm or you have hard coded the encryption key
into your source code. Both of these are mistakes and should be
avoided.

A secure encryption algorithm can be publicly known (like DES or AES)
and still be secure as long as the key is kept secret. If the key is
hard coded into the source code then the key cannot be secure and the
system is compromised.

rossum

Feb 16 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Leandro Pardini | last post by:
Hello there, I'm trying to process a binary file and I really don't know how. The story: gPhoto2 downloads the images from my camera just fine, but small areas of 10x3 pixels are screwed up. I...
4
by: spam | last post by:
Is there a well-known algorithm for replacing many substrings in a string? For example, I'd like to take the string "abc def ghi jkl mno pqr" and replace, say, every instance of "abc", "ghi", and...
9
by: C3 | last post by:
I have to process some data in C that is given to me as a char * array. I have a fairly large number of substrings (well, they're not actually printable, but let's treat them as strings) that I...
4
by: Robert Dodier | last post by:
Hello all, I'm trying to find substrings that look like 'FOO blah blah blah' in a string. For example give 'blah FOO blah1a blah1b FOO blah2 FOO blah3a blah3b blah3b' I want to get three...
8
by: girish | last post by:
Hi, I want to generate all non-empty substrings of a string of length >=2. Also, each substring is to be paired with 'string - substring' part and vice versa. Thus, gives me , , , , , ] etc....
1
by: Jason S | last post by:
Is there a way to get the position of multiple substrings that match a regexp without using closures? match() returns the substrings themselves, not the positions, and search() seems to only return...
1
by: roots_of_culture | last post by:
Hi all, VB novice bear with me... trying to build myself a template in Outlook to make life easier I am aquiring multiple subtrings from the user and want to replce one substring in a outlook...
4
by: rajarora | last post by:
Hi All, I need to have a function that should remove the multiple occurrences of all substrings present in a string. That is the function will be having an input like this:- char* data=NULL;...
2
by: Pilcrow | last post by:
This problem was raised in comp.lang.perl.misc, and the poster was concerned, among other things, by the speed of execution. Since C is faster than perl, I wonder how a C coder would solve it? ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.