Solved

unsigned int to string binary

Posted on 2011-03-08
11
591 Views
Last Modified: 2012-05-11
Sorry for my bad english.

static unsigned int number = 256;
static char FW[20];

How to get a binary string ? like FW = "0000 000100000000"

Thanks
0
Comment
Question by:Ludde72
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
11 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 500 total points
ID: 35068209
Hi Ludde72,

you can use 'itao' to do this - it's done for 'int', but you can cast the 'unsigned int' to 'int' so it shouldn't make a difference. The char array you need should be of length 33 to make sure all 32 bits plus the terminating zero char fit into it.

Example:

static unsigned int number = 256;
static char FW[33];
itoa( number, FW, 2 );

Open in new window


Hope that helps,

ZOPPO
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 35068215
BTW, I forgot to ask: it the space in '0000 000100000000' from your posted sample wanted or a typo?
0
 

Author Comment

by:Ludde72
ID: 35068252
Sorry it was a typo.

Thanks
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

Expert Comment

by:Zoppo
ID: 35068260
No problem ...
0
 

Author Comment

by:Ludde72
ID: 35068363
Can't find itoa, is it ansi c ?, I'm using Labwindows (Watcom).

Thanks

// Ludde

0
 
LVL 31

Expert Comment

by:Zoppo
ID: 35068485
No, sorry, unfortunateley itoa is not ANSI C but comes with most C/C++ compilers.

But samples can be found in the internet how it can be implemented - I found some here: http://www.jb.man.ac.uk/~slowe/cpp/itoa.html

I tested one and found it seems to work fine:

void strreverse( char* begin, char* end )
{
	char aux;

	while( end > begin )
	{
		aux=*end, *end--=*begin, *begin++=aux;
	}
}

void my_itoa( int value, char* str, int base )
{
	static char num[] = "0123456789abcdefghijklmnopqrstuvwxyz";
	char* wstr = str;
	int sign;

	// Validate base
	if ( base < 2 || base > 35 )
	{
		*wstr='\0';
		return;
	}

	// Take care of sign
	if ( ( sign = value ) < 0 )
	{
		value = -value;
	}

	// Conversion. Number is reversed.
	do
	{
		*wstr++ = num[ value % base ];
	} while( value /= base );

	if( sign < 0 )
	{
		*wstr++ = '-';
	}

	*wstr='\0';

	// Reverse string
	strreverse( str, wstr-1 );
}

Open in new window



Hope that helps,

ZOPPO
0
 

Author Comment

by:Ludde72
ID: 35068626
The code works fine, but i need to fill out wiht zero's.

FW = 0000000100000000

not FW = 100000000

Thanks

// Ludde

0
 
LVL 31

Expert Comment

by:Zoppo
ID: 35081829
Hi again,

sorry for late response, but I just arrived my PC at work ...

Here you can see a simplified version of the above posted function which converts and unsigned int to a binary string:

char* my_itoa2( unsigned int value )
{
	int len = 32;
	static char str[33];
	static char num[] = "01";
	char* wstr = str;

	// Conversion. Number is reversed.
	while( --len > 0 )
	{
		*wstr++ = num[ value % 2 ];
		value /= 2;
	}

	*wstr='\0';

	// Reverse string
	strreverse( str, wstr-1 );

	return str;
}

Open in new window


Warning: This code isn't thread safe since it uses static char array - if you need it thread safe it need's to be modified in a way it gets passed a char array ...

ZOPPO
0
 
LVL 40

Expert Comment

by:evilrix
ID: 37419044
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question