how to directly access to show a int variable as char * or string?

Started by
25 comments, last by Sandman 18 years, 10 months ago
You've already been shown how to do this. Either use sprintf (C, non-typesafe) or stringstreams (C++, typesafe).

Enigma
Advertisement
Quote:Original post by Enigma
You've already been shown how to do this. Either use sprintf (C, non-typesafe) or stringstreams (C++, typesafe).

Enigma



but my code no work ,maybe some error exist.

so I define the question below. some one could give code work?

given a int i = 444;

how to show it on screen . or how to get a string or char * c = "444";

only allow using pointer to directly access memory to do this.
Quote:Original post by derek7
only allow using pointer to directly access memory to do this.
Why do you insist on doing that way, when you have been shown multiple ways that work already? If we knew more, we could help you more easily.

Why do you want to only use a pointer or direct memory access? There is absolutely no reason to do it this way, unless this is some kind of homework/test question designed to assess your knowledge of C/C++.

Enigma
Quote:Original post by derek7
given a int i = 444;
how to show it on screen . or how to get a string or char * c = "444";
only allow using pointer to directly access memory to do this.


you can't cast from int to char* and get a readable representation it just doesn't work that way. Use one of the proposed solutions and be done with it.

If you want to only handle the "max 3characters and it's ok to print them backwards and be highly platform dependant about the whole thing" I could show you a bithack to accomplish that but it still would require some extra work.
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
Quote:Original post by Enigma
Why do you want to only use a pointer or direct memory access? There is absolutely no reason to do it this way, unless this is some kind of homework/test question designed to assess your knowledge of C/C++.

Enigma


Yes,I just know computer how to handle this situation
Hmm I tried it, but I couldn't imagine much use for pointers, except character arrays.

#include <iostream>int main(){	// our number to convert	const int x = 1234;	// lets calculate the strings length first by counting the digits.	int str_length = 0;	int divided = x;         // the number in x will be	do {                     // repeatedly		divided /= 10;        // divided by 10,		str_length++;         // while increasing the digit count,	} while ( divided > 0 ); // until it reaches zero.	char* str = new char[str_length+1]; // this holds our string	str[str_length] = '\0';             // and the '\0' terminator.	divided = x; // to convert, we start over with the whole number.	// we go from the end of the array backwards to the start,	for ( int i = str_length-1; i >= 0; i-- ) {		const int rem = divided % 10;           // extracting the last digit		const char ch = (char) ('0' + rem);     // and finding its ASCII value,		str = ch;        // then put it into the array,		divided /= 10;      // and 'remove' the last digit.	};	std::cout << str;	delete[] str;	return 0;}


Umm, where are my + signs? They dissapeared from the preview [embarrass]
[edit]but only there![/edit]
Come on Derek -- each time I fall for helping you with your questions again. You never reply with the results. This time the answer has been given multiple times over and over.

Alas I fall again: we have an integer 65:
int i = 65;

We can interpret it in two ways: as the ordinal value 65 which is just 65 or as the character referred to via ASCII codes, which is 'A'. There is no difference in C/C++:
int i = 65;if ( i == 'A' )  printf( "This always gets printed.\n" );

For general integers, thus ones that exceed the ASCII table we can print it as a string by using itoa() or (s)printf() as said many times:
char buffer[512];sprintf( buffer, "And i is %d.\n", i );printf( buffer );printf( "And i is %d.\n", i );


Still hope it helps. Greetz,

Illco
this workz. You just forgot to add '0' to 9 to convert it to ascii.


int i=9;
char *c;
c = (char*)&i
c = c+1;
*c = 0;
c = c-1;
*c += '0';
cout

But do realize that i variable has changed and is no longer 9. And i must be on range 0-9 ;).

cleaner version:

int i=9;
char *c = (char*)&i
c[0] += '0';
c[1] = '\0';
cout
I believe I was once asked this same question on an entrance interview exam sent via e-mail when i applied (EA maybe?)... and I KNOW d7 is actively looking for a job (God help us) ... could it possibly be that he is trying to get help in moving past the initial questionaire for a job by having us solve his problem?

Anyways, the problem statement was something to the effect:

Write a C function that converts an int to the char* equivalent without using the C library routines.
void convert(char* out, int in){     // fill-in-the-blank}



This topic is closed to new replies.

Advertisement