64,654
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#define MAX_VOLT 2.56
void ValueToBinary(unsigned __int8 v,char *b) {
b[0]=v;
}
void ConvertInitE(double initE, double finalE, char* buf) {
//initE=-1.3
//MAX_VOLT=2.56
//finalE=-1.9
double volt = 0;
unsigned __int16 ivolt;
volt =initE * -1.0;
ivolt = (unsigned __int16)((volt + MAX_VOLT) * 0xFFF / (MAX_VOLT * 2));
unsigned __int8 m_iLow8 = ivolt & 0xFF;
unsigned __int8 m_iHigh4 = (ivolt & 0xf00) >> 8;
//转十六进制
ValueToBinary(m_iHigh4, (char*)buf);
ValueToBinary(m_iLow8, (char*)buf+1);
}
int main() {
char s[2];
ConvertInitE(-1.3,-1.9,s);
printf("%02x %02x\n",(unsigned char)s[0],(unsigned char)s[1]);//0c 0f
return 0;
}
using System;
using System.Collections.Generic;
using System.Text;
namespace t
{
class Program
{
public const double MAX_VOLT=2.56;
static void ConvertInitE(double initE, double finalE, byte[] buf)
{
//initE=-1.3
//MAX_VOLT=2.56
//finalE=-1.9
double volt = 0;
ushort ivolt;
volt =initE * -1.0;
ivolt = (ushort)((volt + MAX_VOLT) * 0xFFFu / (MAX_VOLT * 2));
byte m_iLow8 = (byte)(ivolt & 0xFFu);
byte m_iHigh4 = (byte)((ivolt & 0xf00u) >> 8);
//转十六进制
buf[0]=m_iHigh4;
buf[1]=m_iLow8;
}
static void Main(string[] args)
{
byte[] s={0,0};
ConvertInitE(-1.3,-1.9,s);
Console.WriteLine("{0,2:x2} {1,2:x2}",s[0],s[1]);//0c 0f
}
}
}