Skip to content

Commit

Permalink
Add new sub-package HexUtils
Browse files Browse the repository at this point in the history
This new sub-package contains two utility methods for converting byte
array from/to hex string.
  • Loading branch information
cheeriotb committed Feb 28, 2021
1 parent 085a486 commit e9a8f17
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
24 changes: 24 additions & 0 deletions HexUtils/README.md
@@ -0,0 +1,24 @@
# HexUtils

## Introduction

This is a simple sample code for converting hex string to/from byte array.

## Usage

You can easily understand how to use the following 2 utility methods:

- unsigned char *util_hex_string_to_byte_array(const char *string, size_t length)
- char *util_byte_array_to_hex_string(const unsigned char* array, size_t length)

## Dependency

There is no special dependency on other external libraries.

## Licence

This software is released under the MIT License, see LICENSE.

## Author

Cheerio (cheerio.the.bear@gmail.com)
100 changes: 100 additions & 0 deletions HexUtils/hex_utils.c
@@ -0,0 +1,100 @@
/*
* Copyright (C) 2021 Cheerio <cheerio.the.bear@gmail.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the MIT license.
* See the license information described in LICENSE file.
*/

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static const char *UTIL_CONVERT_TO_HEX_CHAR = "0123456789ABCDEF";

unsigned char *util_hex_string_to_byte_array(const char *string, size_t length)
{
unsigned char *array;
int index;
unsigned char octet;
char nibble;

if ((array = (unsigned char *) malloc(length / 2)) == NULL) {
return NULL;
}

for (index = 0; index < length; index++) {
nibble = string[index];

if (('0' <= nibble) && (nibble <= '9')) {
nibble = nibble - '0';
} else if (('a' <= nibble) && (nibble <= 'f')) {
nibble = 0x0A + (nibble - 'a');
} else if (('A' <= nibble) && (nibble <= 'F')) {
nibble = 0x0A + (nibble - 'A');
} else {
free(array);
return NULL;
}

if ((index % 2) == 0) {
octet = nibble;
} else {
array[index / 2] = (octet << 4) + nibble;
}
}

return array;
}

char *util_byte_array_to_hex_string(const unsigned char* array, size_t length)
{
char *string;
char *destination;
int index;
unsigned char octet;

if (length == 0) {
return NULL;
}

if ((string = destination = (char *) malloc(length * 2 + 1)) == NULL) {
return NULL;
}

for (index = 0; index < length; index++) {
octet = array[index];
*destination++ = UTIL_CONVERT_TO_HEX_CHAR[octet / 0x10];
*destination++ = UTIL_CONVERT_TO_HEX_CHAR[octet % 0x10];
}

*destination = '\0';
return string;
}

int main(int argc, char *argv[]) {

const char *data = "00017F8081FEFF";
unsigned char *array;
char *string;

array = util_hex_string_to_byte_array(data, strlen(data));
assert(array[0] == 0x00);
assert(array[1] == 0x01);
assert(array[2] == 0x7F);
assert(array[3] == 0x80);
assert(array[4] == 0x81);
assert(array[5] == 0xFE);
assert(array[6] == 0xFF);

string = util_byte_array_to_hex_string(array, strlen(data) / 2);
assert(strcmp(data, string) == 0);

free(array);
free(string);

printf("OK\n");

exit(0);
}

0 comments on commit e9a8f17

Please sign in to comment.