BCD 十六进制字符串转整数

发布于 2024-10-09 10:28:40 字数 1028 浏览 5 评论 0原文

我能够修改 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

窗影残 2024-10-16 10:28:40

好吧,尽管这些数字似乎没有意义,但这就是我想出的结果(给出了您期望的结果(从提供的示例中得出)。仔细看看这个结果如果它达到了你所期望的所有结果:

static Int32 GetYearFromBCD(Int32 time)
{
    List<Int32> YearList = new List<Int32>();
    for (Int32 i = 3; 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 < 4; y++)
        Year += YearList[y] * (Int32)Math.Pow(10,(4 - y)-1);
    return Year;
}

我真的只是在黑暗中尝试,因为你给出的数字并不是我所说的典型值,话虽如此,你必须做出你想要的结果。重新收到工作,所以:耸肩:。

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:

static Int32 GetYearFromBCD(Int32 time)
{
    List<Int32> YearList = new List<Int32>();
    for (Int32 i = 3; 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 < 4; y++)
        Year += YearList[y] * (Int32)Math.Pow(10,(4 - y)-1);
    return Year;
}

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:.

罗罗贝儿 2024-10-16 10:28:40

如果你想将数字转换为十六进制表示,只需使用 ToString 并以“x”作为参数:

2010.ToString("x")  output: "7da"

但是,如果你说 2010 年,你期望 200A,我真的不明白你想要实现什么。

If you want to convert number to hex representation just use ToString with "x" as param:

2010.ToString("x")  output: "7da"

However, I don't really understand what to you want to achieve if you say that for 2010, you expect 200A.

罪歌 2024-10-16 10:28:40

你真的确定你的例子是正确的吗?为什么你有一个十六进制数,其中第一部分以 10 为基数,第二部分以 16 为基数?

正常将是以下之一:

  • 2010 => 0x07DA
    • 完整的十六进制
  • 2010 => 0x140A
    • 数字被中途分解,每部分都是十六进制
  • 2010 => 0x2010
    • bcd 编码为维基百科(其他地方)上找到的编码;-)

经过一番谷歌搜索和思考后,我的第一个想法是:

  • 将所有内容放入 BinaryReader
  • 通过调用 BinaryReader.ReadByte( ) 并将其转换为 int
  • 首先将每个值乘以 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:

  • 2010 => 0x07DA
    • complete hex
  • 2010 => 0x140A
    • the number is broken half-way and each part is hex
  • 2010 => 0x2010
    • bcd encoded as found on wikipedia (where else) ;-)

After some googling and thinking here is my first idea:

  • Put everything into an BinaryReader.
  • Get all values individually out of it by calling BinaryReader.ReadByte() and cast it to an int
  • Multiply each value first by 10, then divide by 16
  • Put all of this individual values into a List<int>
  • use Math.Pow(value, 10 * (List.Count - indexOfValue)) to aggregate all these values
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文