BCD 十六进制字符串转整数
我能够修改 Brad Christie 提供的解决方案,以获得我期望的小时、分钟、秒、月、日和年的结果,所有这些结果都采用 BCD 格式:
static Int32 GetYearFromBCD(Int32 time)
{
int length = time.ToString().Length -1;
List<Int32> YearList = new List<Int32>();
for (Int32 i = length; i >= 0; i --)
{
Int32 place = i * 4;
Int32 val = 0x01 << place;
Int32 curVal = (Int32)(time / val);
if (curVal > 9 && YearList.Count > 0)
{
Int32 delta = (Int32)(curVal / 10);
YearList[YearList.Count - 1] += delta;
curVal -= delta * 10;
}
YearList.Add(curVal);
time -= curVal << place;
}
Int32 Year = 0;
for (Int32 y = 0; y < YearList.Count; y++)
Year += YearList[y] * (Int32)Math.Pow(10,(length+1 - y)-1);
return Year;
}
我想提供此问题的更新。在设备在新年期间运行几天后,我能够完全确认 Brad 发布的代码解决方案完全符合我们的需要。
我能够证实我的怀疑,即预期值确实是二进制编码的十进制,我能够确认预期值仅适用于十六进制值。一位同事能够使用表格作为标准独立确认时间和日期,所以我觉得把它放在床上很舒服。
我能够确认,无论出于何种原因,数据的十进制值不起作用,我只能得出结论,设备正在将数据作为十六进制值发送,我唯一关心的是其他应用程序是否会以类似的方法工作。
我感谢大家帮助我解决这个问题,一些评论引导我走上了一条让我弄清楚的道路。
I was able to modify the solution provided by Brad Christie to get the results I am expecting for the Hours, Minutes, Seconds, Months, Days, and Years all of which are in BCD Format:
static Int32 GetYearFromBCD(Int32 time)
{
int length = time.ToString().Length -1;
List<Int32> YearList = new List<Int32>();
for (Int32 i = length; i >= 0; i --)
{
Int32 place = i * 4;
Int32 val = 0x01 << place;
Int32 curVal = (Int32)(time / val);
if (curVal > 9 && YearList.Count > 0)
{
Int32 delta = (Int32)(curVal / 10);
YearList[YearList.Count - 1] += delta;
curVal -= delta * 10;
}
YearList.Add(curVal);
time -= curVal << place;
}
Int32 Year = 0;
for (Int32 y = 0; y < YearList.Count; y++)
Year += YearList[y] * (Int32)Math.Pow(10,(length+1 - y)-1);
return Year;
}
I wanted to provide an update to this question. After the device was running for several days through New Years, I was able to full confirm that the code solution Brad posted does exactly what we will need.
I was able to confirm my suspicions that the expected value was indeed a Binary Coded Decmial, I was able to confirm that the value expected only works has a HEX value. A co-worker was able to independently confirm the time and date, using table for the standard, so I feel comfortable putting this to bed.
I was able to confirm that for whatever reason the decmial value of the data does not work, I can only conclude the data is being sent as a hex value by the device, my only concern is will other applications work in a similar method.
I appreciate everyone's help in figuring this out, some of the comments lead me down a path that allow me to figure out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,尽管这些数字似乎没有意义,但这就是我想出的结果(给出了您期望的结果(从提供的示例中得出)。仔细看看这个结果如果它达到了你所期望的所有结果:
我真的只是在黑暗中尝试,因为你给出的数字并不是我所说的典型值,话虽如此,你必须做出你想要的结果。重新收到工作,所以:耸肩:。
Well, even though the numbers don't seem to make sense, this is what I came up with (giving the results you're expecting (from the examples provided). Take this through the wringer and see if it comes out with all the results you are expecting:
I'm really just taking a shot in the dark at this as the numbers you gave aren't what I would call typical. Having said that, you have to make what you're receiving work, so :shrug:.
如果你想将数字转换为十六进制表示,只需使用 ToString 并以“x”作为参数:
但是,如果你说 2010 年,你期望 200A,我真的不明白你想要实现什么。
If you want to convert number to hex representation just use ToString with "x" as param:
However, I don't really understand what to you want to achieve if you say that for 2010, you expect 200A.
你真的确定你的例子是正确的吗?为什么你有一个十六进制数,其中第一部分以 10 为基数,第二部分以 16 为基数?
正常将是以下之一:
经过一番谷歌搜索和思考后,我的第一个想法是:
List
Math.Pow(value , 10 * (List.Count - indexOfValue))
聚合所有这些值Are you really sure that your example is correct? Why do you have a hex-number, where the first part is with base 10 and the second part is base 16?
Normal would one of these:
After some googling and thinking here is my first idea:
List<int>
Math.Pow(value, 10 * (List.Count - indexOfValue))
to aggregate all these values