Cableguy Expert Licensed User Longtime User Mar 27, 2015 #1 Hi guys I need to convert an integer to both a 7bit and 10 bit binary string representation. But... I don't have the foggiest idea how to do it. any help apreciated
Hi guys I need to convert an integer to both a 7bit and 10 bit binary string representation. But... I don't have the foggiest idea how to do it. any help apreciated
JordiCP Expert Licensed User Longtime User Mar 27, 2015 #2 Code: Copy Sub int2Bin(num As Int, digits As Int) As String Dim s As String For k=0 To digits-1 If Bit.AND(num,1)=1 Then s="1"&s Else s="0"&s End If num=Bit.ShiftRight(num,1) Next Return s End Sub Upvote 0
Code: Copy Sub int2Bin(num As Int, digits As Int) As String Dim s As String For k=0 To digits-1 If Bit.AND(num,1)=1 Then s="1"&s Else s="0"&s End If num=Bit.ShiftRight(num,1) Next Return s End Sub
Cableguy Expert Licensed User Longtime User Mar 27, 2015 #3 Just before I read your solution I came up with: Code: Copy Sub int2bin(value As Int, base As Int) As String Private x As String x = Bit.ToBinaryString(value) If x.Length < base Then Do Until x.Length = base x= "0" & x Loop End If Return x End Sub But thanks for taking the time to help me Upvote 0
Just before I read your solution I came up with: Code: Copy Sub int2bin(value As Int, base As Int) As String Private x As String x = Bit.ToBinaryString(value) If x.Length < base Then Do Until x.Length = base x= "0" & x Loop End If Return x End Sub But thanks for taking the time to help me
JordiCP Expert Licensed User Longtime User Mar 27, 2015 #4 Funny enough, I often use Bit.### methods but had not seen this one Upvote 0