How to convert binary floating point numbers to decimal numbers with verilog? - binary

I am writing a program. there is a binary floating number like this format : XX.XXX. for example,binary floating number 01.101 convert to decimal number is 1.625. I tried it for a long time, but couldn't work it out.
I use [4:0]num to store the number. num[4:3] is the integer part, num[2:0] is the floating part. the integer part is easy, when num[2:0]=3'b101, it means that the binary floating part is 0.101, and convert to decimal number is 0.625. so how can I convert the sequence"101", get a sequence "625"?

The quickest way is probably just to use a LUT (lookup table). Since the fractional portion is only 3 bits, that leaves you with only 8 possibilities. And you could use an 12bit value where each nibble is a digit that could be sent to a display etc.
reg result[11:0] // each nibble represents a digit
always #(*) begin
case (num[2:0])
3'b000 : result = 12'h000;
3'b001 : result = 12'h125;
3'b010 : result = 12'h250;
3'b011 : result = 12'h375;
3'b100 : result = 12'h500;
3'b101 : result = 12'h625;
3'b110 : result = 12'h750;
default : result = 12'h875;
endcase
end

Related

Separating character and numeric variables without delimiter in SAS

I have below data with 5 variables with 5 observations: Name Age Gender Weight country
data have;
length string $30.;
input string$;
datalines;
Naresh30Male70India
Venkey29Male50Kenya
Ravi30Male56Pak
Sai67Female40iran
Divya89Female78Dubai
;
run;
I want to Separate these 5 variables in 5 observations
Help me on it
This is fixed code that should work for any range of ages/weights. Not the most elegant solution, but it works. But I would still rather fix that at the input site, and put some delimiters into the original string.
data want;
set have;
mixed2=substr(Mixeddata, anydigit(Mixeddata), anydigit(Mixeddata,-50)-anydigit(Mixeddata)+1);
mixed3=substr(mixed2, notdigit(mixed2));
name=substr(Mixeddata,1, anydigit(Mixeddata)-1);
age=substr(mixed2,1,notdigit(mixed2)-1);
gender=substr(mixed2, notdigit(mixed2), anydigit(mixed2, 5)-notdigit(mixed2));
country=substr(Mixeddata, anydigit(Mixeddata, -50)+1);
weight=substr(mixed3, anydigit(mixed3));
drop Mixeddata mixed2 mixed3;
run;
Consider the makeup of a single string:
Naresh30Male70India
Each desired column is either a number or a character. If we could break this out into two strings, one with only numbers and one with only characters, we can easily pull the needed values:
string_num: 30 70
string char: Naresh Male India
We can do this with regular expressions by replacing letters with spaces and numbers with spaces.
data want;
set have;
string_num = compbl(prxchange('s/[0-9]/ /', -1, string) );
string_char = compbl(prxchange('s/[a-zA-Z]/ /', -1, string) );
name = scan(string_num, 1);
age = scan(string_char, 1);
gender = scan(string_num, 2);
weight = scan(string_char, 2);
country = scan(string_num, 3);
drop string_num string_char;
run;
Note that we use the compbl function to remove any extra spaces to make it easier to read for learning purposes, but this is an optional step.

Converting from base-32 to decimal without VBA

I've been searching and searching and my Google-fu has failed me. I'm trying to convert an encoded number from base-32 to decimal using either expressions or a macro, but I'm not finding anything. I know Excel has the "Decimal" function, I've been hoping that I could stumble onto something similar.
I'm reluctant to use VBA as I don't want to spend time re-learning the language right now and I'm worried that my organization will flag it as potentially dangerous (which could kill my attempts at making any databases).
With an input of "16O9E55"
I expect a result of 1300543653.
I should clarify that this is "base32hex" according to Wikipedia. It's 0-9, A-V. It's only 7 characters of base-32 that needs to convert to 10 digits of decimal. My use case is decoding a barcode into the data I need.
I doubt this can be accomplished without VBA. Consider code adapted from https://www.excelbanter.com/excel-worksheet-functions/150198-formulat-convert-base-32-decimal.html
Public Function Base32ToDec(Num As String) As Variant
Static Digits As String
Dim i As Integer
Dim myIndex As Integer
Dim myStr As String
Digits = "0123456789ABCDEFGHIJKLMNOPQRSTUV"
For i = Len(Num) To 1 Step -1
myStr = Mid(Num, i, 1)
myIndex = InStr(Digits, myStr) - 1
Base32ToDec = Base32ToDec + myIndex * 32 ^ (Len(Num) - i)
Next i
End Function
According to Wikipedia, Base32 uses a 32-character set comprising the twenty-six upper-case letters A–Z, and the digits 2–7. The variant base32hex starts with 0 - 9 and uses the letters A to V.
If only numbers with a limited range have been encoded, you can decode them easily with VBA, otherwise you would have to return an array of bytes and process it further.
You write, that you have up to 10 decimal digits. The question is, what the maximum number is. The Long type can store numbers up to 2,147,483,647. This are ten digits; however, with 10 digits you could store a number as big as 9,999,999,999.
Therefore, the following function returns the number as Double. If you know that your number will never exceed 2,147,483,647, then you can exchange the Double type by Long for the sum variable and the function return type.
Public Function DecodeBase32hex(ByVal encoded As String) As Double
Dim ch As String
Dim sum As Double
Dim d As Long, i As Long
For i = 1 To Len(encoded)
ch = Mid$(encoded, i, 1)
If ch >= "A" And ch <= "Z" Then
d = Asc(ch) - Asc("A") + 10
ElseIf ch >= "0" And ch <= "9" Then
d = Asc(ch) - Asc("0")
Else
Exit For 'E.g. padding charachters
End If
sum = 32 * sum + d
Next i
DecodeBase32hex = sum
End Function
Test in Access' immediate window:
?DecodeBase32hex("16O9E55")
1300543653

Representing 2 binary digits in hex for vhdl

In VHDL, If X'1 => "0001", X'3 => "0011". i.e, 1 hex digit represents 4 binary values, how do i represent only 2 binary values in hex given that i have only a specific bit range in memory. In this case 2. For instance, the space left in memory can only take 2 bits. I know i can still use the initial representation and mask out either the two msb's or lsb's but is there another way ?
You can do this if you are using VHDL-2008:
2X"2" = "0010"
Further examples from web:
unsigned notation (default):
7UX"F" = "0001111" -- extend
7UX"0F" = "0001111" -- reduce
signed noataion:
7SX"F" = "1111111" -- extend
7SX"CF" = "1001111" -- reduce

Denary to binary conversion program

How does this denary to binary program work? I am finding it hard to comprehend what is happening behind the code.
Can someone explain the lines 6 onwards?
Number = int(input("Hello. \n\nPlease enter a number to convert: "))
if Number < 0:
print ("Can't be less than 0")
else:
Remainder = 0
String = ""
while Number > 0:
Remainder = Number % 2
Number = Number // 2
String = str(Remainder) + String
print (String)
The idea is to separate out the last part of the binary number, stick it in a buffer, and then remove it from "Number". The method is general and can be used for other bases as well.
Start by looking at it as a dec -> dec "conversion" to understand the principle.
Let's say you have the number 174 (base10). If you want to parse out each individual piece (read as "digit") of it you can calculate the number modulo the base (10), then do an integer division to "remove" that digit from the number. I.e. 174%10 and 174//10 => (Number) 17|4 (Reminder). Next iteration you have 17 from the division and when you perform the same procedure, it'll split it up into 1|7. On the next iteration you'll get 0|1, and after that "Number" will be 0 (which is the exit condition for the loop (while Number > 0)).
In each iteration of the loop you take the remainder (which will be a single digit for the specific base you use (it's a basic property of how bases work)), convert it to a string and concatenate it with the string you had from previous iterations (note the order in the code!), and you'll get the converted number once you've divided your way down to zero.
As mentioned before, this works for any base; you can use base 16 to convert to hex (though you'll need to do some translations for digits above 9), octal (base 8), etc.
Python code for converting denary into binary
denary= int(input('Denary: '))
binary= [0,0,0,0]
while denary>0:
for n,i in enumerate(binary):
if denary//(2**(3-n))>=1:
binary[n]= 1
denary -= 2**(3-n)
print(denary)
print (binary)

mysql to append a number of stars based on the string length

I have a requirement that when a string length is less than 10, I need to append as many stars(*) at the end till length equals to 10.
ex:
ipString = "Stack"
opString = "Stack*****"
Is there any way to do it?
Use RPAD
SELECT RPAD('STACK',10,'*')
More accurately:
SET #str := 'Stack ';
SELECT
IF(LENGTH(#str) < 10, RPAD(#str,10,'*') ,#str)
WORKING DEMO If the length of the string is less than 10
WORKING DEMO If the length of the string is greater than or equal to 10
Note on RPAD:
Description
MySQL RPAD() function pads strings from right. The actual string which is to be padded, length of the string returned after padding and string which is used for padding - all these are passed as arguments.
Syntax
RPAD(str, len, padstr)
Arguments
Name Description
str The actual string which is to be padded.
len Length of the string returned after padding.
padstr String which will be used for padding.