LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-28-2008, 06:06 AM   #1
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Rep: Reputation: 38
how do i write a asci-char withthe number?


is it like this 0x48 for 0?
 
Old 03-28-2008, 06:09 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
In which language?
 
Old 03-28-2008, 06:15 AM   #3
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Original Poster
Rep: Reputation: 38
cccccccccccccccc
 
Old 03-28-2008, 06:55 AM   #4
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Original Poster
Rep: Reputation: 38
ok I found it its \048 for 0
 
Old 03-28-2008, 06:59 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
or simply print a number as a character:
Code:
printf("%c\n",48);
 
Old 03-28-2008, 07:06 AM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
You can see a list of ASCII codes in the ascii manual page:
Code:
man ascii
Octal values in C can be written in 0NNN format, hex in 0xNN, and decimal as normal. You can also just write the character in single quotes.
To illustrate:
Code:
#include <stdio.h>

int main(int argc, char** argv)
{
        if ('A' == 65)   { printf("sane decimal\n"); }
        if ('A' == 0x41) { printf("sane hex\n"); }
        if ('A' == 0101) { printf("sane octal\n"); }
        return 0;
}
 
Old 03-28-2008, 07:08 AM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Quote:
Originally Posted by colucix View Post
or simply print a number as a character:
Code:
printf("%c\n",48);
And you can do this too:
Code:
#include <stdio.h>

int main(int argc, char** argv)
{
        printf("char=%c dec=%d hex=%x octal=%o\n", 'A', 'A', 'A', 'A');
        return 0;
}
 
Old 03-28-2008, 07:50 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Thank you, matthewg42 for the clarification. I also discovered that you can do the opposite:
Code:
#include <stdio.h>

int main(int argc, char** argv)
{
        printf("%c %c %c\n",0101,0x41,65);
        return 0;
}
I thought it wasn't possible.
 
Old 03-28-2008, 04:28 PM   #9
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
The ASCII values for the digits 0-9 were chosen to coincide with the binary values of the digit prefixed with the binary 0011. So for a digit a from 0 to 9, its ASCII representation is 0x30 + a (e.g., for 4 it’s 0x34).

This gives you an easy way to obtain the digit itself as well. If you are confident that an ASCII char is holding a digit, then to get the digit, you just ^ (XOR) with 0x30.
 
Old 03-29-2008, 07:26 AM   #10
simplicissimus
Registered User
 
Registered: Mar 2008
Posts: 104

Rep: Reputation: 15
'kalleanka' - isn't that the finnish name for Donald Duck?
 
Old 03-29-2008, 09:58 AM   #11
exscape
Member
 
Registered: Aug 2007
Location: Sweden
Distribution: OS X, Gentoo, FreeBSD
Posts: 82

Rep: Reputation: 15
Quote:
Originally Posted by simplicissimus View Post
'kalleanka' - isn't that the finnish name for Donald Duck?
Swedish
(And no, the languages are NOT similar - I barely know five finnish words!)
 
Old 04-28-2008, 12:53 PM   #12
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Original Poster
Rep: Reputation: 38
AKU ANKKA is the finnish.

Kalla Anka swedish.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
conversion from ‘const char*’ to ‘unsigned char*’ rubadub Programming 2 02-08-2008 05:45 PM
about C++ invalid conversion from 'const char*' to 'char' teoporta Programming 3 07-17-2007 09:24 AM
If I get invalid conversion from `const char*' to `char' what should I be lookin for? RHLinuxGUY Programming 5 03-12-2006 10:35 PM
How do I add character+number to get char in C++? needforspeed Programming 10 02-10-2004 12:16 AM
Linux - ASCI - Windows, proper file format issue aaronluke Linux - General 1 11-07-2002 10:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration