-2

I want to show a 64-bit number as string. The problem is that my hardware doesn't support 64-bit number, just 32-bit.

So, I have the 64-bit number splitted into two 32-bit number (High and low part).

Example: 64-bit number  :  12345678987654321 (002B DC54 6291 F4B1h)
         32-bit low part:  1653732529                  (6291 F4B1h)
         32-bit high part: 2874452                     (002B DC54h)

I think the solution to my problem would be showing this number as string. It is possible?

Thanks.

  • 4
    [tag:python] _or_ [tag:c]? which language are you in? what has your problem to do with [tag:microcontroller] ? there are plenty mc's - which one? please read the tag-descriptions and remove inappropriate ones - thanks – Patrick Artner Dec 11 '18 at 10:54
  • Language doesn't matter to me, I used both as reference. I'm using a embedded system. That's why I putted "microcontroller". I think if there is a solution to my problem, language or system wouldn't matter. Thanks. – kelvinandrade Dec 11 '18 at 11:03
  • Yes you can. If you just want to store a number and show it you can store that number directly as string instead of two 32 bit numbers. Something like `string my_number = "123456789123456789"; cout << my_number;` –  Dec 11 '18 at 11:04
  • Have you tried to do the 64-bit division? Some C compilers can emulate 64-bit division in software. – user694733 Dec 11 '18 at 11:05
  • OP is only talking about storing and showing numbers, not about doing any mathematical operations on them. –  Dec 11 '18 at 11:06
  • Thanks for answering @SembeiNorimaki. Okay, but in this case, the two 32 bit numbers represent one 64 bit number. I want to show the 64 bit number as string, but I can't handle 64 bit numbers, so I can't convert it directly. – kelvinandrade Dec 11 '18 at 11:09
  • @user694733 Sorry, I can't see how this would solve my problem. I want to show a 64 bit number. But my hardware can't handle 64 bit numbers, so I have splited it into two 32 bit registers (high and low part). – kelvinandrade Dec 11 '18 at 11:15
  • Oops, I misread. Anyway, my point was this: If working with modern C compiler for example, they can often emulate 64-bit types with software, and no actual hardware support is needed. (Though it's usually little slower than actual 64-bit hardware support.) If compiler can offer software 64-bit division, then implementing conversion from 64-bit integer to string is trivial. – user694733 Dec 11 '18 at 11:19
  • @user694733 Oh, I got it. That would be nice, but unfortunately It doesn't support 64-bit software emulation :/ – kelvinandrade Dec 11 '18 at 11:27

2 Answers2

1

yes you can use an array of 32 bit uints or even lower bit-width ...

for printing you can use this:

so first print a hex string which is easy on any bit-width (as you just stack up the lower bit-widths prints together from MSW to LSW) and then convert the hex text to dec text...

With this chained array of units you can do the math operations like this:

Doing operation on array of uints is much much more faster than on strings ...

but if you insist yes you can use string representation too ...

There are also hybrid representation like BCD that are suitable for this but your MCU would need to have support for it ...

Spektre
  • 49,595
  • 11
  • 110
  • 380
  • Thanks for answering @Spektre. So, basically I should take my two 32 bit numbers, convert it to hex and then to a string. Concatenate these two strings and convert to decimal? – kelvinandrade Dec 11 '18 at 11:55
  • @KelvinCésardeAndrade yes like that only you can cast hex string from the two uints directly ... – Spektre Dec 11 '18 at 17:25
1

Depending on your language of choice, the language may allow you to use greater-than-32bit integers, even on 32bits architectures (like python). If that is the case the problem becomes trivial: compute the value, then compute the corresponding hex string.

David Raluy
  • 184
  • 5