LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-09-2010, 05:59 AM   #1
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
What is the formula to convert a string to its binary form ?


Like the binary form of the integer '2' is '10',

How should I find out the binary form of a string say "abcd" ?

Kindly guide !
 
Old 06-09-2010, 06:36 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
In which language? If you need to do a conversion in shell scripts, you may try bc by changing the base of the result, for example:
Code:
$ echo "obase=2; 42" | bc
101010
Edit: sorry, you want to convert a string of alphabetic characters to binary, don't you? The advice from dracuss (below) could be a starting point.

Last edited by colucix; 06-09-2010 at 06:40 AM.
 
Old 06-09-2010, 06:37 AM   #3
dracuss
Member
 
Registered: May 2006
Location: Chisinau, Moldova
Distribution: Gentoo, Debian sid
Posts: 151

Rep: Reputation: 29
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.
 
1 members found this post helpful.
Old 06-09-2010, 06:42 AM   #4
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by dracuss View Post
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.
Thanks for replying !

I had guessed so, but was not sure..

Thanks again
 
Old 06-09-2010, 06:43 AM   #5
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Quote:
Originally Posted by anishakaul View Post
Like the binary form of the integer '2' is '10',

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 :-)

Hope that helps,
Andi
 
1 members found this post helpful.
Old 06-09-2010, 06:46 AM   #6
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
colucix

Thanks for bothering !

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.
 
Old 06-09-2010, 06:51 AM   #7
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by ForzaItalia2006 View Post
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 :-)

Hope that helps,
Andi
Thanks Andi,

Your explanation was helpful
 
Old 06-09-2010, 02:45 PM   #8
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
would using od or hexedit be cheating ?
 
Old 06-09-2010, 11:52 PM   #9
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by schneidz View Post
would using od or hexedit be cheating ?
Actually i need to do the conversions inside my C program therefore i can't use any of the tools mentioned.

Thanks for bothering.
 
Old 06-13-2010, 05:15 PM   #10
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
^ can you look at the source code of those and use some of the code ?

also there is an atoi function that mite help.

Last edited by schneidz; 06-13-2010 at 05:19 PM.
 
Old 06-13-2010, 11:53 PM   #11
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by schneidz View Post
^ can you look at the source code of those and use some of the code ?

also there is an atoi function that mite help.
I can look at the source code but I'll have to first locate the source code. Any Idea where its source code is ?

Thanks !
 
Old 06-14-2010, 08:39 AM   #12
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to convert string to integer back to string C++ LazerPhreak Programming 1 03-13-2010 07:32 PM
Project to convert DirectX to linux form moslofu General 10 11-12-2007 11:49 AM
How I can Convert 4 byte IP to an IP string of form ("0.0.0.0") touqeer.ansar Programming 2 07-16-2006 11:02 PM
I want Linux source code for FAT file system in user readable form not in binary form ramya272 Linux - Newbie 5 02-05-2004 07:54 PM
How would one convert a file into binary? MasterC General 8 01-21-2003 10:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration