Home » Code Snippets » C/C++ Code Snippets

C program to extract bytes from a long integer hexadecimal number

By: IncludeHelp On 08 DEC 2016

Here, we will learn how to extract bytes from a long integer number (or convert hexadecimal long integer value into byte array)?

In this program we are taking a long integer hexadecimal number of 8 bytes and converting it into byte array i.e. extracting bytes (one- one byte hexadecimal values) from long integer hexadecimal number.

Extract bytes from long int hexadecimal number in c language

//Extracted bytes from long int hexadecimal values

#include <stdio.h>
typedef unsigned char BYTE;
int main()
{
	int i,j;
	long int hexValue=0x1122334455667788;
	BYTE byteVal[8];
	
	for(j=0,i=7; i>=0; i--,j++){
		byteVal[j]= (hexValue>>(i*8))&0xff;
	}		
	
	printf("Hexadecimal bytes are:\n");
	for(j=0;j<8;j++){
		printf("%02X ",byteVal[j]);
	}
 
	return 0;
}

Output

    Hexadecimal bytes are:
    11 22 33 44 55 66 77 88

COMMENTS




Copyright © 2024 www.includehelp.com. All rights reserved.