Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

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
Joined
Apr 9, 2015
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
252
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.


Any idea please? Thank you in advance
 
Last edited:

wannaknow

Junior Member level 2
Joined
Apr 9, 2015
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
252
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?


Thank you in advance.
 
Last edited:

wannaknow

Junior Member level 2
Joined
Apr 9, 2015
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
252
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
_showbin() in post #5 from this thread https://www.edaboard.com/threads/338259/#post1443173

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.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top