Skip to content

Instantly share code, notes, and snippets.

@x225am
Last active April 18, 2024 11:37
Show Gist options
  • Save x225am/24152ecf5c71adc1635334e050d65002 to your computer and use it in GitHub Desktop.
Save x225am/24152ecf5c71adc1635334e050d65002 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdint.h>
void intToByteArray(int *array, unsigned long int number) {
for (int i = 0; i < 5; i++) {
array[i] = (number >> (8 * i)) & 0xFF;
}
}
long int byteArrayToInt(int *array) {
int number = 0;
for (int i = 0; i < 5; i++) {
number |= (int)array[i] << (8 * i);
}
return number;
}
int main() {
unsigned long int number = 98765455;
int byteArray[5];
// Convert integer to byte array
intToByteArray(byteArray, number);
// Print byte array
printf("Byte Array: ");
for (int i = 0; i < 5; i++) {
printf("%02X ", byteArray[i]);
}
printf("\n");
// Convert byte array back to integer
unsigned long int restoredNumber = byteArrayToInt(byteArray);
// Print restored integer
printf("Restored Number: %i\n", restoredNumber);
return 0;
}
////////////////////******************* Versopn 2 64 bit variable
#include <stdio.h>
#include <stdint.h>
void intToByteArray(uint8_t *array, uint64_t number) {
for (int i = 0; i < 5; i++) {
array[i] = (number >> (8 * i)) & 0xFF;
}
}
uint64_t byteArrayToInt(uint8_t *array) {
uint64_t number = 0;
for (int i = 0; i < 5; i++) {
number |= (uint64_t)array[i] << (8 * i);
}
return number;
}
int main() {
uint64_t number = 98;
uint8_t byteArray[5];
// Convert integer to byte array
intToByteArray(byteArray, number);
// Print byte array
printf("Byte Array: ");
for (int i = 0; i < 5; i++) {
printf("%02X ", byteArray[i]);
}
printf("\n");
// Convert byte array back to integer
uint64_t restoredNumber = byteArrayToInt(byteArray);
// Print restored integer
printf("Restored Number: %llu\n", restoredNumber);
return 0;
}
///////////////////////// text to array and back to text
#include <stdio.h>
#include <string.h>
#include <stdint.h>
void stringToByteArray(const char *str, uint8_t *array) {
int length = strlen(str);
for (int i = 0; i < length; i++) {
array[i] = (uint8_t)str[i];
}
}
void byteArrayToString(const uint8_t *array, char *str, int length) {
for (int i = 0; i < length; i++) {
str[i] = (char)array[i];
}
str[length] = '\0'; // Null-terminate the string
}
int main() {
const char *inputString = "this I need to save ";
uint8_t byteArray[strlen(inputString)]; // Use length of inputString as size
char outputString[strlen(inputString) + 1]; // Extra space for null terminator
// Convert string to byte array
stringToByteArray(inputString, byteArray);
// Print byte array
printf("Byte Array: ");
for (int i = 0; i < strlen(inputString); i++) {
printf("%02X ", byteArray[i]);
}
printf("\n");
// Convert byte array back to string
byteArrayToString(byteArray, outputString, strlen(inputString));
// Print restored string
printf("Restored String: %s\n", outputString);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment