7 Replies Latest reply on Nov 4, 2015 5:07 AM by userc_40547

# how to convert high decimal values into binary code and show them with sprintf ??

Hello guys,

I need to convert some hugh values (~120000) into binary code. Unfortunately this only works if my value is 1023 or less.

Does anyone have an idea? Is something wrong with the conversion or just with the sprintf? I tried to change the argument %... of sprintf but no success.

long   num, decimal_num, remaind, base =1, binary =0;

num= 1023;
decimal_num = num;
while (num>0)
{
remaind = num %2;
binary = binary + remaind * base;
num = num/2;
base=base*10;
}
sprintf(TransmitBuffer, "real_comb_binary: %ld \n", binary);
UART_1_PutString(TransmitBuffer);

Alex

• ###### 1. Re: how to convert high decimal values into binary code and show them with sprintf ??

With num = 1023 the result will be a number with 10 digits which is near the capacity of a long (better use int32). you may use uint64

but the problem will remain the same, end will be a million.

Why don't you use %b format in sprintf?

Converting a binary into a decimal is only needed when you want to print it as a string, so why not converting into a character string?

Bob

• ###### 2. Re: how to convert high decimal values into binary code and show them with sprintf ??

A million would be enough for me, but with int32, 1023 is also the limit. And I cant find any argument for int64 which is accepted by Creator.

The format %b is also not working.

I dont want to convert binary into decimal, I want to convert decimal into binary.

What do you mean by character string? I want to calculate some other things with my binary number in the end.

• ###### 3. Re: how to convert high decimal values into binary code and show them with sprintf ??

All (most) numbers represented in a µProcessor are in hexadecimal. Only when communicating with one of us humans a conversion is needed. Human readable are ASCII characters only where the digits "0" to "9" are represented by the values 0x30 to 0x39 or 48dec to 57dec. So a number represented by a string of digits must be converted to the internal hex representation.

Where do the numbers come from you want to convert?

There are of course C-functions or macros for conversion like atii() and scanf(), have a look into a C-manual like this one.

What do you mean with " I cant find any argument for int64 which is accepted by Creator."?

Bob

• ###### 4. Re: how to convert high decimal values into binary code and show them with sprintf ??

Thanks for the link, I will have a look after lunch.

My numbers are real and imaginary part of a measured impedance. But the numbers have to be converted into twos complement. Therefore I need to convert my decimal value into binary, invert every 0 to 1 and every 1 to 0 and then sum a one (this is the procedure to convert a number into twos complement) Works fine with a pencil and a paper, but I am stuck at the conversion into binary.

Since it worked for values up to 1023 I thought it might be a problem with the value-size. but int32 also doesnt work.

By "argument" I mean the "%" in sprintf. For int I use "%d" and for int32 I use "%ld", but I cant find anything for int64.
Maybe its worth a try to increase the size of the value once more.

• ###### 6. Re: how to convert high decimal values into binary code and show them with sprintf ??

Two's complement schau mal bei Google

The required inverting of all the bits can be made using the unary "~" operator. Then just add 1

Bob

• ###### 7. Re: how to convert high decimal values into binary code and show them with sprintf ??

Hey,

I found an unconventional method which is working nicely.

Because I just need to convert 16 bit values, where the 16th bit is set, the following method is appropriate:

value -65536 (1000000000000000 binary) = value in twos complement.

Sounds weird, but it is working ;)