If you aren\'t interested in converting from an int to a string (from int 127 to string \"127\"), and only want to convert to an ASCII value, then you only need to cast from an int to a char.
What\'s casting? Casting is when we explicitly convert from one primitve data type, or a class, to another. Here\'s a brief example.
public class int_to_char { public static void main(String args[]) { int a = 65; char myChar = (char) a; // cast from int to char
System.out.println (\"Char - \" + myChar); } } In this example, we have a number (65), which represents ASCII character A. We cast the number from an integer to a char, converting it to the letter A. You could also reverse the cast if needed (converting the ASCII char into a number).
Det drejer sig om en int der skal skrives til en DB, hvor man kun kan skrive char*. Det vil sige at vi har en int = 127 som skal konverteres til string = 127 og som vi derefter kan konvertere til char* /Barb
itoa() fra char[] til int. tror ikke man behøver at konveterer prøv dette
char ch[10]; int x=8 string streng=x; strcpy(ch,streng.c_str()); cout<<ch<<endl;
dette skulle gerne virke, har prøvet det engang (har ikke testet). måske skal man include #include <stdlib.h> #include <stdio.h> ved brug af strcpy men prøv uden først.
Hov char* itoa() betyder fra char[] til int. tror ikke man behøver at konveterer i dit problem prøv dette
char ch*; int x=8 string streng=x; strcpy(ch,streng.c_str()); cout<<ch<<endl;
dette skulle gerne virke, har prøvet det engang (har ikke testet). måske skal man include #include <stdlib.h> #include <stdio.h> ved brug af strcpy men prøv uden først.
Hvis du bare skal have din int ned i en fil (med fast format, så du kan læse den igen), er det letteste at bruge fprintf() funktionen:
fprintf(filNavn, \"%015d\", minInt);
hermed vi din int altid fylde 15 karakterer i din fil, men foranstillede nuller. fprintf() har en variant til at gemme indholdet i et char[]:
sprintf(charVar, \"%015d\", minInt);
begge kræver
#include <stdio.h>
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.