473,385 Members | 1,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Convert a binary value to unsigned decimal value

Hi,
I need to convert a Binary value to Decimal. I've been told that the
value is an unsigned one. How can I do this?
I use memcpy into an unsigned char variable, but when I print the
value I got a negative value.
For example if I'm using the xd -c (Unix) on the file, I can see the
value FFFFFFFFFFFFFFA2 which using the memcpy as described above I get
-94.
But the real value that I'd expect to get is a positive one.

Thanks
Nov 13 '05 #1
7 13042

"Golan" <bc****@mot.com> wrote in message
I need to convert a Binary value to Decimal. I've been told that the
value is an unsigned one. How can I do this?
Presumably you mean convert a machine representation value into something
suitable for human reading.
The normal way to do this is
sprintf("%d", (int) x);
if the value is unsigned
sprintf("%u", (unsigned int) x);
However generally unsigned integer values are a BAD THING, use normal int
unless you really do need that extra bit.
I use memcpy into an unsigned char variable, but when I print the
value I got a negative value.
For example if I'm using the xd -c (Unix) on the file, I can see the
value FFFFFFFFFFFFFFA2 which using the memcpy as described
above I get -94.
But the real value that I'd expect to get is a positive one.

OK. It's almost certain that char on your machine is 8 bits. Something is
sign-extending the value to 64 bits. Since your value (0xA2) is greater than
127, a signed char with the same bit pattern would be negative.

memcpy() ing a value into an unsigned char doesn't sound like the way to go.
What is the data originally?

Another problem, I've told you to use sprintf(), but how does sprintf() work
internally? It's something like this.

void bintoascii(char *out, int x)
{
char *save;
char temp;

/* add the minus */
if(x < 0)
{
*out++ = '-';
x = -x;
}
/* handle 0 specially */
if(x == 0)
{
*out++ = '0';
*out = 0;
return;
}

save = out;
while(x)
{
*out++ = (x % 10) + '0';
x /= 10;
}
*out = 0;

out--;

/* reverse the number */
while(out > save)
{
temp = *out;
*out = *save;
*save = temp;
save++;
out--;
}

}
Nov 13 '05 #2
On 7 Dec 2003 01:17:36 -0800, in comp.lang.c , bc****@mot.com (Golan)
wrote:
Hi,
I need to convert a Binary value to Decimal.
in computers,. all numbers are binary. If your "binary number" is
stored in a numerical data type then you don't need to do anything..
I've been told that the
value is an unsigned one. How can I do this?
is your "binary" data in a string? Or in a numeric type? Sounds like
the latter. So its already a number, you only need to printf it using
the right format specifier.
I use memcpy into an unsigned char variable, but when I print the
value I got a negative value.
if you printf an unsigned char array, you should get a string, not a
number !! What /are/ you doing?

For example if I'm using the xd -c (Unix) on the file, I can see the
value FFFFFFFFFFFFFFA2 which using the memcpy as described above I get
-94.


Show us your code for goodness sake !
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #3
Mark McIntyre wrote:
in computers,. all numbers are binary. [...]


<nit attribute="totally_useless_fact">

Not necessarily. The THROBAC design (as described in an article by C.E.
Shannon) uses Roman numerals internally. For same reason, nobody
attempted to target a C compiler at the platform :-)

Which reminds me.... Did I already mention my desire for a Roman
numerals printf/scanf format specifier?

</nit>

Best regards,

Sidney
Nov 13 '05 #4
Malcolm wrote:

"Golan" <bc****@mot.com> wrote in message
I need to convert a Binary value to Decimal. I've been told that the
value is an unsigned one. How can I do this?
Presumably you mean convert a machine representation value into something
suitable for human reading.
The normal way to do this is
sprintf("%d", (int) x);


Where will the sprintf write to ? It must have been
sprintf(buf, "%d", (int)x);
where buf is an array of characters having enough space to hold decimal
string represantation of an int, plus the '\0'.
if the value is unsigned
sprintf("%u", (unsigned int) x);
Ditto
However generally unsigned integer values are a BAD THING,
Why ?
use normal int unless you really do need that extra bit.

I use memcpy into an unsigned char variable, but when I print the
value I got a negative value.
For example if I'm using the xd -c (Unix) on the file, I can see the
value FFFFFFFFFFFFFFA2 which using the memcpy as described
above I get -94.
But the real value that I'd expect to get is a positive one.

OK. It's almost certain that char on your machine is 8 bits. Something is
sign-extending the value to 64 bits. Since your value (0xA2) is greater than
127, a signed char with the same bit pattern would be negative.

memcpy() ing a value into an unsigned char doesn't sound like the way to go.
What is the data originally?

Another problem, I've told you to use sprintf(), but how does sprintf() work
internally? It's something like this.

void bintoascii(char *out, int x)


The function has nothing special to ASCII, so the name of the function
is misleading.
{
char *save;
char temp;

/* add the minus */
if(x < 0)
{
*out++ = '-';
x = -x;
In a two's complement implementation, that does not work if x is equal
to INT_MIN. So this case must be handled specially.
}
/* handle 0 specially */
if(x == 0)
{
*out++ = '0';
*out = 0;
return;
}

save = out;
while(x)
{
*out++ = (x % 10) + '0';
x /= 10;
}
*out = 0;

out--;

/* reverse the number */
while(out > save)
{
temp = *out;
*out = *save;
*save = temp;
save++;
out--;
}

}

Nov 13 '05 #5
On Sun, 07 Dec 2003 12:59:14 +0100, in comp.lang.c , Sidney Cadot
<si****@jigsaw.nl> wrote:
Mark McIntyre wrote:
in computers,. all numbers are binary. [...]
Not necessarily. The THROBAC design (as described in an article by C.E.
Shannon) uses Roman numerals internally. For same reason, nobody
attempted to target a C compiler at the platform :-)


I'll settle for "in computers of practical interest to C
programmers"... :-)
Which reminds me.... Did I already mention my desire for a Roman
numerals printf/scanf format specifier?
I have discovered one, but there's not enough room in this margin to
write it all down.
</nit>


--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #6
Thank you all for your help.
I think now I understand.
Nov 13 '05 #7

"Nejat AYDIN" <ne********@superonline.com> wrote in message
However generally unsigned integer values are a BAD THING,
Why ?

Imagine we are writing a program that contains a number of employees. It is
a typical novice move to think "The number of employees in a company cannot
be negative, so I'll use an unsigned int".

So we have
unsigned int N; /* number of employees */

Now it is very likely that we will want to iterate over the employee array.
Since N is unsigned, i must also be unsigned

unsigned int i;

for(i=0;i<N;i++)
employee[i].salary += 100;

Now the fun comes when we need to do the iteration in reverse

for(i=N-1;i>=0;i--)

whoops won't work.

This is just one of the niggly little problems you get when you allow
unsigned values. Of course it isn't a total disaster - an experienced
programmer would write the loop so that it works.

You also get problems when you mix singed and unsigned arithmetic, and it is
very difficult to avoid doing this if you use unsigned values regularly. Of
course you can use casts to suppress the compiler warnings, at risk of
casting away those warnings that point to real weaknesses in the code.
void bintoascii(char *out, int x)


The function has nothing special to ASCII, so the name of the function
is misleading.

Like atoi().
Nov 13 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Bern | last post by:
how to specifiy a binary number in c++? hex numbers are specified by 0x prefix
9
by: bowsayge | last post by:
Inspired by fb, Bowsayge decided to write a decimal integer to binary string converter. Perhaps some of the experienced C programmers here can critique it. It allocates probably way too much...
7
by: whatluo | last post by:
Hi, all I'm now working on a program which will convert dec number to hex and oct and bin respectively, I've checked the clc but with no luck, so can anybody give me a hit how to make this done...
4
by: QQ | last post by:
Hello unsigned char a; the a represents a 4-bit binary, a represents another 3-bit binary. I'd like to convert a into a decimal. For instance if {a a a a the decimal should be 5
2
by: Jack | last post by:
Hi I am having a little trouble trying to read a binary file, I would like to write an ascii to Metastock converter in python but am not having a lot of success. The file formats are ...
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
28
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
9
by: Leo jay | last post by:
i'd like to implement a class template to convert binary numbers to decimal at compile time. and my test cases are: BOOST_STATIC_ASSERT((bin<1111,1111,1111,1111>::value == 65535));...
10
by: cmdolcet69 | last post by:
Public ArrList As New ArrayList Public bitvalue As Byte() Public Sub addvalues() Dim index As Integer ArrList.Add(100) ArrList.Add(200) ArrList.Add(300) ArrList.Add(400) ArrList.Add(500)
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.