|
-
decrypting numbers to letters
Is there any way I can turn an integer like 26 into an ascii character without doing 127 case statements to see what character the number 26 represents? Using C++. Thanks!
-
Reef Shark
Well there is an itoa function that converts an integer to either a string or acii characters, i forget which. The syntax is itoa(number,char *, radix), wherer number is the number you want to convert, car * is the C-style string you want to convert it to, and radix is the base(10 for our case). Hope this helps.
Edit: Most c++ compilers may not support this function since it is not ANSI standard C(or c++).
Last edited by bocybo; 02-16-2004 at 12:49 AM.
Reed's sister is sooo hot!
-
That's not what he wants, I don't think. Well, in C++ anyway, ints and chars are the same basic type, so you could just do something like ('A' + x - 1). But that's dependant on the charset. The absolute safest way is going to be with the case statement for sure. (or an array would probably be better.. i don't know how a case is implemented, maybe it's the same thing) Like this:
char bob[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char ch = bob[x-1];
-
Hammerhead Shark
Sounds like he just needs to cast...
int ascii = 26;
char ch = (char) ascii;
Stay cool
and be somebody's fool this year
-
Oh I see.. I was thinking that he wanted the number 26 to correspond with 'z'. Confused me, since there are 26 letters in the alphabet...
-
if you are doing assignment for ECE628, PM me bcoz i have wrote the program to solve problem #4 (took me like 10 hrs) >:-(
But i dont hve the solution for #3 yet :P
DHAHL3seasons GP:73 G:121 A:55 Pts:176 GWG:12 +/-:184
UWSWA6seasons GP:41 G:53 A:46 Pts:99 GWG:5 +/-:-25
MCBHL3seasons GP:14 G:20 A:8 Pts:28 GWG:4 +/-:19
uwcdc.com or monkis.com
-
Last edited by lms; 02-19-2004 at 10:13 PM.
-
thats just a first draft, but it works perfect so far, you can use it if you like. But i suggest using itoa() unless this is for some assignment. Also dont confuse char with int (notice i did not need to use any type casts), they are really the same thing, you can use an integer array to store a string if you want, you just have to make sure each number in the array is a real ascii number, look up an ascii table, the numbers start with 30h and end on 39h
Last edited by lms; 02-18-2004 at 07:58 PM.
-
int and char aren't really the same: sizeof(char) tends to be one byte, sizeof(int) tends to be 4. Don't try to store ints in characters. You don't have to do casts because they happen automatically.
Why void main() ?
-
A char is typically 1 byte (8 bits) and an int is 32 bits or 4 bytes, yes they are different sizes, but what people get confused about is that char's are reserved ONLY for letters and ints only for integers, its completely arbitrary and its up to the programmers to use the data types as he or she wishes. That is what I was trying to explain.
why void main()?
You can make up your own function name and parameters, I merely showed you one way how to convert a number into a string, apparently it was now what the guy asked for, perhaps he can be more specific in the future.
-
Reef Shark
hmmm but void main() isn't the c++ standard method, meaning void main() could stop working some day. To be sure you should ALWAYS have int as the return time for main even if you dont need to return anything.
Reed's sister is sooo hot!
-
Hammerhead Shark
couldn't you just make a for loop?
Code:
char num=0;
for(int i=0;i<200;i++)
{
cout << i << endl;
num=i;
}
Amd Athlon Mobile 2600+ (200 X 12.5 @ 1.7)
Asus A7N8X-Deluxe (Not Rev. 2)
2-80gig WD Hard drives
Corsair XMS pc3200 - 512megs
Saphire Radeon 9800pro
Soundblaster Live Platinum 5.1
3Com EtherLink 10/100 3CR990-TX-97
Creative Blaster v.92 Modem
Sony Trinitron 21inch Monitor
Plextor 24X Burnproof Cd-Writer
Pioneer 16X DVD Drive
Klipsch Ultras 5.1 Surround Sound
Sony 3.5inch Floppy Drive
Windows 2k of course...
-------------------------
Bart: Hey! Why is it killing my toys?
Lisa: They must have programed it to destroy the competition.
Bart: Sort of like Microsoft!
-------------------------
   *HAVE A NICE DAY*   
-
im sorry i posted the example
Last edited by lms; 02-20-2004 at 02:07 AM.
-
Reef Shark
Originally posted by lms
im sorry i posted the example
Just finished reading this thread, and I gotta say it was annoying to not know what everyone was talking about because you edited your post and deleted the example.
- 1.5GHZ Pentium M | 768MB PC2700 | ATI Radeon 9600 pro 128MB Turbo | 60GB 7200RPM | 4X DVD+RW | 15.4" WSXGA+ | 802.11b | Logitech MX310 | XP Pro | Slackware 10
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|