Ok, it's not like everywhere. What I want to get here is:
1. We have an int. For example 10.
2. We have a 10-byte string: "0000000000"
3. We want to place our int into that string, so it looks like this: "0000000010"
How would I do that?
mfeik
October 26th, 2002, 10:30 PM
here is an example of how to achieve what you asked. There are many ways to go about it but this will work. I provided no error checking to insure the user entered an number and just so you know, the value of int a is bound by the limits of an int. I'm just saying you should add a bit of code to verify the input is correct BEFORE you try to manipulate it.
#include<iostream>
#include <string.h>
using namespace std;
int main(int argc, char* argv[])
{
int a;
cout << "Enter a number: " << endl;
cin >> a;
char str[10], str2[10]={0};
itoa(a, str,10);
for (int i = 10-strlen(str); i > 0; i--)
strcat(str2, "0");
strcat(str2, str);
cout << str2 << endl;
return 0;
}
mfeik
October 26th, 2002, 10:39 PM
did i just do someones homework??
Oh well... I actually need to write a routine which does exactly the opposite of what I posted so it was a good starter (I needed a function that can fill the end of an a string with whitespace up to the 19th indexable position after the last char). Guess I'm defending myself for doing for someone what they really should have done for themselves as if they get tested and didn't learn it on thier own... well.. we all know the outcome.
-Mel
Silver Ghost
October 27th, 2002, 12:13 AM
Thank you it works just fine!
But for some reason I get "Debug Error" window. If I comment out the line "strcat(buf2, "0")", it doesnt give me an error, but of course it doesnt work. I am debugging it right now, looking for cause, but maybe you can help?
Silver Ghost
October 27th, 2002, 12:14 AM
char * buf1 = new char[spaceForKeyDigits];
char * buf2 = new char[spaceForKeyDigits];
//for ( int j = 0; j < spaceForKeyDigits; j++ ) buf2[j] = 0; // buf2[]={0}
itoa(keyDigits, buf1, 10);
for ( int i = 10-strlen(buf1); i > 0; i-- )
strcat(buf2, "0");
strcat(buf2, buf1);
// Insert buf2 into str
int strLen = strlen(str)/2;
i=0;
for ( int z = strLen; z < strLen+spaceForKeyDigits; z++ )
{
str[z] = buf2[i];
i++;
}
delete [] buf1;
delete [] buf2;
Hmm.. If I dont declare the first two buffers as a dynamic memory it works fine. Interesting.
mfeik
October 27th, 2002, 12:45 AM
I was not clear in understanding your debug error... can you post that? or better yet.. just send it to me email... mfeik@comporium.net and I'll see if I can offer anymore help
Of course you will want to add error checking, etc
ltachna
October 27th, 2002, 08:19 AM
Originally posted by Silver Ghost
Ok, it's not like everywhere. What I want to get here is:
1. We have an int. For example 10.
2. We have a 10-byte string: "0000000000"
3. We want to place our int into that string, so it looks like this: "0000000010"
How would I do that?
try something like this
int i = 10;
char buf[11];
sprintf(buf, "%010d", i);
this works for me no need to reinvent the wheel and this will work for numbers with any amount of digits in it ie 110, 11000, etc and you dont need to start with a string set to all zero's
PaulWendt
October 28th, 2002, 06:09 AM
In case it matters to anyone, itoa() isn't a implemented by most
ANSI C compilers. If you don't care about portability, then use it;
otherwise, stick to the sprintf() solution.
--Paul
mfeik
October 28th, 2002, 01:24 PM
just wanted to mention that I don't like to give a "best" case method when I feel I'm answering a homework post. I have two main reasons for this:
1) I'm selfish... learned to code on my own and....
2) I used to tutor C++ programming students in the New York states' college system callen SUNY. I found that more often than not, student where repremanded for using functions/libraries/routines beyond the scope of what the professor had presented (I always thought to be a reflection on the professors involved) but that is why I posted such a rudementary solution to the question at hand. All in all I can understand a teacher of a programming launguage (when its clearly an introductory course) dissallowing students from using code that clearly show they a) should have 'clepped the course' or b) got the solution from someone with more exerience.
Just thought I would mention this...
Yves M
October 28th, 2002, 01:40 PM
mfeik,
I agree with your stance on homework help. But I don't think that this particular question was a homework assignment. Well, I guess Silver Ghost can clear this up if he wants to ;)
mfeik
October 28th, 2002, 01:57 PM
You could be right. I just took it from the format of the question, the pluar tense, and from no apparent indication that it served a purpose other then to learn a bit about string manipulation to indicate it was a homework problem. Guess I should maybe reconsider how many assumptions I make and just provide the best answer (that I'm aware of) if I'm going to post at all.
I was just thinking that if I needed a similar routine for something I was working on I would post it in as generic a format as I could.
PaulWendt
October 28th, 2002, 02:13 PM
As a further clarification on my part, I wasn't trying to bash your
[mfeik's] answer at all and I hope that my post didn't imply that
the answer was unsatisfactory. I was just trying to give a
portable solution.
--Paul
mfeik
October 28th, 2002, 03:06 PM
Really, I didn't take any offense, and I was a glad to see your post. I cant even count the number of valid, pertinent info-bits picked up from comments like yours. I've learned a great many valid item that I didn't get out of books just from response like yours. :)
-Mel
PaulWendt
October 28th, 2002, 04:08 PM
Okay, great. There are plenty of users who delight in trying to
belittle the posts of others; I am not such a user and I don't want
to come across as such. I, too, have seen a plethora of
informative posts in this forum; I've learned a lot from it.
--Paul
mfeik
October 28th, 2002, 04:12 PM
I agree!!!
Yves M
October 28th, 2002, 04:20 PM
Might I add that there is a feature on this message board called Private Messaging ? ;)
Well, sorry, a moderator has to remind such things from time to time ;)
Silver Ghost
October 28th, 2002, 10:06 PM
1. I am a Russian immigrant in the USA
2. I've lived here for less than 2 years
3. I wake up at 5:30 am
4. I take city bus at 6:45 and get to my college at 8:30
5. I take 20 credits at college, which are free (since I am a 15-year old Running Start student)
6. I stay at college until 6:00 p.m, study, do homework, classes, I don't have money for the lunch.
7. When I get home at about 8:30 - 9:00 p.m. I clean up the house, fix myself a dinner, and a breakfast.
8. I have about 1 hour for my own time, and I spend that hour learning C++ by a book, without having a computer at home.
9. I've been doing this for less than 2 years now (it's been Worse in Russia)
10. I take every single opportunity to hold on to my life, my GPA is 4.0 at the college.
It really hurts me, my heart, and my feelings to hear someone say that I am attempting to abuse my opportunity to access public resources like codeguru.com and ask for help at forums. I don't take any computer classes at college right now (but my major is computer science), since I also have to graduate from a High School.
Silver Ghost
October 28th, 2002, 10:08 PM
And btw, I asked this specific question, because I was reading about encrypting algorithms, and I simply wanted to create my own. If you don't believe me, here's what I've been thinking of, of course don't take it seriously, I am just another 15 year old dumb kid trying to make something out of himself...
SGCrypter - By Silver Ghost aka SG
I came up with idea on 10/26/02
NEW IDEA:
TESTING:
Starting out with string "Don't make me slap you foo! 10/26/02 Testing 0x15!"
1. String contains 50 characters (bytes).
2. The amount of digits in the key is the middle 10 bytes -> final_string = 50+10=60 bytes.
The first byte of those 10 is located by final_string/2
3. Suppose our key will have 10 digits => final_string = 60+10=70 bytes. This is final size:
" "
4. Locating the starting point of the amount of digits in the final_string: 70/2=35
5. Filling the string with the amount of digits:
" 0000000010 "
6. Calculating the position of the first byte of the key by a formula
if amount of digits in a key is even: pos = (pos_of_amount_of_digits(35)/2)-1
if amount of digits in a key is odd : pos = ((string_length*3/2) - (string_length*2/3))-_as_many_byes_that_it_goes_before_key_
7. Randomly generating a first byte of a key, but make sure, that
if it's odd: than position-that_byte >= 0 and != [randge of amount of digits] and != pos_of_any_other_byte
// since the next byte will be located at position-that_byte
if it's even: that position+that_byte < 70 and != [randge of amount of digits] and != pos_of_any_other_byte
// since the next byte will be located at position+that_byte
8. Position for the first byte is (35/2)-1 = 17-1 = 16 (see 6)
9. Randomly generated byte is 4 (satisfies all conditions of 7). String looks like:
" 4 0000000010 "
10. Next bytes position will be byte's_position+byte = 16+4 = 20
11. Randomly generating a byte. Say 9, which satifies all the conditions of 7. The string:
" 4 9 0000000010 "
12. Next bytes position will be byte's_position-byte(since odd) = 20 - 9 = 11
13. Randomly generating a byte. Say 1, which satisfies all the conditions of 7. The string:
" 1 4 9 0000000010 "
14. Next bytes position will be byte's_position-byte = 11-1 = 10
15. Randomly generating a byte. Say 6 - DOES NOT satisfy 7. Try 5 - works:
" 51 4 9 0000000010 "
16. Next bytes position will be byte's_position-byte(since odd) = 10 - 5 = 5
17. Randomly generating a byte. Say 8 - works:
" 8 51 4 9 0000000010 "
19. In the same way generating the rest of the bytes (already have 5):
6. 5+8 = 13 (pos); byte = 6, string:
" 8 51 6 4 9 0000000010 "
7. 13+6 = 19 (pos); byte = 8, string:
" 8 51 6 4 89 0000000010 "
8. 19+8 = 27 (pos); byte = 5, string:
" 8 51 6 4 89 5 0000000010 "
9. 27-5 = 22 (pos); byte = 3, no 5 - yes:
" 8 51 6 4 89 5 5 0000000010 "
10. 22-5 = 18; byte = 2 - yes ( since its the last byte - it don't have to satisfy 3rd condition of 7.
" 8 51 6 4 289 5 5 0000000010 "
20. Filling the final string with original one:
"Don't make me slap you foo! 10/26/02 Testing 0x15!" (original)
"Don'8t ma51k6e 4m289e5 sla5p you f0000000010oo! 10/26/02 Testing 0x15!" (final)
21. Key is: 4915868552
22. Reversign the key: 2558685194
22. Now we can jack all the bytes (except for the key bytes in an old-fashioned key-phasing.
IDEA:
Encryption - takes the string, creates a new one of size (old string + key_bytes),
where key_bytes is the length of the key ( 12345678 has a length of 8 ). Than it calculates
the position of the last byte of the key by a formula like
pos = (string_length*3/2) - (string_length*2/3)
After that, if that byte is even, than the next piece of key is + that byte (0-f), if the
byte is odd, than the next piece of key is is - that byte. For example, we recieve a line:
"Hi How Are You Doing? 123 helo!"(31 bytes) . Suppose we want to use a 3-digit key. The length
of a new string would be 31+3=34 byts. Than the position for the first element would be
34*3/2-34*2/3=11 . The byte is generated randomly, but we have to take in concern, that if
it's odd, it can't be more than 11, and if it's even, it can't be more than 34-11=23. Suppose
the generated key would be 5. The line now looks like:
" 5 "
We record the position of the byte (so that later we can't accidently put a new byte into
that place), and start calculating a new byte. Sice the first byte is odd, this one should
be in the 11-5=6th position in the string. Suppose a random generated byte is 8. The line will
look like this:
" 8 5 "
The next byte must be 6+8=14th position in the string. Suppose randomly generated byte is 3.
If our key was more than 3 digits, we wouldn't accept this number, since it'd point to the 11th
element which is already taken. But since it's the last element - we can accept it. The string
now looks like:
" 8 5 3 "
Now we can fill it with the old array:
"Hi Ho8w Ar5e 3You Doing? 123helo!"
They key is: 583. Now we reverse it: 385, and do the old type of encrypting for each element,
except each key element. Viola!
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.