Using int sprintf()

Ok, I'm stuck. I've read about 5 tutorials on how to use int sprintf() to create an int. I've tried several flags to copy the 4 positions of my byte variable (%i, %c, %%) but it's not transferring to my int. I need the value of int MachAdd to equal "0x01" from the sprintf(). I assumed I needed the "%c" in the second position because of the 'x' in the HEX number.

The assistance is greatly appreciated.

This is the last int sprintf() I've tried:

byte MachAddX[4];
int MachAdd = 0x01;
byte sa[1];

setup()
{

sprintf( MachAddX, "0x%02X", sa[0]);  <--The value of sa[0] is "1" from a previous process.
  delay(10);
  int sprintf( MachAdd, "%1u%1c%1u%1u", MachAddX[0], MachAddX[1], MachAddX[2], MachAddX[3]);
  Serial.write( MachAddX[0]); Serial.write(MachAddX[1]); Serial.write(MachAddX[2]); Serial.write(MachAddX[3]);

  delay(50);
  Serial.print(MachAdd);
  delay(200);
}

The outputs I get are:
MachAddX = 0x01; <---The correct value I need
MachAdd = 0; <---This should also be "0x01"

The "int sprintf()" line makes no sense at all and gets several compile warnings. I don't think it does anything.

Of course when you Serial.print() an integer variable containing 1 it shows up as 1.

The output of sprintf(MachAddX, "0x%02X", sa[0]); takes up SIX characters, not four. You need to allow for the null terminator character.

char MachAddX[6];
int MachAdd = 0x01;
byte sa[1] = {1};


void setup()
{
  Serial.begin(115200);
  
  sprintf(MachAddX, "0x%02X", sa[0]);  // < --The value of sa[0] is "1" from a previous process.
  Serial.println(MachAddX);


  // This line makes no sense at all:
  // int sprintf( MachAdd, "%1u%1c%1u%1u", MachAddX[0], MachAddX[1], MachAddX[2], MachAddX[3]);


  Serial.println(MachAdd);
}


void loop () {}

how to use int sprintf() to create an int.

I can't figure out what you're trying to do, but one does NOT normaly use sprintf() to "create" an int; it would be more typical to use it to turn an into into something else...

westfw:
I can't figure out what you're trying to do, but one does NOT normaly use sprintf() to "create" an int; it would be more typical to use it to turn an into into something else...

I'm trying to create an int that has to be identical to a HEX that is used when creating a byte string of HEX's of different byte lengths (2, 3, 9, 15, or etc) that are sent. That's how I stumbled across the 'int sprintf()' because the different C websites stated that an int could be created this way from any argument or groups of arguments.

johnwasser:
The "int sprintf()" line makes no sense at all and gets several compile warnings. I don't think it does anything.

Of course when you Serial.print() an integer variable containing 1 it shows up as 1.

The output of sprintf(MachAddX, "0x%02X", sa[0]); takes up SIX characters, not four. You need to allow for the null terminator character.

I changed MachAddX to [6] and still get the same undesired results. :frowning:

  // This line makes no sense at all:

// int sprintf( MachAdd, "%1u%1c%1u%1u", MachAddX[0], MachAddX[1], MachAddX[2], MachAddX[3]);

I was going by the different websites that stated this is the structure of int sprintf(). But even by their tutelage, it's still not working.

@peasoup, you are still far away from something working.
Can you tell more about the hex numbers that you want to convert. Where do they come from ? Can you give a few examples how they look, and so on.

sscanf ?
http://www.cplusplus.com/reference/cstdio/sscanf/

When you read something on a website, can you give a link to it ? We don't know such websites.

The return value of sprintf() can be used to test if the conversion was successful, or to determine the size of the resulting text.

I'm trying to create an int that has to be identical to a HEX that is used when creating a byte string of HEX's of different byte lengths (2, 3, 9, 15, or etc) that are sent.

I'm still not getting it:
You're receiving a byte sequence like "12" or "345" or "6789ABCDEF", or "102030405060708" ?
and you want to get the integer values instead of strings? "12" and "345" can work (except - are you expecting 0x0012 and 0x0234 or 0x1200 and 0x2340, or something else?), but the maximum numbers of hex digits you can fit in an "int" on arduino is 4, and the max you can fit in a long is 8, so I don't know what you expect to happen with your 9 and 15 byte strings.

westfw:
I'm still not getting it:
You're receiving a byte sequence like "12" or "345" or "6789ABCDEF", or "102030405060708" ?
and you want to get the integer values instead of strings? "12" and "345" can work (except - are you expecting 0x0012 and 0x0234 or 0x1200 and 0x2340, or something else?), but the maximum numbers of hex digits you can fit in an "int" on arduino is 4, and the max you can fit in a long is 8, so I don't know what you expect to happen with your 9 and 15 byte strings.

No, I am creating a HEX value (0x01) from the line where sa[0] can be the value of 1-127 (01-7F):

sprintf(MachAddX, "0x%02X", sa[0])

I need that HEX value put into the integer "MachAdd" and all I'm getting is "0". I need that integer to be the FOUR digits of "0x" plus the 2 digit HEX value.

The 4 digit int I'm creating is only 1 part of the 9 or 15 bytes that will be sent.

In this example, byte RTEEnbl[3] = {MachAdd, 0x0E, 0x01}, MachAdd has to be an integer.

Koepel:
@peasoup, you are still far away from something working.
Can you tell more about the hex numbers that you want to convert. Where do they come from ? Can you give a few examples how they look, and so on.

sscanf ?
http://www.cplusplus.com/reference/cstdio/sscanf/

When you read something on a website, can you give a link to it ? We don't know such websites.

The return value of sprintf() can be used to test if the conversion was successful, or to determine the size of the resulting text.

Just tried sscanf() with same results. Thanks for pointing that one out. Here's two of the websites:

http://www.cplusplus.com/reference/cstdio/sprintf/

The sprintf family fills a character array; it does not create an int.

From the first link above

Return Value
If successful, the total number of characters written is returned excluding the null-character appended at the end of the string, otherwise a negative number is returned in case of failure.

So it returns the length of the string that is created.

I need that HEX value put into the integer "MachAdd" and all I'm getting is "0". I need that integer to be the FOUR digits of "0x" plus the 2 digit HEX value.

"0x12" is not an "integer", it's a string. (and "x" isn't a digit, either.)

void setup() {
  Serial.begin(9600);
  while (!Serial)
    ;
}
char outputBuffer[20];   // big enough for lots of digits...
byte inputBytes[8] = {9, 15, 31, 127, 0xDE, 0xAD, 0xBE, 0xEF};

void loop() {
  // Convert one byte
  sprintf(outputBuffer,  // char array to put the output string.
              "0x"       //"0x" is just string, copied to output
              "%02x",    // "%02x" is "two digits of number, in hex, with leading zeros
          inputBytes[0]);  // value to output
  Serial.println(outputBuffer);

  // Convert three bytes
  sprintf(outputBuffer, "0x%02x%02x%02x",     // "%02x" is repeated, for 3 inputs
       inputBytes[0], inputBytes[1], inputBytes[2]);  // three values provided
  Serial.println(outputBuffer);

  // Convert all the bytes, one at a time, in a loop
  sprintf(outputBuffer, "0x");  // output prefix
  char *p = &outputBuffer[2];   // pointer to 3rd char in otuput
  for (byte i = 0; i < sizeof(inputBytes); i++) { // loop over inputBytes
    sprintf(p, "%02x", inputBytes[i]);  // print two digits of hex
    p += 2;  // advance pointer 2 chatracters.
  }
  Serial.println(outputBuffer);

  delay(5000);
}

Hi Peasoup,

integer-variables are numbers.

int MachAdd = 0x01;  // assign number "1"  given as hexadecimal number

does assign the exact same value to variable MachhAdd as

int MachAdd = 1; // assign number "1" given as decimal number

does assign the exact same value to variable MachhAdd as

int MachAdd = B00000001; // assign number "1" given as binary number with 8 digits

So it all depends on what do you want to do next with the value stored into your variable "MachhAdd" ????

From your line

Serial.write( MachAddX[0]); Serial.write(MachAddX[1]); Serial.write(MachAddX[2]); Serial.write(MachAddX[3]);

I guess you want four bytes be written as hexadecimal numbers
the output should look like that

0x1A0xC200x5C0xB7

so as you have some misconceptions about programming. Please describe in normal words what you want to do with the content of variable MachAdd and give an example how the output should look like

As you are using Serial.write() Serial.write does outputting an 8 bit-value a single byte.

If you really need the character-sequence "0", "x", "0", "1"
described in words
"0" digit zero
"x" letter x
"0" digit zero
"1" digit one

This is four bytes and you can't use Serial.write at all
Or if you would like to use it it must be something like

Serial.write('0');
Serial.write('x');
Serial.write('0');
Serial.write('1');

But we are talking about some details. What is the main purpose of this all?

Do you have a certain byte-sequence that must be sended to another device
and he manual describes them as hexadecimal values?

Do the four bytes just have to be printed to the serial monitor as hexadecimal values?

please describe the whole thing.

best regards Stefan

peasoup:

Quote from: peasoupIn this example, MachAdd has to be an integer.

byte RTEEnbl[3] = {MachAdd, 0x0E, 0x01};

Variables are just collections of bits. Hexadecimal is just a way to present a collection of bits to a human. When the bits are in memory they are not 'hex' or 'decimal', they are just bits. If you want to take a value from the 'sa' array and put it in the 'RTEEnbl' array, 'hex' is not a thing.

byte sa[1] = {1};
char HexBuffer[10];
void setup()
{
  Serial.begin(115200);
  for (byte test = 0; test < 255; test += 15)
  {
    Serial.print("test=");
    Serial.print(test);
    Serial.print("  RTEEnbl=");
    
    sa[0] = test;
    
    byte MachAdd = sa[0];
    byte RTEEnbl[3] = {MachAdd, 0x0E, 0x01};
    for (int i = 0; i < 3; i++)
    {
      sprintf(HexBuffer, "0x%02X ", RTEEnbl[i]);  // < --The value of sa[0] is "1" from a previous process.
      Serial.print(HexBuffer);
    }
    Serial.println();
  }
}
void loop () {}

Output:

test=0  RTEEnbl=0x00 0x0E 0x01 
test=15  RTEEnbl=0x0F 0x0E 0x01 
test=30  RTEEnbl=0x1E 0x0E 0x01 
test=45  RTEEnbl=0x2D 0x0E 0x01 
test=60  RTEEnbl=0x3C 0x0E 0x01 
test=75  RTEEnbl=0x4B 0x0E 0x01 
test=90  RTEEnbl=0x5A 0x0E 0x01 
test=105  RTEEnbl=0x69 0x0E 0x01 
test=120  RTEEnbl=0x78 0x0E 0x01 
test=135  RTEEnbl=0x87 0x0E 0x01 
test=150  RTEEnbl=0x96 0x0E 0x01 
test=165  RTEEnbl=0xA5 0x0E 0x01 
test=180  RTEEnbl=0xB4 0x0E 0x01 
test=195  RTEEnbl=0xC3 0x0E 0x01 
test=210  RTEEnbl=0xD2 0x0E 0x01 
test=225  RTEEnbl=0xE1 0x0E 0x01 
test=240  RTEEnbl=0xF0 0x0E 0x01

johnwasser:
Variables are just collections of bits. Hexadecimal is just a way to present a collection of bits to a human. When the bits are in memory they are not 'hex' or 'decimal', they are just bits. If you want to take a value from the 'sa' array and put it in the 'RTEEnbl' array, 'hex' is not a thing.

John,
I appreciate the time taken to help me with this dilemma, but I can't seem to get this to work the way I thought it would. Perhaps I was being over-zealous to make this program very customizable. I'm struggling with another task (html button to call a void function()) and decided to place this on hold until I can get the final product working, then go back and put in customizable features.

Thanks,
Steve

Hi pea soup,
I haven’t read all the to-ing and fro-ing above, but I did see this line...

I need that HEX value put into the integer "MachAdd"

The value list is already in sa[], hex or otherwise, they’re all just numbers.
So your sprintf() of sa[n] does what you want.

bufindex = 0;
saindex = 0;
// You can keep the ‘return value’ from sprintf()  
bufindex += sprintf(buffer+ bufindex, “02x_“, sa[saindex++]);
// - and for all subsequent values of sa[]
bufindex += sprintf(buffer+ bufindex,  “02x_“, sa[saindex++]) // tacked onto the end, as many times as you want.

I'm struggling with another task (html button to call a void function()) and decided to place this on hold until I can get the final product working, then go back and put in customizable features.

some people seem to need the big fail before they understand: learn the basics.
Even a beginner that has worked through just three chapters of any tutorial would not talk of "void functions"
best regards Stefan