aaroncohn
02-15-2004, 01:54 AM
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!
|
Click to See Complete Forum and Search --> : decrypting numbers to letters aaroncohn 02-15-2004, 01:54 AM 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! bocybo 02-15-2004, 11:48 PM 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++). Strogian 02-16-2004, 09:52 AM 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]; EverlastingGod 02-16-2004, 12:06 PM Sounds like he just needs to cast... int ascii = 26; char ch = (char) ascii; Strogian 02-16-2004, 12:24 PM 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... :D namgor 02-16-2004, 02:48 PM 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 lms 02-18-2004, 06:47 PM nt lms 02-18-2004, 06:48 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 stoo 02-19-2004, 05:17 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() ? lms 02-19-2004, 08:09 PM 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. bocybo 02-19-2004, 08:51 PM 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. m316foley 02-19-2004, 09:11 PM couldn't you just make a for loop? char num=0; for(int i=0;i<200;i++) { cout << i << endl; num=i; } lms 02-19-2004, 09:13 PM im sorry i posted the example barton boi 03-06-2004, 04:14 AM 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. :( SharkyExtreme.com
Copyright Internet.com Inc. All Rights Reserved. |