Sicne you talk about an embedded application I assume that you want to save the numbers as values and not as strings/characters. So if you just want to store your character data as numbers (for example in an integer), you can use sscanf.

This means you could do something like this:

 char source_val[] = {'0','A','0','3','B','7'} // Represents the numbers 0x0A, 0x03 and 0xB7
 uint8 dest_val[3];                            // We want to save 3 numbers
 for(int i = 0; i<3; i++)
 {
     sscanf(&source_val[i*2],"%x%x",&dest_val[i]); // Everytime we read two chars --> %x%x
 }
 // Now dest_val contains 0x0A, 0x03 and 0xB7

However if you want to store it as a string (like in your example), you can't use unsigned char since this type is also just 8-Bit long, which means it can only store one character. Displaying 'B3' in a single (unsigned) char does not work.

edit: Ok according to comments, the goal is to save the passed data as a numerical value. Unfortunately the compiler from the opener does not support sscanf which would be the easiest way to do so. Anyhow, since this is (in my opinion) the simplest approach, I will leave this part of the answer at it is and try to add a more custom approach in this edit.

Regarding the data type, it actually doesn't matter if you have uint8. Even though I would advise to use some kind of integer data type, you can also store your data into an unsigned char. The problem here is, that the data you get passed, is a character/letter, that you want to interpret as a numerical value. However, the internal storage of your character differs. You can check the ASCII Table, where you can check the internal values for every character. For example:

char letter = 'A'; // Internally 0x41 
char number = 0x61; // Internally 0x64 - represents the letter 'a'

As you can see there is also a differnce between upper an lower case.

If you do something like this:

int myVal = letter;  //

myVal won't represent the value 0xA (decimal 10), it will have the value 0x41.

The fact you can't use sscanf means you need a custom function. So first of all we need a way to conver one letter into an integer:

int charToInt(char letter)
{
    int myNumerical;
    // First we want to check if its 0-9, A-F, or a-f) --> See ASCII Table
    if(letter > 47 && letter < 58)
    {
        // 0-9
        myNumerical = letter-48;
        // The Letter "0" is in the ASCII table at position 48 -> meaning if we subtract 48 we get 0 and so on...
    }
    else if(letter > 64 && letter < 71)
    {
       // A-F
       myNumerical = letter-55 
       // The Letter "A" (dec 10) is at Pos 65 --> 65-55 = 10 and so on..
    }
    else if(letter > 96 && letter < 103)
    {
       // a-f
       myNumerical = letter-87
       // The Letter "a" (dec 10) is at Pos 97--> 97-87 = 10 and so on...
    }
    else
    {
       // Not supported letter...
       myNumerical = -1;
    }
    return myNumerical;
}

Now we have a way to convert every single character into a number. The other problem, is to always append two characters together, but this is rather easy:

int appendNumbers(int higherNibble, int lowerNibble)
{
     int myNumber = higherNibble << 4;
     myNumber |= lowerNibbler;
     return myNumber;
    // Example: higherNibble = 0x0A, lowerNibble = 0x03;  -> myNumber 0 0xA3
    // Of course you have to ensure that the parameters are not bigger than 0x0F 
}

Now everything together would be something like this:

 char source_val[] = {'0','A','0','3','B','7'} // Represents the numbers 0x0A, 0x03 and 0xB7
 int dest_val[3];                             // We want to save 3 numbers
 int temp_low, temp_high;
 for(int i = 0; i<3; i++)
 {
     temp_high = charToInt(source_val[i*2]);
     temp_low = charToInt(source_val[i*2+1]);
     dest_val[i] = appendNumbers(temp_high , temp_low);
 }

I hope that I understood your problem right, and this helps..

Answer from Toby on Stack Overflow
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ converting string into byte array in ascii presentation
r/learnpython on Reddit: Converting String Into Byte Array In ASCII Presentation
April 21, 2023 -

Hello everyone!

I am trying to convert a string into a byte array. If I for example use the string 'Hello' I want to get something like this to store it in a byte array variable:

b'\x48\x45\x4C\x4C\x4F'

What I tried is following:

buf = bytearray(50) # length of text string varies, so I reserved a buffer for a max length of 50 chars

char_arr = [char for char in text]

for i in range(0, len(char_arr)):

buf[i] = hex(ord(char_arr[i]))

This could possibly be total nonsense and I feel like I am making things way too complicated. The variable "text" is the string i read from. I tried converting the string into a char array to read it into the byte array. While doing that I used hex(ord()) to convert the char into its hexadecimal ASCII representation. I just started programming in this language to use CircuitPython on my Raspberry Pi Pico. I get an error called "can't convert str to int". Any suggestions? Thanks in advance!

๐ŸŒ
Online Tools
onlinetools.com โ€บ ascii โ€บ convert-ascii-to-bytes
Convert ASCII to Bytes โ€“ Online ASCII Tools
A simple browser-based utility that converts ASCII strings to bytes. Just paste your ASCII string in the input area and you will instantly get bytes in the output area. Fast, free, and without ads. Import ASCII โ€“ get bytes.
Discussions

android - how to convert ascii to byte array format - Stack Overflow
i want to convert the ascii code to byte array format: Code : byte[] byteArr = passenger_sign.getBytes(); Output : [B@34c59da i dont know how to convert it to byte array format,anyone have solut... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to convert ASCII Hex string to Byte array? | Qt Forum
Hi, I try to convert a string, containing hex chars only, to a byte array. I could do it in C but in Qt I did not find any solution :) . Yes it is easy and t... More on forum.qt.io
๐ŸŒ forum.qt.io
March 22, 2021
c# - Converting string of ASCII code list to byte array - Stack Overflow
If it was a hex string it can be ... the code it works also just an idea how to separate ASCII characters like : 246, 29, 70, 28, 57,... Summary Edit: Actually I am trying to reverse the string back to its byte array.... More on stackoverflow.com
๐ŸŒ stackoverflow.com
c# - Byte[] to ASCII - Stack Overflow
As an alternative to reading a data from a stream to a byte array, you could let the framework handle everything and just use a StreamReader set up with an ASCII encoding to read in the string. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Top answer
1 of 4
4

Sicne you talk about an embedded application I assume that you want to save the numbers as values and not as strings/characters. So if you just want to store your character data as numbers (for example in an integer), you can use sscanf.

This means you could do something like this:

 char source_val[] = {'0','A','0','3','B','7'} // Represents the numbers 0x0A, 0x03 and 0xB7
 uint8 dest_val[3];                            // We want to save 3 numbers
 for(int i = 0; i<3; i++)
 {
     sscanf(&source_val[i*2],"%x%x",&dest_val[i]); // Everytime we read two chars --> %x%x
 }
 // Now dest_val contains 0x0A, 0x03 and 0xB7

However if you want to store it as a string (like in your example), you can't use unsigned char since this type is also just 8-Bit long, which means it can only store one character. Displaying 'B3' in a single (unsigned) char does not work.

edit: Ok according to comments, the goal is to save the passed data as a numerical value. Unfortunately the compiler from the opener does not support sscanf which would be the easiest way to do so. Anyhow, since this is (in my opinion) the simplest approach, I will leave this part of the answer at it is and try to add a more custom approach in this edit.

Regarding the data type, it actually doesn't matter if you have uint8. Even though I would advise to use some kind of integer data type, you can also store your data into an unsigned char. The problem here is, that the data you get passed, is a character/letter, that you want to interpret as a numerical value. However, the internal storage of your character differs. You can check the ASCII Table, where you can check the internal values for every character. For example:

char letter = 'A'; // Internally 0x41 
char number = 0x61; // Internally 0x64 - represents the letter 'a'

As you can see there is also a differnce between upper an lower case.

If you do something like this:

int myVal = letter;  //

myVal won't represent the value 0xA (decimal 10), it will have the value 0x41.

The fact you can't use sscanf means you need a custom function. So first of all we need a way to conver one letter into an integer:

int charToInt(char letter)
{
    int myNumerical;
    // First we want to check if its 0-9, A-F, or a-f) --> See ASCII Table
    if(letter > 47 && letter < 58)
    {
        // 0-9
        myNumerical = letter-48;
        // The Letter "0" is in the ASCII table at position 48 -> meaning if we subtract 48 we get 0 and so on...
    }
    else if(letter > 64 && letter < 71)
    {
       // A-F
       myNumerical = letter-55 
       // The Letter "A" (dec 10) is at Pos 65 --> 65-55 = 10 and so on..
    }
    else if(letter > 96 && letter < 103)
    {
       // a-f
       myNumerical = letter-87
       // The Letter "a" (dec 10) is at Pos 97--> 97-87 = 10 and so on...
    }
    else
    {
       // Not supported letter...
       myNumerical = -1;
    }
    return myNumerical;
}

Now we have a way to convert every single character into a number. The other problem, is to always append two characters together, but this is rather easy:

int appendNumbers(int higherNibble, int lowerNibble)
{
     int myNumber = higherNibble << 4;
     myNumber |= lowerNibbler;
     return myNumber;
    // Example: higherNibble = 0x0A, lowerNibble = 0x03;  -> myNumber 0 0xA3
    // Of course you have to ensure that the parameters are not bigger than 0x0F 
}

Now everything together would be something like this:

 char source_val[] = {'0','A','0','3','B','7'} // Represents the numbers 0x0A, 0x03 and 0xB7
 int dest_val[3];                             // We want to save 3 numbers
 int temp_low, temp_high;
 for(int i = 0; i<3; i++)
 {
     temp_high = charToInt(source_val[i*2]);
     temp_low = charToInt(source_val[i*2+1]);
     dest_val[i] = appendNumbers(temp_high , temp_low);
 }

I hope that I understood your problem right, and this helps..

2 of 4
4

If you have a "proper" array, like value as declared in the question, then you loop over the size of it to get each character. If you're on a system which uses the ASCII alphabet (which is most likely) then you can convert a hexadecimal digit in character form to a decimal value by subtracting '0' for digits (see the linked ASCII table to understand why), and subtracting 'A' or 'a' for letters (make sure no letters are higher than 'F' of course) and add ten.

When you have the value from the first hexadeximal digit, then convert the second hexadecimal digit the same way. Multiply the first value by 16 and add the second value. You now have single byte value corresponding to two hexadecimal digits in character form.


Time for some code examples:

/* Function which converts a hexadecimal digit character to its integer value */
int hex_to_val(const char ch)
{
    if (ch >= '0' && ch <= '9')
        return ch - '0';  /* Simple ASCII arithmetic */
    else if (ch >= 'a' && ch <= 'f')
        return 10 + ch - 'a';  /* Because hex-digit a is ten */
    else if (ch >= 'A' && ch <= 'F')
        return 10 + ch - 'A';  /* Because hex-digit A is ten */
    else
        return -1;  /* Not a valid hexadecimal digit */
}

...

/* Source character array */
char value []={'0','2','0','c','0','3'};

/* Destination "byte" array */
char val[3];

/* `i < sizeof(value)` works because `sizeof(char)` is always 1 */
/* `i += 2` because there is two digits per value */
/* NOTE: This loop can only handle an array of even number of entries */
for (size_t i = 0, j = 0; i < sizeof(value); i += 2, ++j)
{
    int digit1 = hex_to_val(value[i]);      /* Get value of first digit */
    int digit2 = hex_to_val(value[i + 1]);  /* Get value of second digit */

    if (digit1 == -1 || digit2 == -1)
        continue;  /* Not a valid hexadecimal digit */

    /* The first digit is multiplied with the base */
    /* Cast to the destination type */
    val[j] = (char) (digit1 * 16 + digit2);
}

for (size_t i = 0; i < 3; ++i)
    printf("Hex value %lu = %02x\n", i + 1, val[i]);

The output from the code above is

Hex value 1 = 02
Hex value 2 = 0c
Hex value 3 = 03

A note about the ASCII arithmetic: The ASCII value for the character '0' is 48, and the ASCII value for the character '1' is 49. Therefore '1' - '0' will result in 1.

๐ŸŒ
Online Tools
onlinetools.com โ€บ ascii โ€บ convert-bytes-to-ascii
Convert Bytes to ASCII โ€“ Online ASCII Tools
Asciiabulous! This tool takes bytes as input and converts them to readable ASCII strings. You can use the Byte radix option to set the radix of input bytes or leave it empty to automatically detect the radix.
๐ŸŒ
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 - Here, we created a function void string2ByteArray(char* input, BYTE* output), to convert ASCII string to BYTE array, the final output (array of integers) is storing in arr variable, which is passed as a reference in the function.
๐ŸŒ
GitHub
gist.github.com โ€บ 1097708
Convert string to packed ascii byte array ยท GitHub
Convert string to packed ascii byte array. GitHub Gist: instantly share code, notes, and snippets.
๐ŸŒ
Qt Forum
forum.qt.io โ€บ home โ€บ qt development โ€บ general and desktop โ€บ how to convert ascii hex string to byte array?
How to convert ASCII Hex string to Byte array? | Qt Forum
March 22, 2021 - bytearray[7]=0xAC ... Again, why create a QString ? Just use QByteArray directly or if you really really want a QString, use e.g. toLatin1 ยท Interested in AI ? www.idiap.ch Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct ... Again, why create a QString ? Just use QByteArray directly or if you really really want a QString, use e.g. toLatin1
Find elsewhere
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 74136800 โ€บ converting-string-of-ascii-code-list-to-byte-array
c# - Converting string of ASCII code list to byte array - Stack Overflow
If file is some (ascii) text, why don't read as text one: string text = File.ReadAllText(samplePicture.Text, Encoding.ASCII);? If file is binary, byte[] data = File.ReadAllBytes(samplePicture.Text); ... What is the actual question? How to convert pre-existing strings back into bytes?
๐ŸŒ
C# Corner
c-sharpcorner.com โ€บ article โ€บ c-sharp-string-to-byte-array
How to Convert String To Byte Array in C#
February 21, 2023 - The Encoding class provides the functionality to convert from one encoding to another, i.e., conversion from ASCII to Unicode. The Encoding.Covert() method converts a range of bytes or an entire byte array in a byte array from one encoding to ...
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ api โ€บ system.text.asciiencoding.getbytes
ASCIIEncoding.GetBytes Method (System.Text) | Microsoft Learn
Any Unicode character greater than U+007F is translated to an ASCII question mark ("?"). ... For security reasons, your application is recommended to use UTF8Encoding, UnicodeEncoding, or UTF32Encoding and enable error detection. ... Encodes a set of characters from the specified character array into the specified byte array.
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-38769.html
bytearray object - why converting to ascii?
November 21, 2022 - I am a new Python developer. Albeit new, I come from a C background. I have a file of bytes I want to read into a bytearray() object. SOME of the byte values are getting converted to an ascii value instead of remaining as a byte value. I must be...