简体   繁体   中英

convert decimal to hex, add to byte array

I am working on some image conversion in objective C, for iOS, and having some difficulties in saving the total image size in a byte array..

I did some searching, and stackoverflow has many questions with conversion from decimal to hex, but all of them explain how to print hex value using %x and printf(or NSLog), which is not what I want.

I have an image with size 2500

int totalSize  = 2500;

I want to add this size to specific index in image header

UInt8 headerData[HEADER_SIZE];
memset(headerData, 0, HEADER_SIZE);

//2500 dec  = 9C4 hex
headerData[2]   = 0xC4;
headerData[3]   = 0x09;
headerData[4]   = 0x00;
headerData[5]   = 0x00;    //image size total 4 bytes

I found this thread and tried it out

char hexval[5];
memset(hexval, 0, 5);
if (totalSize <= 0xFFFF){
    sprintf(&hexval[0], "%0x", totalSize);
}

printf("%x %x %x %x", hexval[0], hexval[1], hexval[2], hexval[3]);

and it printed

36 36 33 32

which is not the correct value, or am I missing something obvious here?

EDIT: Sorry, forgot put the endianness part.. I want to save in little endian format.

Note : I am adding a C tag also, since a C solution is applicable here too..

Revised : so this is not byte order issue, but here's code to properly get the byte values in little endian order, byte by byte:

int totalSize = 2500;

headerData[2] = totalSize >> 0 & 0xFF; // 0xC4
headerData[3] = totalSize >> 8 & 0xFF; // 0x09
headerData[4] = totalSize >> 16 & 0xFF; // 0x00
headerData[5] = totalSize >> 24 & 0xFF; // 0x00

That 2nd piece of code is just wrong though in many ways. What is it's purpose?

Do you perhaps just want this (no loops or anything):

printf("%04x", totalSize);

Or to get it to hexval C string and print that:

char hexval[5]; // actually, since totalSize can be > 0xFFFF, 9 would be better size
snprintf(hexval, sizeof hexval, "%04x", totalSize); // note: sizeof hexval only works if hexval is char array with static size
printf("%s", hexval);

Or do you really want the ASCII codes of individual characters, when value is printed out as hex string?


Hmm, also, when you say "with conversion from decimal to hex", what do you mean? Storing numeric value to a binary file (I presume your image file is binary file and not XML or something) has nothing to do with decimal/hexadecimal conversion. It is storing a number in memory to bytes in file, and decimal/hexadecimal does not really enter into it at all (except to print values for humans to read for debugging purposes).

If you are doing this under iOS, which is little endian, and you want little endian order, it's as simple as:

*(uint32_t *)(header + 2) = totalSize;

If you want a cross-platform solution, then the byte-by-byte solution provided by @hyde is the way to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM