How to use client.read() to read string between two characters? - wifi

In my ESP8266 WiFi project I'm getting characters from a website through a GET request. The current code is this:
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
To get a string between particular characters, how do I edit this?

Put the code snip after read string operation and change the below divider delimeters with yours and gatheredStr will be your desired string:
char firstDivider = 'X';
char secondDivider = 'Y';
int firstDividerIndex = line.indexOf(firstDivider);
int secondDividerIndex = line.indexOf(secondDivider);
String gatheredStr = line.substring(firstDividerIndex, secondDividerIndex);

Related

How to convert a unicode hex string into its ASCII equivalent

I hope everything is going well.
I have this unicodestring:
353135313531353135313531
And I want to transform it into another unicodestring with this content:
515151515151
In other words, convert a hex representation into its ASCII interpretation.
It is very straightforward to do this in C, but the idea is to work with C++ Builder.
This is what I have been trying to do:
String hex_to_ascii(const String& hex_str) {
String ascii_str = "";
for (int i = 1; i <= hex_str.Length(); i += 2) {
String hex_char = hex_str.SubString(i, 2);
int ascii_char = hex_char.ToInt();
// ascii_str += String().sprintf(_D("%c"), ascii_char);
ascii_str.Insert(ascii_char, ascii_str.Length() + 1);
}
return ascii_str;
But no luck so far.
I know there is a method called ToHex I've been trying to search for documentation about it because it's related to what I am trying to do, so probably the library that has this method has also something close to what I need.
If you know how to do this or where can I read about the ToHex method, please let me know. Thank you for reading.
The code you have is very close, it just needs some minor tweaks.
Most importantly, String::ToInt() WILL NOT decode hex, like you are expecting. It will convert "35" to an integer with a value of decimal 35 (NOT hex 0x35, decimal 53), and will convert "31" to an integer with a value of decimal 31 (NOT hex 0x31, decimal 49), etc.
You need to instead use Sysutils::StrToInt() with a 0x hex prefix prepended to the string value.
Try this:
String hex_to_ascii(const String& hex_str) {
String ascii_str;
for (int i = 1; i <= hex_str.Length(); i += 2) {
String hex_char = _D("0x") + hex_str.SubString(i, 2);
int ascii_char = StrToInt(hex_char);
ascii_str += static_cast<Char>(ascii_char);
}
return ascii_str;
}
Alternatively, you can use HexToBin() to decode the hex into a byte array, and then construct a UnicodeString from those bytes, eg:
String hex_to_ascii(const String& hex_str) {
TBytes bytes;
bytes.Length = hex_str.Length() / 2;
HexToBin(hex_str.c_str(), &bytes[0], bytes.Length);
return String((char*)&bytes[0], bytes.Length);
// Alternatively:
// return TEncoding::Default.GetString(bytes);
}

How to remove a character from string and concatinate it in Dart | Flutter

I have two variables
String firstInput = "1.1.5";
String secondInput = "1.1.6";
From this I want the output firstOutput = 115 secondOutput = 116
How to remove dots from the string and concatenate remains as one variable ?
You can use the replaceAll method.
It would look like String out = firstInput.replaceAll(".","");
You can also use replaceAllwith RE as shown below
void main(){
final myString = '1.3.4.6.6';
String withoutDots = myString.replaceAll(RegExp('\\.'), ''); "Here \\ is used to as esc char"
print(withoutDots); // prints 13466
}

Flutter and Dart - how can I format an integer date in one line of code

How can I format an integer such as "20190331" to output as "2019-03-31" without firstly converting it to a date and without splitting it using substring. The date is stored as an integer in SQLite (SQFlite). I would like to do it in 1 line such as (pseudo code) Note: integer :
(pseudo) String sDate = fmt("####'-'##'-'##", map["DueDate"]);
I know that I could do it such as :
String sDate = map["DueDate"].toString();
sDate = sDate.substring(0,4)+'-'+sDate.substring(4,6)+'-'+sDate.substring(6,8);
That however is two lines of code, and Visual Studio Code turns it into 8 lines when formatted and I like to keep my code compact.
Write a function called fmt and call it as in your pseudo code.
String fmt(String f, int i) {
StringBuffer sb = StringBuffer();
RuneIterator format = RuneIterator(f);
RuneIterator input = RuneIterator(i.toString());
while (format.moveNext()) {
var currentAsString = format.currentAsString;
if (currentAsString == '#') {
input.moveNext();
sb.write(input.currentAsString);
} else {
sb.write(currentAsString);
}
}
return sb.toString();
}
one line:
print(fmt('####-##-##', 20190331));

Writing a string to disk without any character conversion in Groovy

I have a String object, now because it comes a diff of a folder containing different file types, not everything in it is encoded in the same character set.
The correct codes are in the string, but whenever I try to access the string, groovy tries to be helpful and decode the string, which messes things up.
Now the following seems to do what I need
String decoded_diff = "String that contains codes from different character encodings"
patch_file_name = 'changes.patch'
patch_file = new File(pwd(), patch_file_name)
patch_file.delete()
max_block_size = 1024 * 1024
char[] char_buffer = new char[max_block_size]
block_start = 0
patch_length = decoded_diff.length()
while (true) {
block_size = Math.min(patch_length - block_start, max_block_size)
decoded_diff.getChars(block_start, block_start + block_size, char_buffer, 0)
block_start += block_size
byte[] byte_buffer = new byte[block_size]
for (int i = 0; i < block_size; i++) {
byte_buffer[i] = (int) char_buffer[i]
}
patch_file.append(byte_buffer)
if (block_start == patch_length) break
}
However, it is sloooow
Is there a faster way to achieve the same thing? The final patch file must be identical to the original diff to work. Unfortunately I can't send the file itself (jenkins currently doesn't support file parameters in pipeline jobs) so I have to escape it and send it as part of a json parameter list, hence this painful rigmarole on the receiving end.
Why not:
String decoded_diff = "String that contains codes from different character encodings"
patch_file_name = 'changes.patch'
patch_file = new File(pwd(), patch_file_name)
patch_file.delete()
patchFile.withOutputStream { os ->
os << decoded_diff.bytes
}

Extract More than 3 Words After the First Word

In the app I'm working on, I need to extract the first word from a String and put it into another String and the rest of the words in yet another String. I was able to extract the first word using:
String pString = "KOF0000094 Implementation:ListingRequest:User FO-Partner"
int spacePos3 = pString.indexOf(" ");
String pFirstWord = pString.substring(0,spacePos3);
Result : KOF0000094
Now I want the "Implementation:ListingRequest:User FO-Partner" to put
in another String.
Thanks for your help in advance
Simplest solution with what you already have.
String restOfString = pString.substring(spacePos3+1)
String pSecondWord = pString.substring(spacePos3 + 1);
String whole = "KOF0000094 Implementation:ListingRequest:User FO-Partner";
String firstWord = "";
String restOfWords = "";
int spacesIndex = whole.indexOf(" ", 0);
restOfWords = whole.substring(spacesIndex, whole.length());
restOfWords = restOfWords.trim();
firstWord = whole.substring(0, spacesIndex);
firstWord = firstWord.trim();
This is simple string parsing... just find the first index of the first space... i.e. in a for loop...
if(string.charAt(i) == Characters.SPACE)
indexOfSpace = i;
Then your first word will be
String part1 = string.substring(0,indexOfSpace);
and the second string will be
String part2 = string.substring(indexOfSpace + 1);
Try using another call to substring(). What is the index of the first character in the string you want? What is the index of the last character?
You already have the index of the first space, which marks the end of the first word, so all you need to do is take the substring from the index immediately after that (so you don't include the space itself) to the end of the string.
You're probably better of using the split function
It would look something like this:
String pString = "KOF0000094 Implementation:ListingRequest:User FO-Partner";
String[] parts = pString.split(" ");
String partone = parts[0];
String partwo = parts[1] + " " +parts[2];
Or something similar, if there are going to be more spaces in the part following the first word you could use a loop or something similar;
You could use split, for instance...
static String pString = "KOF0000094 Implementation:ListingRequest:User FO-Partner";
static String[] pFirstWord = pString.split(" ");
/**
* #param args
*/
public static void main(String[] args) {
for(String word : pFirstWord) {
System.out.println(word);
}
}
This returned...
KOF0000094
Implementation:ListingRequest:User
FO-Partner
So the last string would be pFirstWord[1] + pFirstWord[2]
String class has a split method: http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#split(java.lang.String)
Use that if you want to get all the words.
EDIT: as mentioned in the comments, String.split is not supported in BB Java.
Or this if you just want the first word and the rest of the string:
int index=yourstring.indexOf(" ");
String firstWord = yourstring.substring(0,index);
String rest = yourstring.substring(index+1);

Resources