Jump to content
  • Advertisement

Archived

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

ASCII confusion

This topic is 5937 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
Advertisement
You might try looking at it from the other direction. Convert "pdstatha" into ascii and then try to figure out how 6847 came to be generated. Then reverse the process.

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
That there wasn''t any correlation between the uid and the username, but from this example it can be clearly seen that you can get the username from the uid

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!