Geekpedia Programming Tutorials






Transforming a string into ASCII

Transforming a string into ASCII code using int() and the 'while' loop. Good for beginners in programming and C++.

On Monday, March 15th 2004 at 07:22 PM
By Andrew Pociu (View Profile)
****-   (Rated 3.5 with 18 votes)
Contextual Ads
More C++ Resources
Advertisement

ASCII = computer data exchange standard: a standard that identifies the letters of the alphabet, numbers, and various symbols by code numbers for exchanging data between different computer systems.
Full form American Standard Code for Information Interchange

Microsoft® Encarta® Reference Library 2003. © 1993-2002 Microsoft Corporation. All rights reserved.

Every character has a different number under which it is stored on your computer. There are 256 different ASCII codes (the possible values a byte can hold), that is why a character takes one byte.


// Transform a word into ASCII code

#include <iostream>
using namespace std;
int main()
{
    char word[32];
    int x = 0;
    cout << "Please enter the word (maximum 32 characters):\n";
    cin >> word;
    cout << "The ASCII for this word is:\n";
    while (word[x] != '\0')    // While the string isn't at the end...
    {
        cout << int(word[x]);    // Transform the char to int
        x++;
    }
    cout << "\n";
    return 0;
}


With ‘while’ we loop trough the array and convert every character using int(word[x]) to its ASCII value. If we hit the ‘\0’ (end of the string), the while loop returns false and ends.


There is more to say about ASCII than I said here... Google can help you .
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by Ernest Pazera on Wednesday, March 17th 2004 at 07:28 PM

You are simply promoting a char (a NUMERIC type) into an int (also a numeric type). There is no difference between 'A' and 65, and hence there really is no conversion from a character to its ASCII value. A character *IS* it's ASCII value. The only conversion going on here is by cout, which is converting your int value into a human readable string of characters.

by Andrei Pociu on Wednesday, April 7th 2004 at 10:35 PM

Of course the character is stored in the computer using its ASCII code, I said that at the beginning.
When we use 'cout' the ASCII code is converted to a human readable character.
But when we want to see the ASCII value of the character we convert the character back to its ASCII value.
That's what I meant and I think it's clear enough.

by issa on Thursday, July 15th 2004 at 02:29 AM

how to transforming a ASCII into string?

by Andrei Pociu on Friday, July 16th 2004 at 02:58 AM

An integer is at least 4 bytes and a char is one byte, so be careful when transforming.

int i = 65;
cout << "Character: " << char(i) << "\n";

by lubna on Thursday, November 11th 2004 at 06:45 AM

how can i convert integer into Ascii code in c++

by Andrei Pociu on Thursday, November 11th 2004 at 07:02 AM

string str = new string((Char)65, 1);

// 65 in ASCII is character 'A'.

Good luck!

by lubna on Monday, November 22nd 2004 at 07:11 AM

my program is : input string and numbers of characters and display right location of string through the numbers of characters which is input by user?
this program is very difficult for me to make it,can u help me?

by Andrei Pociu on Monday, November 22nd 2004 at 10:27 AM

I'm positive I can, email me with further details.

by IVAN on Sunday, September 25th 2005 at 01:48 PM

Hello!!!
How can I convert integer into char in C#???

by Roger Fischer on Wednesday, October 5th 2005 at 11:56 AM

OK ... I can see this in C++ ( integer casting to the output stream ) ... How can I transform an input character or (byte stream) into their actual hex bytes in Delphi ? In other words, if the character 'R' is seen, I want '52' the hex ASCII value for this character ...

by rk on Wednesday, October 5th 2005 at 04:25 PM

Hi, I was wondering if anyone knows how to convert a string into its ascii value in c? thanks

by Mo on Sunday, December 4th 2005 at 08:04 PM

Hi Andrei Pociu,
i tried converting
X
X1
X2
X3
X4
X5
X6
X7
X8
X9
X10
V
W
Y
Z
V1
V2
V3
V4
V5
ANGLE
B27
B25
B
Q
Q5
Q17
PI
PHI
O
O1
O2
ONER
O3
O4
ONE
VAR
PLY
AR
TRY
CRY
FRY
DRY
PRY
ORY
WRY
COY
BOY
BUY
to ASCII but it only converts X, X1, X10 and ANGLE. Do you know why this happens

by Mo on Sunday, December 4th 2005 at 09:12 PM

Never mind, i think i got it. By the way i think this website is really helpful.

by Tapan on Tuesday, January 10th 2006 at 06:01 AM

Plz give me suggestion how to convert string to integer using CInt keyword through asp 3.0

by bng on Sunday, January 22nd 2006 at 02:53 AM

I stll don't know how to do it. Can you explan line by line in more detail? Thanks.

by aliasunway on Thursday, January 26th 2006 at 07:07 AM

Hai.
can anyone help me on how to convert from int to char in C++ ?

by joseph on Tuesday, February 28th 2006 at 07:15 AM

hi.. i just need some help.. i don't know how to use uhm you know the kbhit() and getch() properly i need to know what was key pressed in my program.. pls help

by Letish on Sunday, March 26th 2006 at 03:16 PM

I know this thread is probably dead by now but I need to know how to represent the carriage return "ASCII 13" in a while loop ie (if CT != "ASCII 13")

Any ideas?

by Andrei Pociu on Sunday, March 26th 2006 at 03:20 PM

Are you sure you don't want to check for "\r\n\" - which is a carriage return in C#?

by Nelson Siu on Wednesday, April 12th 2006 at 06:22 AM

Transform each letter to the corresponding ASCII value.

For example, if the file contains “What?” then

‘W’ corresponds to 87

‘h’ corresponds to 104

‘a’ corresponds to 97

‘t’ corresponds to 116

and also Calculate and output r1=the sum of all obtained integers (e.g. r1=87+104+97+116=404)

by Nelson Siu on Wednesday, April 12th 2006 at 06:23 AM

From my previous comment how do i do that

by aya vallez on Tuesday, April 18th 2006 at 02:01 AM

hi!! how is scientific notation express in E notation???

by gaz on Thursday, April 27th 2006 at 02:57 AM

how would you:
Transform each letter to the corresponding ASCII value.

For example, if the file contains “What?” then

‘W’ corresponds to 87

‘h’ corresponds to 104

‘a’ corresponds to 97

‘t’ corresponds to 116

and also Calculate and output r1=the sum of all obtained integers (e.g. r1=87+104+97+116=404)

by Ajeya on Wednesday, May 3rd 2006 at 12:52 PM

I would like to know how to make a program to make \"Ctrl\" into a number using ascii. Is it possible?
I want to make one of those programs wherein the user has to press \"Ctrl\" to exit.
If anyone can tell me I would be really glad. Thanx.

by mikyz on Friday, May 12th 2006 at 09:29 AM

hi!
can somebody pliz show me how to write a program to change the numeric value int words from zero to ten thousand i.e when you enter 10 value,it will give in words \"ten\"
pliz e-mail me at makaya@webmail.co.za, if theres anyone

by kavita on Tuesday, June 13th 2006 at 06:37 AM

how to convert an integer value in ascii value

by shakti on Monday, July 10th 2006 at 05:55 AM

how do u check for title case in C#??

by Gangakhedkar on Friday, July 28th 2006 at 12:35 AM

how can I get the ascii value of a given char.
like function('a') should return 97

thanks in advance.
Vamshi.

by William on Friday, August 18th 2006 at 06:04 AM

The in built function in C/C++ can help you out. The function is:
int __toascii(int a);

Say u want the ascii value of 'a';

int x = __toascii('a');

x will contain the ascii value of 'a', i.e. 97

by red on Monday, February 12th 2007 at 02:09 AM

bitches

by kenny b on Tuesday, March 6th 2007 at 06:38 AM

there\'s a whole lot of those bitches who dont read through the page and keep posting the same questions.
and i hate people asking for help in short form. superficial.

by rhaq on Monday, June 4th 2007 at 01:45 PM

Hello,

I am trying to undo the operation in this one line of code...my problem is a bit similar to ones mentioned above for char to ascii conversion and vice versa... it is converting a string into ascii code in java by:
int = int.shiftLeft(8).add(ascii value of a char).
I am trying to convert the ascii code back into the string, and would really appreciate some help! thanks.

by mansi on Thursday, July 26th 2007 at 08:36 AM

hi
i have to make a pgm that reads from a file(having character strings,integers and float values) ans separate them into columns based on their type.

please help
im aware of c++

thanks a lot

by anju on Friday, August 24th 2007 at 02:02 AM

hello sir
I have a problem related to my project.
in this i have to convert a hexstring to an ascii string .
and all string are converted but my conversion function is unable to convert a eg-9a hex string to the aproprate one but in place of answer it give wrong ascii code like?

so plese sir/mam help me my code conversion is

private static String toAscii(String hexString) {

if((hexString.length() % 2) != 0) {
throw new RuntimeException("String not comprised of Hex digit pairs.");
}
char[] chars = new char[hexString.length()];
char[] convChars = new char[hexString.length()/2];
hexString.getChars(0, hexString.length(), chars, 0);
StringBuffer buff = new StringBuffer();
for(int i=0;i<hexString.length();i+=2) {
String hexToken = new String(chars, i, 2);
System.out.println(hexToken);
convChars[i/2] = (char)Integer.parseInt(hexToken, 16);
int tt=Integer.parseInt(hexToken, 16);
System.out.println("hex"+hexToken +tt);
}
return new String(convChars);
}

by Vinay Bohara on Sunday, October 14th 2007 at 08:56 AM

I have a string which has ascii values. I want this complete string to be dispalyed in character format.. how do i do it?

Please help...!

Thanks a lot.
Vinay B.

by Muhammad Awais on Thursday, August 28th 2008 at 10:00 PM

thanks. your program to convert string into ASCII is really helpful for me. can you give me the program to convert a string into binary(of ASCII).
thanks with regards

by Burak on Tuesday, January 6th 2009 at 06:08 AM

thanks a lot it was very useful for me.

by B Chowdhury on Friday, February 13th 2009 at 02:23 PM

Hello, Real friends,can you mention me about how ican convert 10110510997496770 into its EILA3ad equivalents or ascii string.C or C .

by Ernest on Wednesday, March 18th 2009 at 05:13 AM

please just send to me more code about c

by Nim on Tuesday, April 21st 2009 at 11:40 PM

Hi
I have this task can anyone please help me!

(A) Read the first name and the surname of a person.
Calculate and output n1 = the integer representing the first letter (counting
from the left) of the first name in the ASCII table.
For example, if the first name is “James”, then n1=74 (i.e. the value representing ‘J’ is
74 in the ASCII table).
Note that there is no restriction that the first letter entered must be a capital letter.

(B) Calculate and display n2 = the integer representing the first letter of the
surname in the ASCII table.
For example, if the surname is “Bond”, then n2 = 66 (i.e. the value representing ‘B’ is
66 in the ASCII table).

(C) Calculate and display n3 = the squared digit length of n1.
The following process determines the squared digit length of an integer. Take any
integer and add up the squares of its digits. This will give you another integer. Repeat
this procedure until the number you end up with is 1 or 4. The number of times this
process has to be repeated before it gets to 1 or 4 is the squared digit length. For
example, if we start with 85, we get:
82 52 = 89
82 92 = 145
12 42 52 = 42
42 22 = 20
22 02 = 4
This process shows that the squared digit length of 85 is 5.
According to the experts, this process will always eventually reach either 1 or 4. The
squared digit length of 1 and 4 is zero, since we don't actually have to apply the
process to them to reach the stopping condition (interestingly, though, if we did apply
the process, to 1, we would keep getting 1, but if we applied it to 4, we would get a
repeating sequence: 4, 16, 37, 58, 89, 145, 42, 20, 4).

(D) Calculate and display n4 = the squared digit length of n2.
(E) Calculate and display n5 = the largest prime factor of n3 n4.
A prime factor of integer n is a factor of n which is a prime number. A prime number
is any integer greater than 1 and only divisible by itself and 1 (e.g. 2, 3, 5, 7, 11, 13,
17 etc). For example, 3 is the largest prime factor of 27 and 7 is the largest prime
factor of 49.

by moomii on Sunday, April 26th 2009 at 09:54 AM

Hi
I have this task can anyone please help me!

Write a C program that accepts a character using the scanf() function. Is the character is a lowercase, convert it to uppercase and display the letter in its upper form. If the character is a upper letter, convert the letter to lowercase and display the letter in its lower form. If the character entered is not an alphabet (!@#$% 5 or any other non alphabet), display 0. (Hint: subtracting the integer value 32 from a lowercase letter yields the code for the equivalent uppercase letter. Thus, ‘A’=’a’-32

by moomii on Sunday, April 26th 2009 at 09:59 AM

Hi
I also have this task can anyone please help me!

Print the decimal, octal and hexadecimal values of all the characters between the start and stop characters entered by user. For example, if the user enters a and z, the program should print all the characters between a and z their respective numerical values. Make sure that the second character entered by the user occurs later in the alphabet than the first character. If it does not, write a loop that repeatedly asks the user for a valid second character until one is entered.


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C++ Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons