How to convert a char array to a byte array?
I'm working on my project and now I'm stuck with a problem that is, how can I convert a char array to a byte array?.
For example: I need to convert char[9]
"fff2bdf1"
to a byte array that is byte[4]
is 0xff,0xf2,0xbd,0xf1
.
c++ arrays arduino char
add a comment |
I'm working on my project and now I'm stuck with a problem that is, how can I convert a char array to a byte array?.
For example: I need to convert char[9]
"fff2bdf1"
to a byte array that is byte[4]
is 0xff,0xf2,0xbd,0xf1
.
c++ arrays arduino char
So you want to parse the hex values?
– Karsten Koop
Jun 2 '16 at 7:20
add a comment |
I'm working on my project and now I'm stuck with a problem that is, how can I convert a char array to a byte array?.
For example: I need to convert char[9]
"fff2bdf1"
to a byte array that is byte[4]
is 0xff,0xf2,0xbd,0xf1
.
c++ arrays arduino char
I'm working on my project and now I'm stuck with a problem that is, how can I convert a char array to a byte array?.
For example: I need to convert char[9]
"fff2bdf1"
to a byte array that is byte[4]
is 0xff,0xf2,0xbd,0xf1
.
c++ arrays arduino char
c++ arrays arduino char
edited Mar 6 '18 at 15:12
gre_gor
4,18592631
4,18592631
asked Jun 2 '16 at 3:02
Trung Phạm KhánhTrung Phạm Khánh
812
812
So you want to parse the hex values?
– Karsten Koop
Jun 2 '16 at 7:20
add a comment |
So you want to parse the hex values?
– Karsten Koop
Jun 2 '16 at 7:20
So you want to parse the hex values?
– Karsten Koop
Jun 2 '16 at 7:20
So you want to parse the hex values?
– Karsten Koop
Jun 2 '16 at 7:20
add a comment |
3 Answers
3
active
oldest
votes
Here is a little Arduino sketch illustrating one way to do this:
void setup() {
Serial.begin(9600);
char arr = "abcdef98";
byte out[4];
auto getNum = (char c){ return c > '9' ? c - 'a' + 10 : c - '0'; };
byte *ptr = out;
for(char *idx = arr ; *idx ; ++idx, ++ptr ){
*ptr = (getNum( *idx++ ) << 4) + getNum( *idx );
}
//Check converted byte values.
for( byte b : out )
Serial.println( b, HEX );
}
void loop() {
}
The loop will keep converting until it hits a null character. Also the code used in getNum
only deals with lower case values. If you need to parse uppercase values its an easy change. If you need to parse both then its only a little more code, I'll leave that for you if needed (let me know if you cannot work it out and need it).
This will output to the serial monitor the 4 byte values contained in out
after conversion.
AB
CD
EF
98
Edit: How to use different length inputs.
The loop does not care how much data there is, as long as there are an even number of inputs (two ascii chars for each byte of output) plus a single terminating null. It simply stops converting when it hits the input strings terminating null.
So to do a longer conversion in the sketch above, you only need to change the length of the output (to accommodate the longer number). I.e:
char arr = "abcdef9876543210";
byte out[8];
The 4
inside the loop doesn't change. It is shifting the first number into position.
For the first two inputs ("ab"
) the code first converts the 'a' to the number 10
, or hexidecimal A
. It then shifts it left 4 bits, so it resides in the upper four bits of the byte: 0A
to A0
. Then the second value B
is simply added to the number giving AB
.
Thank you for your comment. I'm trying to understand your code and edit it to the case of an array of 6 bytes. I edited the number '4' to 6 but it's not work. Can you explain a little bit further about your code?.
– Trung Phạm Khánh
Jun 3 '16 at 14:41
@TrungPhạmKhánh I have edited the end of the question with an explanation.
– Chris A
Jun 3 '16 at 19:22
How to parse only upper case? If thechar arr = "ABCDEF98"
– Embedded Geek
Jun 24 '17 at 12:41
add a comment |
Just shift 0 or 1 to its position in binary format :)
char lineChars[8] = {1,1,0,0,0,1,0,1};
char lineChar = 0;
for(int i=0; i<8;i++)
{
lineChar |= lineChars[i] << (7-i);
}
Example 2. But is not tested!
void abs()
{
char* charData = new char;
*charData = 'h';
BYTE* byteData = new BYTE;
*byteData = *(BYTE*)charData;
}
Can you explain a bit more what's going on in your code? I don't understand how this parses hex values.
– Karsten Koop
Jun 2 '16 at 7:21
add a comment |
Assuming you want to parse the hex values in your string, and two letters always make up one byte value (so you use leading zeros), you can use sscanf
like this:
char input = "fff2bdf1";
unsigned char output[4];
for (int i=0; i<4; i++) {
sscanf(&input[i*2], "%02xd", &data[i]);
}
Thank you for your support.
– Trung Phạm Khánh
Jun 3 '16 at 14:39
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37581925%2fhow-to-convert-a-char-array-to-a-byte-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here is a little Arduino sketch illustrating one way to do this:
void setup() {
Serial.begin(9600);
char arr = "abcdef98";
byte out[4];
auto getNum = (char c){ return c > '9' ? c - 'a' + 10 : c - '0'; };
byte *ptr = out;
for(char *idx = arr ; *idx ; ++idx, ++ptr ){
*ptr = (getNum( *idx++ ) << 4) + getNum( *idx );
}
//Check converted byte values.
for( byte b : out )
Serial.println( b, HEX );
}
void loop() {
}
The loop will keep converting until it hits a null character. Also the code used in getNum
only deals with lower case values. If you need to parse uppercase values its an easy change. If you need to parse both then its only a little more code, I'll leave that for you if needed (let me know if you cannot work it out and need it).
This will output to the serial monitor the 4 byte values contained in out
after conversion.
AB
CD
EF
98
Edit: How to use different length inputs.
The loop does not care how much data there is, as long as there are an even number of inputs (two ascii chars for each byte of output) plus a single terminating null. It simply stops converting when it hits the input strings terminating null.
So to do a longer conversion in the sketch above, you only need to change the length of the output (to accommodate the longer number). I.e:
char arr = "abcdef9876543210";
byte out[8];
The 4
inside the loop doesn't change. It is shifting the first number into position.
For the first two inputs ("ab"
) the code first converts the 'a' to the number 10
, or hexidecimal A
. It then shifts it left 4 bits, so it resides in the upper four bits of the byte: 0A
to A0
. Then the second value B
is simply added to the number giving AB
.
Thank you for your comment. I'm trying to understand your code and edit it to the case of an array of 6 bytes. I edited the number '4' to 6 but it's not work. Can you explain a little bit further about your code?.
– Trung Phạm Khánh
Jun 3 '16 at 14:41
@TrungPhạmKhánh I have edited the end of the question with an explanation.
– Chris A
Jun 3 '16 at 19:22
How to parse only upper case? If thechar arr = "ABCDEF98"
– Embedded Geek
Jun 24 '17 at 12:41
add a comment |
Here is a little Arduino sketch illustrating one way to do this:
void setup() {
Serial.begin(9600);
char arr = "abcdef98";
byte out[4];
auto getNum = (char c){ return c > '9' ? c - 'a' + 10 : c - '0'; };
byte *ptr = out;
for(char *idx = arr ; *idx ; ++idx, ++ptr ){
*ptr = (getNum( *idx++ ) << 4) + getNum( *idx );
}
//Check converted byte values.
for( byte b : out )
Serial.println( b, HEX );
}
void loop() {
}
The loop will keep converting until it hits a null character. Also the code used in getNum
only deals with lower case values. If you need to parse uppercase values its an easy change. If you need to parse both then its only a little more code, I'll leave that for you if needed (let me know if you cannot work it out and need it).
This will output to the serial monitor the 4 byte values contained in out
after conversion.
AB
CD
EF
98
Edit: How to use different length inputs.
The loop does not care how much data there is, as long as there are an even number of inputs (two ascii chars for each byte of output) plus a single terminating null. It simply stops converting when it hits the input strings terminating null.
So to do a longer conversion in the sketch above, you only need to change the length of the output (to accommodate the longer number). I.e:
char arr = "abcdef9876543210";
byte out[8];
The 4
inside the loop doesn't change. It is shifting the first number into position.
For the first two inputs ("ab"
) the code first converts the 'a' to the number 10
, or hexidecimal A
. It then shifts it left 4 bits, so it resides in the upper four bits of the byte: 0A
to A0
. Then the second value B
is simply added to the number giving AB
.
Thank you for your comment. I'm trying to understand your code and edit it to the case of an array of 6 bytes. I edited the number '4' to 6 but it's not work. Can you explain a little bit further about your code?.
– Trung Phạm Khánh
Jun 3 '16 at 14:41
@TrungPhạmKhánh I have edited the end of the question with an explanation.
– Chris A
Jun 3 '16 at 19:22
How to parse only upper case? If thechar arr = "ABCDEF98"
– Embedded Geek
Jun 24 '17 at 12:41
add a comment |
Here is a little Arduino sketch illustrating one way to do this:
void setup() {
Serial.begin(9600);
char arr = "abcdef98";
byte out[4];
auto getNum = (char c){ return c > '9' ? c - 'a' + 10 : c - '0'; };
byte *ptr = out;
for(char *idx = arr ; *idx ; ++idx, ++ptr ){
*ptr = (getNum( *idx++ ) << 4) + getNum( *idx );
}
//Check converted byte values.
for( byte b : out )
Serial.println( b, HEX );
}
void loop() {
}
The loop will keep converting until it hits a null character. Also the code used in getNum
only deals with lower case values. If you need to parse uppercase values its an easy change. If you need to parse both then its only a little more code, I'll leave that for you if needed (let me know if you cannot work it out and need it).
This will output to the serial monitor the 4 byte values contained in out
after conversion.
AB
CD
EF
98
Edit: How to use different length inputs.
The loop does not care how much data there is, as long as there are an even number of inputs (two ascii chars for each byte of output) plus a single terminating null. It simply stops converting when it hits the input strings terminating null.
So to do a longer conversion in the sketch above, you only need to change the length of the output (to accommodate the longer number). I.e:
char arr = "abcdef9876543210";
byte out[8];
The 4
inside the loop doesn't change. It is shifting the first number into position.
For the first two inputs ("ab"
) the code first converts the 'a' to the number 10
, or hexidecimal A
. It then shifts it left 4 bits, so it resides in the upper four bits of the byte: 0A
to A0
. Then the second value B
is simply added to the number giving AB
.
Here is a little Arduino sketch illustrating one way to do this:
void setup() {
Serial.begin(9600);
char arr = "abcdef98";
byte out[4];
auto getNum = (char c){ return c > '9' ? c - 'a' + 10 : c - '0'; };
byte *ptr = out;
for(char *idx = arr ; *idx ; ++idx, ++ptr ){
*ptr = (getNum( *idx++ ) << 4) + getNum( *idx );
}
//Check converted byte values.
for( byte b : out )
Serial.println( b, HEX );
}
void loop() {
}
The loop will keep converting until it hits a null character. Also the code used in getNum
only deals with lower case values. If you need to parse uppercase values its an easy change. If you need to parse both then its only a little more code, I'll leave that for you if needed (let me know if you cannot work it out and need it).
This will output to the serial monitor the 4 byte values contained in out
after conversion.
AB
CD
EF
98
Edit: How to use different length inputs.
The loop does not care how much data there is, as long as there are an even number of inputs (two ascii chars for each byte of output) plus a single terminating null. It simply stops converting when it hits the input strings terminating null.
So to do a longer conversion in the sketch above, you only need to change the length of the output (to accommodate the longer number). I.e:
char arr = "abcdef9876543210";
byte out[8];
The 4
inside the loop doesn't change. It is shifting the first number into position.
For the first two inputs ("ab"
) the code first converts the 'a' to the number 10
, or hexidecimal A
. It then shifts it left 4 bits, so it resides in the upper four bits of the byte: 0A
to A0
. Then the second value B
is simply added to the number giving AB
.
edited Jun 5 '16 at 2:17
answered Jun 2 '16 at 8:08
Chris AChris A
1,28221015
1,28221015
Thank you for your comment. I'm trying to understand your code and edit it to the case of an array of 6 bytes. I edited the number '4' to 6 but it's not work. Can you explain a little bit further about your code?.
– Trung Phạm Khánh
Jun 3 '16 at 14:41
@TrungPhạmKhánh I have edited the end of the question with an explanation.
– Chris A
Jun 3 '16 at 19:22
How to parse only upper case? If thechar arr = "ABCDEF98"
– Embedded Geek
Jun 24 '17 at 12:41
add a comment |
Thank you for your comment. I'm trying to understand your code and edit it to the case of an array of 6 bytes. I edited the number '4' to 6 but it's not work. Can you explain a little bit further about your code?.
– Trung Phạm Khánh
Jun 3 '16 at 14:41
@TrungPhạmKhánh I have edited the end of the question with an explanation.
– Chris A
Jun 3 '16 at 19:22
How to parse only upper case? If thechar arr = "ABCDEF98"
– Embedded Geek
Jun 24 '17 at 12:41
Thank you for your comment. I'm trying to understand your code and edit it to the case of an array of 6 bytes. I edited the number '4' to 6 but it's not work. Can you explain a little bit further about your code?.
– Trung Phạm Khánh
Jun 3 '16 at 14:41
Thank you for your comment. I'm trying to understand your code and edit it to the case of an array of 6 bytes. I edited the number '4' to 6 but it's not work. Can you explain a little bit further about your code?.
– Trung Phạm Khánh
Jun 3 '16 at 14:41
@TrungPhạmKhánh I have edited the end of the question with an explanation.
– Chris A
Jun 3 '16 at 19:22
@TrungPhạmKhánh I have edited the end of the question with an explanation.
– Chris A
Jun 3 '16 at 19:22
How to parse only upper case? If the
char arr = "ABCDEF98"
– Embedded Geek
Jun 24 '17 at 12:41
How to parse only upper case? If the
char arr = "ABCDEF98"
– Embedded Geek
Jun 24 '17 at 12:41
add a comment |
Just shift 0 or 1 to its position in binary format :)
char lineChars[8] = {1,1,0,0,0,1,0,1};
char lineChar = 0;
for(int i=0; i<8;i++)
{
lineChar |= lineChars[i] << (7-i);
}
Example 2. But is not tested!
void abs()
{
char* charData = new char;
*charData = 'h';
BYTE* byteData = new BYTE;
*byteData = *(BYTE*)charData;
}
Can you explain a bit more what's going on in your code? I don't understand how this parses hex values.
– Karsten Koop
Jun 2 '16 at 7:21
add a comment |
Just shift 0 or 1 to its position in binary format :)
char lineChars[8] = {1,1,0,0,0,1,0,1};
char lineChar = 0;
for(int i=0; i<8;i++)
{
lineChar |= lineChars[i] << (7-i);
}
Example 2. But is not tested!
void abs()
{
char* charData = new char;
*charData = 'h';
BYTE* byteData = new BYTE;
*byteData = *(BYTE*)charData;
}
Can you explain a bit more what's going on in your code? I don't understand how this parses hex values.
– Karsten Koop
Jun 2 '16 at 7:21
add a comment |
Just shift 0 or 1 to its position in binary format :)
char lineChars[8] = {1,1,0,0,0,1,0,1};
char lineChar = 0;
for(int i=0; i<8;i++)
{
lineChar |= lineChars[i] << (7-i);
}
Example 2. But is not tested!
void abs()
{
char* charData = new char;
*charData = 'h';
BYTE* byteData = new BYTE;
*byteData = *(BYTE*)charData;
}
Just shift 0 or 1 to its position in binary format :)
char lineChars[8] = {1,1,0,0,0,1,0,1};
char lineChar = 0;
for(int i=0; i<8;i++)
{
lineChar |= lineChars[i] << (7-i);
}
Example 2. But is not tested!
void abs()
{
char* charData = new char;
*charData = 'h';
BYTE* byteData = new BYTE;
*byteData = *(BYTE*)charData;
}
answered Jun 2 '16 at 7:12
FortranFortran
489412
489412
Can you explain a bit more what's going on in your code? I don't understand how this parses hex values.
– Karsten Koop
Jun 2 '16 at 7:21
add a comment |
Can you explain a bit more what's going on in your code? I don't understand how this parses hex values.
– Karsten Koop
Jun 2 '16 at 7:21
Can you explain a bit more what's going on in your code? I don't understand how this parses hex values.
– Karsten Koop
Jun 2 '16 at 7:21
Can you explain a bit more what's going on in your code? I don't understand how this parses hex values.
– Karsten Koop
Jun 2 '16 at 7:21
add a comment |
Assuming you want to parse the hex values in your string, and two letters always make up one byte value (so you use leading zeros), you can use sscanf
like this:
char input = "fff2bdf1";
unsigned char output[4];
for (int i=0; i<4; i++) {
sscanf(&input[i*2], "%02xd", &data[i]);
}
Thank you for your support.
– Trung Phạm Khánh
Jun 3 '16 at 14:39
add a comment |
Assuming you want to parse the hex values in your string, and two letters always make up one byte value (so you use leading zeros), you can use sscanf
like this:
char input = "fff2bdf1";
unsigned char output[4];
for (int i=0; i<4; i++) {
sscanf(&input[i*2], "%02xd", &data[i]);
}
Thank you for your support.
– Trung Phạm Khánh
Jun 3 '16 at 14:39
add a comment |
Assuming you want to parse the hex values in your string, and two letters always make up one byte value (so you use leading zeros), you can use sscanf
like this:
char input = "fff2bdf1";
unsigned char output[4];
for (int i=0; i<4; i++) {
sscanf(&input[i*2], "%02xd", &data[i]);
}
Assuming you want to parse the hex values in your string, and two letters always make up one byte value (so you use leading zeros), you can use sscanf
like this:
char input = "fff2bdf1";
unsigned char output[4];
for (int i=0; i<4; i++) {
sscanf(&input[i*2], "%02xd", &data[i]);
}
answered Jun 2 '16 at 7:30
Karsten KoopKarsten Koop
2,07611420
2,07611420
Thank you for your support.
– Trung Phạm Khánh
Jun 3 '16 at 14:39
add a comment |
Thank you for your support.
– Trung Phạm Khánh
Jun 3 '16 at 14:39
Thank you for your support.
– Trung Phạm Khánh
Jun 3 '16 at 14:39
Thank you for your support.
– Trung Phạm Khánh
Jun 3 '16 at 14:39
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37581925%2fhow-to-convert-a-char-array-to-a-byte-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
So you want to parse the hex values?
– Karsten Koop
Jun 2 '16 at 7:20