ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Are you saying that you want to take a C datatype such as an "int" and format it as a (printable) ascii string?
Of course, you'll use the C library function sprintf(), for example:
Code:
#include <stdio.h>
#include <stdlib.h>
int i;
char buffer[80]; /* A buffer to hold the ascii string */
i = 44;
sprintf(&buffer[0], "%d", i);
/* buffer[] now contains the string "44" */
sprintf() has quite a few options for formatting various C datatypes as ascii strings, so consult a manual on it.
For going the other way (ie, converting an ascii string back to a C datatype such as long or int), you'll want to look at atoi() atol(), atof(), etc.
Can anyone tell me how can I convert a string into ascii, and hex?
my plateform is linux gcc.
Code:
// Note: for reasons of 'safety' decstring and hexstring
// are longer than string itself
// This code is not very efficient
char string[64], decstring[256], hexstring[256], dummy[16];
int i;
// Fill the string somehow. Up to you.
// intialise to empty
decstring[0] ='\0';
hexstring[0] = '\0';
for (i=0; i<strlen(string); i++)
{
sprintf(dummy, "%02X ", string[i]); // Convert to hexadecimal notation
strcat(hexstring, dummy);
sprintf(dummy, "%03d ", string[i]); // Convert to decimal notation
strcat(decstring, dummy);
}
Look at strtol() and related for converting strings to any base you choose. If you are in kernel space and can't use any of the standard libraries then you'll have to roll your own.
This routine converts an ascii string of the form 0x123abc to an integer, and it tests to make sure the string starts with 0x or 0X. I use this function in kernel space when I need to do this:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.