Hello. Im reading file using FILE and reading that using fgetc to read that.
fgetc function returns me int value of my chars in ASCII.
Now i want to print that data in char values.
How to convert my ascii numbers to chars?
You most likely don't need any conversion. If the native character set on your system is ASCII (which is the most common) then no conversion is needed. 'A' == 65 etc.
That means to print a character you just print it, with e.g. putchar or printf or any other function that allows you to print characters.
int x = 48;
printf("%c", x);
it will print 0, also you can do this
int x = 48;
char xx = (char)x;
Specify format as for a char, like so:
printf("%c", number);
printf("%c", 65); // A
#include <stdio.h>
int main()
{
int i;
for (i=97; i <=200 ; i++)
{
printf("%d %c,\t",i,i);
};
return 0;}
This will give you the whole chart upto 200.
Related
I was trying to make this int to char program. The +'0' in the do while loop wont convert the int value to ascii, whereas, +'0' in main is converting. I have tried many statements, but it won't work in convert() .
#include<stdio.h>
#include<string.h>
void convert(int input,char s[]);
void reverse(char s[]);
int main()
{
int input;
char string[5];
//prcharf("enter int\n");
printf("enter int\n");
scanf("%d",&input);
convert(input,string);
printf("Converted Input is : %s\n",string);
int i=54;
printf("%c\n",(i+'0')); //This give ascii char value of int
printf("out\n");
}
void convert(int input,char s[])
{
int sign,i=0;
char d;
if((sign=input)<0)
input=-input;
do
{
s[i++]='0'+input%10;//but this gives int only
} while((input/=10)>0);
if(sign<0)
s[i++]='-';
s[i]=EOF;
reverse(s);
}
void reverse(char s[])
{
int i,j;
char temp;
for(i=0,j=strlen(s)-1;i<j;i++,j--)
{
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
Output screenshot
Code screenshot
The +'0' in the do while loop wont convert the int value to ascii
Your own screenshot shows otherwise (assuming an ASCII-based terminal).
Your code printed 56, so it printed the bytes 0x35 and 0x36, so string[0] and string[1] contain 0x35 and 0x36 respectively, and 0x35 and 0x36 are the ASCII encodings of 5 and 6 respectively.
You can also verify this by printing the elements of string individually.
for (int i=0; string[i]; ++i)
printf("%02X ", string[i]);
printf("\n");
I tried your program and it is working for the most part. I get some goofy output because of this line:
s[i]=EOF;
EOF is a negative integer macro that represents "End Of File." Its actual value is implementation defined. It appears what you actually want is a null terminator:
s[i]='\0';
That will remove any goofy characters in the output.
I would also make that string in main a little bigger. No reason we couldn't use something like
char string[12];
I would use a bare minimum of 12 which will cover you to a 32 bit INT_MAX with sign.
EDIT
It appears (based on all the comments) you may be actually trying to make a program that simply outputs characters using numeric ascii values. What the convert function actually does is converts an integer to a string representation of that integer. For example:
int num = 123; /* Integer input */
char str_num[12] = "123"; /* char array output */
convert is basically a manual implementation of itoa.
If you are trying to simply output characters given ascii codes, this is a much simpler program. First, you should understand that this code here is a mis-interpretation of what convert was trying to do:
int i=54;
printf("%c\n",(i+'0'));
The point of adding '0' previously, was to convert single digit integers to their ascii code version. For reference, use this: asciitable. For example if you wanted to convert the integer 4 to a character '4', you would add 4 to '0' which is ascii code 48 to get 52. 52 being the ascii code for the character '4'. To print out the character that is represented by ascii code, the solution is much more straightforward. As others have stated in the comments, char is a essentially a numeric type already. This will give you the desired behavior:
int i = 102 /* The actual ascii value of 'f' */
printf("%c\n", i);
That will work, but to be safe that should be cast to type char. Whether or not this is redundant may be implementation defined. I do believe that sending incorrect types to printf is undefined behavior whether it works in this case or not. Safe version:
printf("%c\n", (char) i);
So you can write the entire program in main since there is no need for the convert function:
int main()
{
/* Make initialization a habit */
int input = 0;
/* Loop through until we get a value between 0-127 */
do {
printf("enter int\n");
scanf("%d",&input);
} while (input < 0 || input > 127);
printf("Converted Input is : %c\n", (char)input);
}
We don't want anything outside of 0-127. char has a range of 256 bits (AKA a byte!) and spans from -127 to 127. If you wanted literal interpretation of higher characters, you could use unsigned char (0-255). This is undesirable on the linux terminal which is likely expecting UTF-8 characters. Values above 127 will be represent portions of multi-byte characters. If you wanted to support this, you will need a char[] and the code will become a lot more complex.
I am new to C programming and trying to make a program to add up the digits from the input like this:
input = 12345 <= 5 digit
output = 15 <= add up digit
I try to convert the char index to int but it dosent seems to work! Can anyone help?
Here's my code:
#include <stdio.h>
#include <string.h>
int main(){
char nilai[5];
int j,length,nilai_asli=0,i;
printf("nilai: ");
scanf("%s",&nilai);
length = strlen(nilai);
for(i=0; i<length; i++){
int nilai1 = nilai[i];
printf("%d",nilai1);
}
}
Output:
nilai: 12345
4950515253
You have two problems with the code you show.
First lets talk about the problem you ask about... You display the encoded character value. All characters in C are encoded in one way or another. The most common encoding scheme is called ASCII where the digits are encoded with '0' starting at 48 up to '9' at 57.
Using this knowledge it should be quite easy to figure out a way to convert a digit character to the integer value of the digit: Subtract the character '0'. As in
int nilai1 = nilai[i] - '0'; // "Convert" digit character to its integer value
Now for the second problem: Strings in C are really called null-terminated byte strings. That null-terminated bit is quite important, and all strings functions (like strlen) will look for that to know when the string ends.
When you input five character for the scanf call, the scanf function will write the null-terminator on the sixth position in the five-element array. That is out of bounds and leads to undefined behavior.
You can solve this by either making the array longer, or by telling scanf not to write more characters into the array than it can actually fit:
scanf("%4s", nilai); // Read at most four characters
// which will fit with the terminator in a five-element array
First of all, your buffer isn't big enough. String input is null-terminated, so if you want to read in your output 12345 of 5 numbers, you need a buffer of at least 6 chars:
char nilai[6];
And if your input is bigger than 5 chars, then your buffer has to be bigger, too.
But the problem with adding up the digits is that you're not actually adding up anything. You're just assigning to int nilai1 over and over and discarding the result. Instead, put int nilai1 before the loop and increase it in the loop. Also, to convert from a char to the int it represents, subtract '0'. All in all this part should look like this:
int nilai1 = 0;
for (i = 0; i < length; i++) {
nilai1 += nilai[i] - '0';
}
printf("%d\n", nilai1);
For starters according to the C Standard the function main without parameters shall be declared like
int main( void )
This character array
char nilai[5];
can not contain a string with 5 digits. Declare the array with at least one more character to store the terminating zero of a string.
char nilai[6];
In the call of scanf
scanf("%s",&nilai);
remove the operator & before the name nilai. And such a call is unsafe. You could use for example the standard function fgets.
This call
length = strlen(nilai);
is redundant and moreover the variable length should be declared having the type size_t.
This loop
for(i=0; i<length; i++){
int nilai1 = nilai[i];
printf("%d",nilai1);
}
entirely does not make sense.
The program can look the following way
#include <stdio.h>
#include <ctype.h>
int main(void)
{
enum { N = 6 };
char nilai[N];
printf( "nilai: ");
fgets( nilai, sizeof( nilai ), stdin );
int nilai1 = 0;
for ( const char *p = nilai; *p != '\0'; ++p )
{
if ( isdigit( ( unsigned char ) *p ) ) nilai1 += *p - '0';
}
printf( "%d\n", nilai1 );
return 0;
}
Its output might look like
nilai: 12345
15
I'm reading a text file using getchar() and I'm need to print all the integers in a file. If the file looks like:
hello 42 world
I know how to find single digit integers because getchar() gives me the ascii value of each character in the file so I need to look for values between 48 and 57 (ascii values for 0-9) but I can't figure out how to do this for n digits. In the example above I would find the values 52 and 50 for "42" but how can I turn that into 42?
I thought about creating a char array and when I find a digit put it in the array and then use atoi() to convert the string to an int but it feels like a bad solution, if it would even work.
Gene's approach packed in a program:
#include <stdio.h>
#include <ctype.h>
main()
{
int ch;
do
{
int val = 0, digits = 0;
while (isdigit(ch = getchar())) ++digits, val = 10 * val + (ch - '0');
if (digits) printf("%d\n", val);
} while (ch != EOF);
return 0;
}
How can I get the Unicode code point for a character? Here is what I have tried, but it is not printing the same character, Am I properley understanding how unicode works?
How can I get the value of a unicode character?
#include <stdio.h>
int main()
{
char *a = "ā";
int n;
while(a[n] != '\0')
{
printf("%x", a[n]);
n+=1;
}
printf("\n \uC481");
return 0;
}
In the first place, there are few corrections in your code.
#include <stdio.h>
int main()
{
char *a = "ā";
int n = 0; //Initialize n with zero.
while(a[n] != '\0')
{
printf("%x", a[n]);
n+=1;
}
//\u will not work. To print hexadecimal value, use \x
printf("\n %X\n\", 0xC481);
return 0;
}
Here, you are trying to print hex value of each byte. This will be not a Unicode value of character beyond 0xff.
unsigned short is the most common data structure used to store Unicode value although it cannot store all the code points. If you need to store all the Unicode points as it is, then use int which must be 32-bit.
Unicode value of a character is numeric value of each character when it is represented in UTF-32. Otherwise, you will have to compute from the byte sequence if encoding is UTF-8 or UTF-16.
I am trying to figure out how to print the ascii value of a character in binary. Here is what I have done so far, but it does not work at all and I dont know why. Can someone of you C wizards help me??
#include <stdio.h>
int main(int argc, char** argv)
{
char myChar;
printf("Enter a character:\n");
scanf("%c", &myChar);
printf("Your character is %c\n", myChar);
printf("ASCII in BIN: %c\n", toBinary(myChar));
return 0;
}
char* toBinary(int decimalNumber)
{
char binaryValue[7] = "";
for (int i = sizeof(binaryValue); i >= 0; ++i)
{
int remainder = decimalNumber % 2;
decimalNumber = decimalNumber / 2;
binaryValue[i] = remainder;
}
return &binaryValue;
}
The %c format string will always interpret the corresponding printf argument as a character. In this case, its probably not what you want.
printf("ASCII in BIN: %d\n", myChar);
will print out the ascii code point of myChar just by telling printf to treat it as a number.
If you'd like to print out the string being returned by your toBinary function, you can do that with
printf("ASCII in BIN: %s\n", toBinary(myChar));
There's a good reference of the various % codes and what they mean here.
However, it's also worth noting that your toBinary function probably doesn't do what you want. This loop condition:
for (int i = sizeof(binaryValue); i >= 0; ++i)
will start at sizeof(int) and count up until it runs out of integers, then stop only because INT_MAX + 1 == INT_MIN. Because you're using i as an array index, this will almost certainly crash your program.
You also need to make sure to terminate the string you're generating with a '\0', as that is how subsequent calls to printf will recognize the string has ended.
And, as noted in other answers, your toBinary implementation is also returning a pointer to a memory address that will get automatically deleted as soon as toBinary returns.
You are returning address of local array.
This is both incorrect return type (char ** vs char *) and a bad thing to do at all.
printf("ASCII in BIN: %c\n", toBinary(myChar));
%c is to print a character.
And, there is no BOOL type in C, so you can change it in to a string, and pass the pointer to an array to printf("%s", pointer)