1

In my application i have to convert long long number into 8 byte array. Then i have to convert 8 byte array into hexadecimel string. Can you please help me in this. i'm struck up.

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
PremPalanisamy
  • 116
  • 1
  • 12

2 Answers2

3

One way to do integer/byte array conversion is to use a union:

union {
    long long l;
    uint8_t b[sizeof(long long)];
} u;

u.l = mylonglong;

Then u.b[] contains the bytes, which can be accessed individually.

EDIT: Please note as pointed out by @NikolaiRuhe this use of union can lead to undefined behaviour, so it might be best to use memcpy() instead:

uint8_t b[sizeof(long long)];
memcpy(b, &mylonglong, sizeof(b));

If you want the hex string of the long long in native-endian order, then:

void hexChar(uint8_t b, char *out)
{
    static const char *chars = "0123456789abcdef";
    out[0] = chars[(b >> 4) & 0xf];
    out[1] = chars[b & 0xf];
}

// Make sure outbuf is big enough
void hexChars(const uint8_t *buffer, size_t len, char *outbuf)
{
    for (size_t i = 0; i < len; i++)
    {
        hexChar(buffer[i], outbuf);
        outbuf += 2;
    }
    *outbuf = '\0';
}

and call it with:

char hex[32];
hexChars(u.b, sizeof(u.b), hex);

However if instead you want the hex value of the long long:

char hex[32];
sprintf(hex, "%llx", mylonglong);
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • I'm new to this. Can you please correct me if i'm doing it wrong way. union { long long l; uint8_t b[sizeof(long long)]; } u; u.l = longvalue; dataWithBytes=[NSData dataWithBytes:u.b length:sizeof(u.b)]; Then i'm converting NSData into Hex string. But i'm getting wrong result. – PremPalanisamy May 31 '13 at 12:26
  • @PremPalanisamy Yeah that looks OK to me. – trojanfoe May 31 '13 at 12:27
  • @PremPalanisamy How are you converting the `NSData` object into a hex string? Have you considered endian issues (i.e. iOS and OSX use little endian where the byte order is reversed)? – trojanfoe May 31 '13 at 12:29
  • Using a union to write one element and then read another relies on undefined behavior. It probably works on most platforms, yet the C standard explicitly warns about this. – Nikolai Ruhe May 31 '13 at 12:30
  • @NikolaiRuhe That surprises me. Can you provide a link please? – trojanfoe May 31 '13 at 12:32
  • I dont have a link to the standard (I'm on a phone) but google has lots of hits for "C union undefined behavior". Here's one: http://stackoverflow.com/a/2310534/104790 – Nikolai Ruhe May 31 '13 at 12:37
  • Generally in C, when converting data types bitwise it's necessary to use `memcpy`. – Nikolai Ruhe May 31 '13 at 12:40
  • @NikolaiRuhe OK thanks. I will need to caveat that use of `union` then to make it obvious it can lead to undefined behaviour and I will suggest `memcpy()` instead. – trojanfoe May 31 '13 at 12:42
  • char hex[32]; sprintf(hex, "%llx", mylonglong); This will do the trick. Thanks for the quick response. Anyway have to cross check with server decryption also. – PremPalanisamy May 31 '13 at 13:03
0

would that do the trick ?

#include <stdio.h>
int main() {
    long long int val = 0x424242;
    char str_val[32];
    snprintf(str_val, sizeof(str_val), "%#llx", val);
    printf("Value : %s\n", str_val);
}
DCMaxxx
  • 2,534
  • 2
  • 25
  • 46
  • Sorry, I assumed that you just need to put it in a string. If you really need to get your 8 byte array, see trojanfoe's solution. – DCMaxxx May 31 '13 at 12:19