[chirp_devel] def convert_freq_to_bytes(real_freq):

Les Niles
Mon Jan 21 22:16:46 PST 2013


That looks to me like it will work, if you convert the real number to an int before converting it to a string (to prevent the for loop from applying the int operator to the decimal point).  You can even combine everything into one statement, so the function definition would be something like:

def freq2bytes(f):
	return [int(i) for i in str(int(f*100000))]


  -Les W6VN

On 21 Jan 2013, at 19:08, Jim Unroe <rock.unroe at gmail.com> wrote:
> Hi group,
> 
> I'm trying to create a helper function to convert a floating point number to a group of bytes
> 
> For example I have this value: 146.52000
> I think I need it change to something like 0x01,0x04,0x06,0x05,0x02,0x00,0x00,0x00
> 
> The first thing I beieve I need to do is shift the decimal point to the right 5 places. So I did this
> 
>     real_freq *= 100000.0
> 
> I believe this gets me 14652000
> 
> Now I think I need to do something like this to separate the individual digits
> 
>     digits =[int(i) for i in str(real_freq)]
> 
> I'm not positive, but I think this makes an array something like
> 
> digits[0] = 1
> digits[1] = 4
> digits[2] = 6
> digits[3] = 5
> digits[4] = 2
> digits[5] = 0
> digits[6] = 0
> digits[7] = 0
> 
> Here is where I'm stuck. I've tried a few think but so far no luck. I've searched the other drivers to see if I could locate something similar. If there is, I missed it.
> 
> Then ultimately, I think I need to end with
> 
>     return bytes
> 
> So am I on the right track? Any advice on what I need to do next.
> 
> Thanks,
> Jim




More information about the chirp_devel mailing list