2

I am looking for a way to convert epoch time in decimal to hexadecimal

(for example: decimal = 1417502160098 and hexadecimal = 14a09b674e2).

Loadrunner script is using hexadecimal value to get the latest image.

I am finding this difficult as there is no identifier in c to weight the decimal value of 13 digits.

Please let me know if any one has tried this in Loadrunner.

Koby Douek
  • 16,156
  • 19
  • 74
  • 103
Andy Savdt
  • 21
  • 3
  • You need this value in a string? Obviously internal representation is in binary. For a number of that size I hope that you're using a `long`. – Jonathan Mee Dec 02 '14 at 11:49
  • @Jonathan long data type has maximum of 32 bits in loadrunner, and this timestamp crosses 32 bits, so definitely I need the value in character array, but how do we convert it to hexadecimal in Loadrunner/ – Andy Savdt Dec 02 '14 at 11:54
  • Apologies, I meant a `long long`, so you have 64-bits. [unwind](http://stackoverflow.com/users/28169/unwind)'s answer shows how to convert to a string. But note that for a 64-bit integer you'll only need a max of 16 characters + 1 character for the '\0'. – Jonathan Mee Dec 02 '14 at 12:12

3 Answers3

1

There is an 64-bit integer type in C. It's called int64_t and can be used like this:

#include <stdio.h>
#include <inttypes.h>

int main(void)
{
   int64_t epoch = 1417502160098LL;

   printf("%" PRIx64 "\n", epoch);

   return 0;
}
Lars Ljung
  • 375
  • 2
  • 6
1

I'm not at all sure what you mean by "there is no identifier in c to weight the decimal value of 13 digits".

However, if you want to do this in C code, you would typically use the snprintf() function to convert a variable's value to a string of hexadecimal digits:

#include <stdio.h>
#include <inttypes.h>

const uint64_t value = 1417502160098UL;
char hexbuf[32];

snprintf(hexbuf, sizeof hexbuf, "%" PRIx64, value);

UPDATE: If you don't have access to uint64_t, figure out if the compiler's unsigned long long type is large enough, it's often 64 bits which is what you need. If that fails, you might need to roll your own high-precision integer, since you only need e.g. a pair of 32-bit numbers it shouldn't be too hard.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • Thanks for the information, but inttypes.h was included in C as C99 from 1999, but Loadrunner uses the basic C, so above code didn't turn well to me. – Andy Savdt Dec 03 '14 at 07:10
1

As noted above, you need C code to do this. Although you need a set of code which is both 32 bit and 64 bit compliant, so the use of the 64 bit integer type is not likely to be of much use in this case.

This conversion from one numerical base to another is a fairly common exercise for first year programming students in college, so you may find a routine in any number of textbooks. I am also including a link to a sample (untested/unvalidated)

http://www.programiz.com/c-programming/examples/hexadecimal-decimal-convert

James Pulley
  • 5,606
  • 1
  • 14
  • 14