Archived

This topic is now archived and is closed to further replies.

ASCII confusion

This topic is 5776 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The following snippet of code takes the integer 97 and converts it to it''s character equivalent (which should be ''a'' ).
  
#include <iostream>
using namespace std;

int main()
{
	int name = 97;
        cout << (char) name << "\n";
        return 0;
}
  
Ok now thats correct and it works fine however I want to convert 6847 into it''s character equivalent. Now what is 6847 you ask? Well 6847 is supposedly my user id within the unix system, now computers don''t recognise characters and this is why the characters "pdstatha" (character equivalent of my user id) are stored as this number. So my question is how do I get it to convert from 6847 to pdstatha???

Share this post


Link to post
Share on other sites
Hmph. There is no "character equivalent" to 6487.
Unix does *not* get it''s userid by translating your username into a number or anything like that. Typically you''ll find that root is uid 0, various daemon-users inhabit uid up to 499, and regular users are at 500 and up.

If you really want to start out with a userid and find the username of that user the best way is probably to grep the password-file.

-Neophyte

- Death awaits you all with nasty, big, pointy teeth. -

Share this post


Link to post
Share on other sites
Ok i think I'm starting to get somewhere, heres some more code

    
#include <iostream>

using namespace std;

int main()
{
char name[8];
name[0] = 'p';
name[1] = 'd';
name[2] = 's';
name[3] = 't';
name[4] = 'a';
name[5] = 't';
name[6] = 'h';
name[7] = 'a';
for(int i=0; i<sizeof(name); i++)
cout << (int) name[i] << "\n";
return 0;
}


This gives the following output

112
100
115
116
97
116
104
97

[EDIT]
It doesn't??? Then why the hell did my tutor say that it did?
[/EDIT]

Edited by - pdstatha on February 18, 2002 9:25:13 AM

Share this post


Link to post
Share on other sites
After further studying, I discovered Neophyte was indeed correct, I have however found an alternate solution to grep. The following program will print out the username of a file owner.

    
#include <grp.h>

#include <pwd.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <unistd.h>

#include <stdlib.h>

void main(int argc, char **argv) {

uid_t me1,me2;
struct passwd *my_passwd;
struct group *my_group;
struct stat sbuf;

me1 = sbuf.stuid;
me2 = seteuid(me1); //this may need altering


my_passwd = getwuid(me2);
if(!my_passwd) {
printf("Couldn't find out about user %d\n", (int) me2);
exit(EXIT_FAILURE);
}

if(argv[1] != '\0')
if(stat(argv[1],&sbuf) != -1)
printf("file %s is owned by %s\n",argv[1],
my_passwd->pw_name);
else
printf("Can't stat %s\n",argv[1]);
else
printf("Usage:%s file_name\n", argv[0]);

}


Edited by - pdstatha on February 18, 2002 10:56:05 AM

Share this post


Link to post
Share on other sites
Let me repeat myself a little more clearly:
On Un*ix systems there is absolutely *no* correlation between your username and your uid. None whatsoever. Period.
If your tutor said something that you interpreted that way there is a ~99.99% chance that you misunderstood what s/he said.

That said, there is an extremely slight theoretical possibility that your school/university/whatever has implemented some kind of hashing algorithm to give you your uid, but why anyone with a sane mind would want to do that is beyond me (it would be about as useful as hashing your birthdate to give you your usename (a lot less, actually, come to think of it)).

-Neophyte

- Death awaits you all with nasty, big, pointy teeth. -

Share this post


Link to post
Share on other sites
There is no correlation between the UID and the username. The snippet of code that you say proves otherwise does not. What that code does is query the system database; that's it. Each UID is associated with each username by the system, but the UIDs are not generated from the usernames; there is no mathematical correlation.

To understand the idea, look at this code. Then try to write a second program to associate an arbitrary name with a uid. You won't be able to. The reason this program can do the association is that it has access to several arrays, which are analagous to the system database previously mentioned.
      
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;

int main()
{
char name[10][128];
unsigned long uid[10];
char getname[128];
int i;

for(i = 0; i < 10; i++)
{
cout << "Input username:\t";
cin << name[i];

uid[i] = rand();
}

cout << "Done generating UIDs for 10 names.\n";

cout << "Input a name to retrieve a UID: ";
cin >> getname;

for(i = 0; i < 10; i++)
{
if(strcmp(getname, name[i]) == 0)
{
cout << "UID:\t" << uid[i];
break;
}
}

return 0;
}




Edited by - TerranFury on February 18, 2002 11:56:11 AM

Share this post


Link to post
Share on other sites
Yeah sorry getting my terms mixed up, I didn't mean that you could directly derive username from uid. I meant given the uid you can get the username.

[edit]
Updated code that works

  
if(argv[1] !='\0'){
if(stat(argv[1],&sbuf) != -1){
me1 = sbuf.st_uid;
me2 = me1;
my_passwd = getpwuid(me2);
if(!my_passwd){
printf("Couldn't find out about user %d\n", (int)me2);
}
printf("file %s is owned by %s\n", argv[1],
my_passwd->pw_name);
}else
printf("Can't stat %s\n", argv[1]);
}else
printf("Usage:%s file_name\n",argv[0]);


Edited by - pdstatha on February 18, 2002 3:46:05 PM

Share this post


Link to post
Share on other sites