Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 02-11-2004, 01:28 AM   #1
LQ Newbie
Registered: Feb 2004
Posts: 2

Rep: Reputation: 0
small C hangup.. Sparc platform

Hey everyone,

As a small part of a program that I am currently writing, I am trying to read into a function a single int in machine language (1's and 0's) and shoot this number out as a char array in base 16. I am not aware of any library functions for doing this, and quite honestly, would prefer just coding it without functions. I am on a Sparc platform and coding in C.

Old 02-11-2004, 01:56 AM   #2
LQ Newbie
Registered: Feb 2004
Posts: 2

Original Poster
Rep: Reputation: 0
follow up question as well

Now that my curiousity has been piqued, is their a way (without using atoi) to get the binary value from something along the lines of :

char A[]="ADF123";

which would be something to the tune of 1010 1110 1111 0001 0010 0011

My cse courses never went over any of this in detail, so hopefully someone on here could provide some insight.

Old 02-13-2004, 10:23 PM   #3
LQ Guru
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
Well, *all* data in C (and any other language for that matter) is already in binary; you just have to know how to print it out that way. If you want to read in a string of characters (1 and 0) representing a binary number, you'd probably have to write a function to do it, since I don't know of any standard C functions for it. A while loop could do the job too, but a function would be better so you could re-use it

Something along the lines of:

int result = 0;
while (more characters)
result *= 2;
if (1 was read), result += 1;

As for printing in hexadecimal, check the manpage for printf()

Your second question could work along the same lines as the first:

int result = 0;
while (more characters)
result *= 16; // since it's base-16
result += current_digit_value; // hex digit converted to an int

Converting the character from hexadecimal notation to an integer is the only slightly tricky part; something like this maybe:

if ( digit >= '0' && digit <= '9' ) // if digit is between 0 and 9
value = digit - '0'; // subtract ascii value of '0' from digit; '0' - '0' = 0
else if ( digit >= 'a' && digit <= 'f' )
value = digit - 'a' + 10; // since 'a' - 'a' = 0, add 10 to get value of 'a', etc.

Last edited by wapcaplet; 02-13-2004 at 10:37 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Replaciing java platform by Sun's Java platform machan Linux - Newbie 1 11-11-2005 01:55 AM
Linux For Sun Sparc Platform bono02 Linux - Newbie 1 06-23-2005 06:57 AM
hangup problem ohlinux Red Hat 2 07-31-2004 08:36 AM
How can i send message from Linux platform to Windows platform linuxeagle Linux - Networking 1 02-17-2004 12:35 AM
modem hangup swekalle Slackware 4 12-26-2002 06:59 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:47 PM.

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