# Convert binary (integer and a fraction) from vhdl to decimal in C code, negative value

Status
Not open for further replies.

#### wannaknow

##### Junior Member level 2
Convert binary ( integer and fraction) from vhdl to decimal in C code,negative value

HI,

I have a 14-bit data that is fed from FPGA in vhdl, The NIos II processor reads the 14-bit data from FPGA and do some processing tasks, where Nios II system is programmed in C code

The 14-bit data can be positive, zero or negative. In Altera compiler, I can only define the data to be 8,16 or 32. So I define this to be 16 bit data.

First, I need to check if the data is negative, if it is negative, I need to pad the first two MSB to be bit '1' so the system detects it as negative value instead of positive value.

Second, I need to compute the real value of this binary representation into a decimal value of BOTH integer and fraction.

I learned from this link (https://stackoverflow.com/questions...ary-floating-point-1101-11-into-decimal-13-75) that I could convert a binary (consists of both integer and fraction) to decimal values.

I am wondering if this code can be used to check for negative value? I did try with a binary string of 11111101.11 and it gives the output of 253.75...

I have two questions:

What are the modifications I need to do in order to read a negative value?
I know that I can do the bit shift (as below) to check if the msb is 1, if it is 1, I know it is negative value...

Code:
if (14bit_data & 0x2000) //if true, it is negative value

The issue is, since it involves fraction part (but not only integer), it confused me a bit if the method still works...

If the binary number is originally not in string format, is there any way I could convert it to string? The binary number is originally fed from a fpga block say, 14 bits, with msb as the sign bit, the following 6 bits are the magnitude for integer and the last 6 bits are the magnitude for fractional part.

Last edited:

#### wannaknow

##### Junior Member level 2
How to read a 16-bit data into string in C code

I have a 16-bit data that is fed from FPGA in vhdl, the NIos II processor reads the 16-bit data from FPGA and do some processing tasks, where Nios II system is programmed in C code

How could I read this 16-bit data in string?

Code:
printf("%d", 16_bit_data);
// it shows a decimal value of 15282

What should I do in order to get the result displayed as a string 0011101110110010?

Last edited:

#### xenos

##### Full Member level 4
Re: How to read a 16-bit data into string in C code

Translate to hex (sprintf("%04x",v)) and then translate each hex digit to binary, or use something like the function

#### wannaknow

##### Junior Member level 2
Re: How to read a 16-bit data into string in C code

Translate to hex (sprintf("%04x",v)) and then translate each hex digit to binary, or use something like the function

Thanks for your reply, forgive me for didnt make it clear enough.

I need to compute the real value of this binary representation into a decimal value of BOTH integer and fraction, for example, the value is 15282 (0011 1011 1011 0010), but it is not the real value, how to make sure I will get the value as -30.750015?

The 14-bit-data is defined as follow: with msb as the sign bit, the following 6 bits are the magnitude for integer and the last 6 bits are the magnitude for fractional part.

#### FvM

##### Super Moderator
Staff member

Status
Not open for further replies.

Replies
9
Views
6K
Replies
1
Views
9K
Replies
11
Views
12K
Replies
15
Views
2K
Replies
7
Views
2K