# Combining two sentences

**URL:** <https://forum.arduino.cc/t/combining-two-sentences/1037288>\
**Category:** Programming\
**Created:** [October 1, 2022, 5:22am UTC](https://forum.arduino.cc/t/combining-two-sentences/1037288 "2022-10-01T05:22:20Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![darkmour](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@darkmour](https://forum.arduino.cc/u/darkmour)\
**Post date:** [October 1, 2022, 5:22am UTC](https://forum.arduino.cc/t/combining-two-sentences/1037288/1 "2022-10-01T05:22:20Z")

</div>

How can I print these two sentences as a compound sentence in serial print?

1.sentence = 0x  
2.sentence = F740BF

Serial.println("0x"+F740BF); I tried this but it didn't work

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [October 1, 2022, 5:31am UTC](https://forum.arduino.cc/t/combining-two-sentences/1037288/2 "2022-10-01T05:31:49Z")

</div>

```cpp
Serial.print("0x");
Serial.println("F740BF");

```

---

<div class="post-metadata">

**Author:** ![darkmour](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@darkmour](https://forum.arduino.cc/u/darkmour)\
**Post date:** [October 1, 2022, 5:38am UTC](https://forum.arduino.cc/t/combining-two-sentences/1037288/3 "2022-10-01T05:38:02Z")

</div>

hi, i need the join code as i will put it in a variable

sample

veri = 0x+F740BF;

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [October 1, 2022, 5:41am UTC](https://forum.arduino.cc/t/combining-two-sentences/1037288/4 "2022-10-01T05:41:24Z")

</div>

Try then

```cpp
unsigned long veri = 0xF740BF;
Serial.print("0x");
Serial.println(veri, HEX);

```

---

<div class="post-metadata">

**Author:** ![darkmour](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@darkmour](https://forum.arduino.cc/u/darkmour)\
**Post date:** [October 1, 2022, 5:44am UTC](https://forum.arduino.cc/t/combining-two-sentences/1037288/5 "2022-10-01T05:44:14Z")

</div>

code= irsend.sendNEC(0xF740BF, 32); i am using this code. I'm sending the data to the part that says 0xF740BF, but it should be 0x at the beginning

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [October 1, 2022, 5:48am UTC](https://forum.arduino.cc/t/combining-two-sentences/1037288/6 "2022-10-01T05:48:46Z")

</div>

Do you have a number or a string?  
I don’t understand what irsend.sendNEC() has to do with your printing question. Can you clarify exactly the need and context?

---

<div class="post-metadata">

**Author:** ![darkmour](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@darkmour](https://forum.arduino.cc/u/darkmour)\
**Post date:** [October 1, 2022, 5:52am UTC](https://forum.arduino.cc/t/combining-two-sentences/1037288/7 "2022-10-01T05:52:44Z")

</div>

Incoming data in ir receiver =F740BF this data irsend.sendNEC(HERE,32); I'm posting it here, but there should be a 0x at the beginning, is there a code that can combine the two sentences?

---

<div class="post-metadata">

**Author:** ![markd833](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/markd833/32/1186928_2.png) [@markd833](https://forum.arduino.cc/u/markd833)\
**Post date:** [October 1, 2022, 5:55am UTC](https://forum.arduino.cc/t/combining-two-sentences/1037288/8 "2022-10-01T05:55:12Z")

</div>

> [@darkmour](#):
>
> there should be a 0x at the beginning

Why do you think this? The "0x" is indicating that what follows is a number expressed as a hexadecimal value.

---

<div class="post-metadata">

**Author:** ![darkmour](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@darkmour](https://forum.arduino.cc/u/darkmour)\
**Post date:** [October 1, 2022, 5:59am UTC](https://forum.arduino.cc/t/combining-two-sentences/1037288/9 "2022-10-01T05:59:18Z")

</div>

this is ir send code. should be 0x at the beginning

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [October 1, 2022, 6:02am UTC](https://forum.arduino.cc/t/combining-two-sentences/1037288/10 "2022-10-01T06:02:55Z")

</div>

Post your current sketch to see how we can help.  
My crystalball is in the dishwasher.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [October 1, 2022, 6:03am UTC](https://forum.arduino.cc/t/combining-two-sentences/1037288/11 "2022-10-01T06:03:03Z")

</div>

If variable `veri` holds your number you do

```cpp

unsigned long veri = 0xF740BF;
Serial.print("0x");
Serial.println(veri, HEX);

irsend.sendNEC(veri, 32);

```

The 0x is just a notation to say that the number literal that follows is expressed in hexadecimal. You probably misunderstand this. When the number is in a variable, then you don’t worry about the base (it’s in binary in memory).

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [October 1, 2022, 12:46pm UTC](https://forum.arduino.cc/t/combining-two-sentences/1037288/12 "2022-10-01T12:46:37Z")

</div>

Try this:

```cpp
  Serial.begin(9600);
  String myString = "0x";
  Serial.println(myString+"F740BF"); ///shows: 0xF740BF

```

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [October 1, 2022, 2:30pm UTC](https://forum.arduino.cc/t/combining-two-sentences/1037288/13 "2022-10-01T14:30:50Z")

</div>

What a waste of resources 🙂 but it does the job !

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [October 1, 2022, 2:38pm UTC](https://forum.arduino.cc/t/combining-two-sentences/1037288/14 "2022-10-01T14:38:29Z")

</div>

> [@darkmour](#):
>
> code= irsend.sendNEC(0xF740BF, 32); i am using this code.

That is your **code** , which is compiled on a PC. It has nothing at all to do with the serial port.

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [October 1, 2022, 3:35pm UTC](https://forum.arduino.cc/t/combining-two-sentences/1037288/28 "2022-10-01T15:35:35Z")

</div>

> [@darkmour](#):
>
> this is ir send code. should be 0x at the beginning

no, you don't need any 0x at the beginning for IR sending. You are confusing text and numeric representation of a variable

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [October 2, 2022, 2:30am UTC](https://forum.arduino.cc/t/combining-two-sentences/1037288/29 "2022-10-02T02:30:44Z")

</div>

> [@J-M-L](#):
>
> What a waste of resources 🙂 but it does the job !

Codes of post #12 could be inefficient, but it does not cause wastage of resources as these codes require some storage area.

> [@J-M-L](#):
>
> `Serial.println("F(F740BF");)`

?

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [October 2, 2022, 5:22am UTC](https://forum.arduino.cc/t/combining-two-sentences/1037288/30 "2022-10-02T05:22:09Z")

</div>

@GolamMostafa

> Codes of post #12 could be inefficient, but it does not cause wastage of resources as these codes require some storage area

I let you judge the compared costs and you then come back with your opinion

When I do

```arduino
Serial.println("F(F740BF"));

```

The compiler allocates the cStrng in flash memory (guaranteed to have room or the compilation would fail) and at run time this flash memory address (pointer) is passed to the println() method which goes and reads bytes one by one from flash memory and write() them out. 1 copy of the text in flash is used and a 2 bytes pointer on the stack for the println() call.

Compare now with your code

When you do this

> [@GolamMostafa](#):
>
> ```arduino
> String myString = "0x";
> Serial.println(myString+"F740BF"); 
> 
> ```

It goes somewhat like this:

You have flash storage for the 2 string literals then at run time stack memory is used to instantiate myString and heap memory is allocated for the 3 bytes of "0x" then “0x" is copied over from flash to the heap location.

Then you perform a concatenation with + for the cString "F740BF" which is copied over from flash to the stack and promoted to a String instance so stack and heap memory allocated again for "F740BF" which is copied over from the stack to the heap in this transitory String. Then this transitory instance is passed as parameter to the overloaded + operator which is called and it starts by calculating the length of that string (a call to the length method) then a new transitory String is allocated on the stack and the heap , large enough to fit the concatenated Strings (some maths done to find that size) and "0x" is copied over to this new location and the "F740BF" is also copied over. Note that all these heap allocations are run time decisions that are costly operation as the heap needs to maintains the list of free blocks.

At that point we have a copy of "F740BF" in Flash, on the stack, in the first transitory String heap memory and in the new concatenation transitory String… 4 copies of the same bytes…

Then, if all the calls for memory allocation did not silently fail because you were running out of space - which can happen and Murphy says it will - the println method is called on that transitory String which extracts the pointer to the storage of the cString on the heap and the println() method goes and reads bytes one by one from SRAM memory and write() them out.

But wait that’s not all… now the 2 temporary Strings need clean up and the destructor method is called which returns memory to the heap - again a costly operation as the heap needs to maintains the list of free blocks.

As your function exits myString will also need to be destructed and so another call to heap functions will happen…

——

Now that you might have a better understanding of what is at play when you abuse the String class and use the + operator, what do you think? Does my claim that this is a waste of resources makes more sense??

(Was the same reason I told @Best_codes it was not so good)

* * *

Edit/ I see you removed your comment. I keep that answer as it might help newcomers understand the hidden cost of this String class and concatenation..

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [October 2, 2022, 5:47am UTC](https://forum.arduino.cc/t/combining-two-sentences/1037288/31 "2022-10-02T05:47:30Z")

</div>

I have worked with an Oil Company (Schlumberger) who recruits both Geologists and Engineers; where, a Geologist interprets log data in the Computer Center by virtue of his academic excellence and an Engineer interprets log data (for real time quality control) in the Oil Field by virtue of his training excellence. It is the Geologist whose interpretation is always better.

Myself, being an Electrical Engineer who has learnt programming mainly through training/practice while testing the target hardware, does feel proud of reading the details of ("what is being happened in the background when **String** data type is used") offered by a Software Engineer/Programmer who is seasoned by virtue of his academic excellence.

> [@J-M-L](#):
>
> Edit/ I see you removed your comment. I keep that answer as it might help newcomers understand the hidden cost of this String class and concatenation..

I have put back my comments of post #29.

---

<div class="post-metadata">

**Author:** ![Best\_codes](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/best_codes/32/1020839_2.png) [@Best\_codes](https://forum.arduino.cc/u/Best_codes)\
**Post date:** [October 2, 2022, 2:43pm UTC](https://forum.arduino.cc/t/combining-two-sentences/1037288/32 "2022-10-02T14:43:21Z")

</div>

> [@J-M-L](#):
>
> `Serial.println("F(F740BF"));`

I think that code kindof defeats the purpose of what this guy wants, @J-M-L .

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [October 2, 2022, 3:43pm UTC](https://forum.arduino.cc/t/combining-two-sentences/1037288/33 "2022-10-02T15:43:01Z")

</div>

Yes, that’s why the right answer with a variable is

```cpp
unsigned long veri = 0xF740BF;
Serial.printF(("0x"));
Serial.println(veri, HEX);

```

And not the “waste” suggested by @GolamMostafa that OP seems to like… but hey, it is his program;)

Note that it turned out as well that the whole thing was a total misunderstanding of the difference between the value in memory and it’s screen representation in a given base…

[Next page](https://forum.arduino.cc/t/combining-two-sentences/1037288.md?page=2)
