[SOLVED] What is the formula to convert a string to its binary form ?
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.
Every character is simply a number. for example A=65, B=66, C=67 and so on (look here http://www.asciitable.com/ ) and afterwards just transform the decimal number to binary.
Every character is simply a number. for example A=65, B=66, C=67 and so on (look here http://www.asciitable.com/ ) and afterwards just transform the decimal number to binary.
How should I find out the binary form of a string say "abcd" ?
Hey,
I guess you mean programmatically?
First of all, you need to know the representation of the string "abcd" in the sense of how is the character 'a' (or whichever) stored on the disk or in memory. Once you know the numeric representation of the character (say 'a' is number 97 in ASCII), you could then convert this decimal number to a binary number. For Unicode encodings other than UTF-8, you need to also check the endianess of your system.
Regarding programatically converting a decimal number to a binary number and print this number out on the screen, please search the web. There you'll find a _lot_ of algorithms :-)
The language is C. I don't know any kind of shell scripts. Besides I did not think that mentioning the language would be necessary as formulas do not depend on languages.
First of all, you need to know the representation of the string "abcd" in the sense of how is the character 'a' (or whichever) stored on the disk or in memory. Once you know the numeric representation of the character (say 'a' is number 97 in ASCII), you could then convert this decimal number to a binary number. For Unicode encodings other than UTF-8, you need to also check the endianess of your system.
Regarding programatically converting a decimal number to a binary number and print this number out on the screen, please search the web. There you'll find a _lot_ of algorithms :-)
atoi() converts a string containing a number to and integer. For example, it converts the string "35" to the integer 35.
To print the actual character codes of each character in a string:
Code:
int i;
for (i=0; str[i]; i++) printf("%d, ", str[i]);
Actually, a computer doesn't even have a concept of letters! It's only the display part that does. When it recieves a number, it looks up the font glyph associated with that number, and displays it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.