As far as I know, there's no standard function to do so, but it's simple to achieve in the following manner:

#include <stdio.h>

int main(int argc, char **argv) {
    const char hexstring[] = "DEadbeef10203040b00b1e50", *pos = hexstring;
    unsigned char val[12];

     /* WARNING: no sanitization or error-checking whatsoever */
    for (size_t count = 0; count < sizeof val/sizeof *val; count++) {
        sscanf(pos, "%2hhx", &val[count]);
        pos += 2;
    }

    printf("0x");
    for(size_t count = 0; count < sizeof val/sizeof *val; count++)
        printf("%02x", val[count]);
    printf("\n");

    return 0;
}

Edit

As Al pointed out, in case of an odd number of hex digits in the string, you have to make sure you prefix it with a starting 0. For example, the string "f00f5" will be evaluated as {0xf0, 0x0f, 0x05} erroneously by the above example, instead of the proper {0x0f, 0x00, 0xf5}.

Answer from Michael F on Stack Overflow
🌐
IncludeHelp
includehelp.com › c › convert-ascii-string-to-byte-array-in-c.aspx
Convert ASCII string (char[]) to BYTE array in C
April 20, 2023 - Extract characters from the input string and get the character's value in integer/number format using %d format specifier, %d gives integer (number) i.e. BYTE value of any character. Add these bytes (number) which is an integer value of an ASCII character to the output array.
🌐
Reddit
reddit.com › r/learnprogramming › c help needed. (string to byte array)
r/learnprogramming on Reddit: C help needed. (string to BYTE array)
December 23, 2022 -

Hello:

Would someone please point me to the solution for this? ( I am a C newbie )

I am running a C program with an Input argument .

This argument (argv[1]) is a string containing "1112131415e0",

I need to have this string stored in a BYTE array (container1) such that is the same as if I wrote :

BYTE container1[] = { 0x11, 0x12, 0x13, 0x14,0x14,0xe0}

I don't want to convert 11 to hex.. I just want to add the 0x and have it stored.

Thanks a lot!

Discussions

convert byte array into string
I'll even tell you some standard ... string (optional) Now, show us your code. -- Lew Pitcher Master Codewright & JOAT-in-training | Registered Linux User #112576 http://pitcher.digitalfreehold.ca/ | GPG public key available by request ---------- Slackware - Because I know what I'm doing. ------ ... Using c language what's the best way to convert a byte array as char * ... More on thecodingforums.com
🌐 thecodingforums.com
9
February 8, 2009
Convert string to bytes array
I receive a string by serial as: "13ABF2C1" and I want to convert it to bytes as: byte b[4] = {13, AB, F2, C1} so that when i rewrite: · I think using scanf() is often an H-bomb-to-kill-an-ant. It can do a lot of things, but programs often use only a small fraction of its utility, resulting ... More on forum.arduino.cc
🌐 forum.arduino.cc
19
0
June 29, 2015
String to byte array
What is a correct way to convert Julia-String UTF8 to byte array which can be exported as C-like string char* using ccall callback function ? pseudo-code: jString = String("a_šČô_φ_привет_일_迪弗尺") strByteArray = toByteA… More on discourse.julialang.org
🌐 discourse.julialang.org
5
0
May 16, 2017
Converting string to byte array in C# - Stack Overflow
Timothy, I've looked through the VB code and I can't seem to find a byte array as you have mentioned. 2013-04-18T01:06:48.163Z+00:00 ... All I can see is that there are a number items attached to Properties as a string. I'm not sure if that's what you were asking me though. More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 16
83

As far as I know, there's no standard function to do so, but it's simple to achieve in the following manner:

#include <stdio.h>

int main(int argc, char **argv) {
    const char hexstring[] = "DEadbeef10203040b00b1e50", *pos = hexstring;
    unsigned char val[12];

     /* WARNING: no sanitization or error-checking whatsoever */
    for (size_t count = 0; count < sizeof val/sizeof *val; count++) {
        sscanf(pos, "%2hhx", &val[count]);
        pos += 2;
    }

    printf("0x");
    for(size_t count = 0; count < sizeof val/sizeof *val; count++)
        printf("%02x", val[count]);
    printf("\n");

    return 0;
}

Edit

As Al pointed out, in case of an odd number of hex digits in the string, you have to make sure you prefix it with a starting 0. For example, the string "f00f5" will be evaluated as {0xf0, 0x0f, 0x05} erroneously by the above example, instead of the proper {0x0f, 0x00, 0xf5}.

2 of 16
25

I found this question by Googling for the same thing. I don't like the idea of calling sscanf() or strtol() since it feels like overkill. I wrote a quick function which does not validate that the text is indeed the hexadecimal presentation of a byte stream, but will handle odd number of hex digits:

uint8_t tallymarker_hextobin(const char * str, uint8_t * bytes, size_t blen)
{
   uint8_t  pos;
   uint8_t  idx0;
   uint8_t  idx1;

   // mapping of ASCII characters to hex values
   const uint8_t hashmap[] =
   {
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //  !"#$%&'
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ()*+,-./
     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // 01234567
     0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 89:;<=>?
     0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, // @ABCDEFG
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // HIJKLMNO
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // PQRSTUVW
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // XYZ[\]^_
     0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, // `abcdefg
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // hijklmno
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // pqrstuvw
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // xyz{|}~.
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00  // ........
   };

   bzero(bytes, blen);
   for (pos = 0; ((pos < (blen*2)) && (pos < strlen(str))); pos += 2)
   {
      idx0 = (uint8_t)str[pos+0];
      idx1 = (uint8_t)str[pos+1];
      bytes[pos/2] = (uint8_t)(hashmap[idx0] << 4) | hashmap[idx1];
   };

   return(0);
}
🌐
Bytes
bytes.com › home › forum › topic
HELP PLEASE - How to convert string to byte array - Post.Byes
November 2, 2015 - A C string can be used like a byte array. For example: char *s = "Hello, world!"; char c = s[0]; /* c now contains 'H' */ -- /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\ \-------------------------------------------------------- rules!
🌐
GitHub
gist.github.com › xsleonard › 7341172
hex string to byte array, C · GitHub
To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters ... This is freaking ingenious. ... int byteArrayToHexString(uint8_t *byte_array, int byte_array_len, char *hexstr, int hexstr_len) { int off = 0; int i; for (i = 0; i < byte_array_len; i ++) { off += snprintf(hexstr + off, hexstr_len - off, "x", byte_array[i]); } hexstr[off] = '\0'; return off; }
🌐
Quora
quora.com › How-do-I-convert-a-string-into-bytes-in-C
How to convert a string into bytes in C - Quora
Answer (1 of 3): How do I convert a string into bytes in C? Umm.. In C, a “string” is *already* bytes. That’s why the ‘char’ variable type exists. char mystring[] = “Hello world”; Its an array of char. You want the 4th byte? mystring[3]. Remember to make sure that there’s a ...
Find elsewhere
🌐
The Coding Forums
thecodingforums.com › archive › archive › c programming
convert byte array into string | C Programming | Coding Forums
February 8, 2009 - -- Lew Pitcher Master Codewright ... c language what's the best way to convert a byte array as char * An example: byte array[]={10, 255, 10}...
🌐
CryptoSys
cryptosys.net › pki › manpki › pki_stringstobytes.html
Converting strings to bytes and vice versa
VB6 stores its strings internally in "Unicode" format, two bytes per character, but the StrConv function will convert to an array of bytes encoded in "ANSI" format using your default code page.
🌐
Online String Tools
onlinestringtools.com › convert-string-to-bytes
Convert a String to Bytes – Online String Tools
Free online string to bytes converter. Just load your string and it will automatically get converted to a byte array. There are no intrusive ads, popups or nonsense, just a simple string converter. Load a string – get bytes.
🌐
C# Corner
c-sharpcorner.com › home › technologies › c# strings › how to convert string to byte array in c#
How to Convert String To Byte Array in C#
February 21, 2023 - string s = new string(data.Sel... is an abstract class. That means you can use its functionality via its derived classes only. The Encoding.GetBytes() method converts a string into a bytes array in C#....
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Convert string to bytes array - Programming - Arduino Forum
June 29, 2015 - HI, I receive a string by serial as: "13ABF2C1" and I want to convert it to bytes as: byte b[4] = {13, AB, F2, C1} so that when i rewrite: for(int i=0; i
🌐
Julia Programming Language
discourse.julialang.org › new to julia
String to byte array - New to Julia - Julia Programming Language
May 16, 2017 - What is a correct way to convert Julia-String UTF8 to byte array which can be exported as C-like string char* using ccall callback function ? pseudo-code: jString = String("a_šČô_φ_привет_일_迪弗尺") strByteArray = toByteArray(jString) len = length(strByteArray) # fnc is pointer to C callback function void(__stdcall*)(const char* str, const size_t len); fnc != Ptr{Void}(0) && ccall(fnc, Void, (Cstring, Int64), strByteArray, len)
🌐
Cprogramming
cboard.cprogramming.com › cplusplus-programming › 138795-converting-string-byte-cplusplus.html
converting string to byte in c++ - C Board - Cprogramming.com
June 13, 2011 - A Java String is a sequence of Java chars, which are UTF-16 codepoints. getBytes() without arguments takes the platform default encoding (some Windows-xxxx codepage under Windows, UTF-8 on most Unices) and converts the Unicode sequence to that encoding, returning the resulting data as a byte array.
🌐
CCS, Inc.
ccsinfo.com › forum › viewtopic.php
CCS :: View topic - how to convert a byte array to a string....
Profile Log in to check your private messages Log in · CCS does not monitor this forum on a regular basis
🌐
C# Corner
c-sharpcorner.com › home › technologies › c# strings › convert byte array to string in c#
Convert Byte Array To String In C#
February 9, 2023 - This code snippet is an example of how to convert a byte array into a string. String conversion includes two types.