Convert a binary value to unsigned decimal value

G

Golan

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
 
M

Malcolm

Golan said:
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--;
}

}
 
M

Mark McIntyre

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 !
 
S

Sidney Cadot

Mark said:
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
 
N

Nejat AYDIN

Malcolm said:
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.
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.
 
M

Mark McIntyre

Mark said:
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.
 
M

Malcolm

Nejat AYDIN said:
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.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.
The function has nothing special to ASCII, so the name of the function
is misleading.
Like atoi().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top