how could I convert a string in binary to string again in react native?
Ex: 01010 to Hello
I have the code to convert string to binary
Ex: text.split('').map(l => l.charCodeAt(0).toString(2)).join(' '),
let txt="Hello".split('').map(l => l.charCodeAt(0).toString(2)).join(' ')
let s = txt.split(" ").map(w=> String.fromCharCode(parseInt(w,2)))
console.log(s.join(""))
Just convert binary string back to integer and map those values to character using fromCharCode
To convert binary string to sting you can use parseInt and String.fromCharCode
parseInt converts strings to number. First argument is string value and the second argument is the base value in which the string representation is.
let num = parseInt(binaryStr, 2);
String.fromCharCode converts character code to matching string.
let str = String.fromCharCode(65);
Complete code with example:
const text = 'Hello'
let binaryStr = text.split('').map(l => l.charCodeAt(0).toString(2)).join(' ');
console.log('Binary string:', binaryStr);
//To convert binary to string
let strVal = binaryStr.split(' ').map(l => String.fromCharCode(parseInt(l, 2))).join('');
console.log('String:', strVal);
Output of above code:
Binary string: 1001000 1100101 1101100 1101100 1101111
String: Hello
Related
I have a code.
Double value = 6.589715E7;
DecimalFormatSymbols symbol = new DecimalFormatSymbols(Locale.GERMANY);
symbol.setGroupingSeparator(',');
final DecimalFormat doris = new DecimalFormat("#,###000",symbol );
System.out.println(changedValue);
which displays the value like this rite now.
65,897150
is there any way out this can be displayed as.
65,9 (also rounding off to one decimal place)
This example rounds up 2 but you get the point, try this
inputValue = Math.Round(inputValue, 2);
Here I put answer in string format. you can also convert into double format.
Double value = 6.589715E7;
DecimalFormatSymbols symbol = new DecimalFormatSymbols(Locale.GERMANY);
symbol.setGroupingSeparator(',');
final DecimalFormat doris = new DecimalFormat("##,###000",symbol );
String str = String.valueOf(doris.format(value));
String formated=str.replace(".", ",").substring(0,str.indexOf(",")+2);
Log.e("Log","d result="+formated);
I recife a JSON with some values for example 399.00&euro but I need to convert the text &euro to € in this example. But when I receive something else like 399.00&USD I have to convert the text to $.
How can I convert the text after the number automatically?
you can use replaceAll() if you are reciving an String as response and them convert to JSONObject:
String responseAux= response.replaceAll("&euro","€");
Or do it when you get the String of the json object using replace():
String value= jsonObject.getString(Constants.JSON_NAME).replace("&euro","€");
Try this:
/**
*#param {String} separator e.g: &
*#param {Array} symbols e.g: [{key:'euro', value:'€'}, {key:'USD', value:'$'}]
*#param {String} string to inspection
*#return {String}
/**
var convertCurrency=function(separator, symbols, value){
if(_.isArray(symbols) && !_.isEmpty(symbols)){
for(var i=0;i<symbols.length;i++){
var symbol=symbols[i];
var replace=separator.concat(symbol.key);
value=value.replaceAll(replace, symbol.value);
}
}
return value;
};
I have text below,
{\rtf1\ansi\ansicpg1252\cocoartf1138\cocoasubrtf510
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\pard\
\f0\fs24 \cf0 \'83}
In the above text \'83 corresponds to RTF file format and is in NSWindowsCP1252StringEncoding. Now my problem is how to convert to actual representation in
83 corresponds to string ƒ in NSWindowsCP1252StringEncoding.
NSString string with the encoding say using the api
stringWithCString:encoding. I have called the api like below.
NSString* str = [NSString stringWithCString:#"\'83" encoding:NSWindowsCP1252StringEncoding];
but it does not give the text. please let me know how to convert the value to particular text.
Regards,
Lenin
#"\'83" is not a c string, so you shouldn't be passing it to stringWithCString:encoding:. Even a C string version, "\'83", is just ascii characters so using the encoding NSWdindowsCP1252StringEncoding will not produce a string with any special characters.
Instead you need a c string with non-ascii values in order for NSWindowsCP1252StringEncoding to result in a non-ascii character like 'ƒ'.
NSString *str = [NSString stringWithCString:"\x83" encoding:NSWindowsCP1252StringEncoding];
I'm trying to convert BSTR to float with:
wcstod(data, NULL)
The problem is that this function works ok if data = 239.78, but i receive them in this format data = 239,78.
CComBSTR data = SysAllocString(L"239,78");
cout<<wcstod(data,NUll)<<endl;
Output of this code is 239.
Anyone could help?
Thank you.
You should use wcstod_l instead and pass the locale you need as the 3rd argument, so the ',' is understood and parsed properly. Something like this:
_locale_t fr = _create_locale(LC_ALL, "fr-FR"); // french locale
CComBSTR data = SysAllocString(L"239,78");
cout<<wcstod_l(data, NULL, fr)<<endl;
If the title isn't clear; I want to be able to select any character from textbox without making some complex loops-dependent code (I can do that one). For example, let's consider this text is entered in a textbox:
hello user!
I want some syntax when I tell to get me the index 1's value, it gives me "h", for index 5 = "o"... etc
So, anyone knows what's the right syntax, please help!
string can be directly indexed without any special code.
//from metadata
public sealed class String : IComparable, ICloneable, IConvertible, IComparable<string>, IEnumerable<char>, IEnumerable, IEquatable<string>
{
....
// Summary:
// Gets the character at a specified character position in the current System.String
// object.
//
// Parameters:
// index:
// A character position in the current string.
//
// Returns:
// A Unicode character.
//
// Exceptions:
// System.IndexOutOfRangeException:
// index is greater than or equal to the length of this object or less than
// zero.
public char this[int index] { get; }
....
}
dim str = "hello";
dim hchar = str(0);
dim echar = str(1);
dim lchar = str(2);
ect
Dim x As String = "Hello"
Console.Write(x.IndexOf("e")) 'Would return the position
Console.Write(x(x.IndexOf("e"))) 'Would return the character based upon the position
Console.Write(x(1)) 'Returns the character based at position 1 of the string
You can remove the Console.Write if you are using a WinForm.
TextBox1.Text = x(x.IndexOf("e"))
This should work.
Dim orig = "hello user!"
Dim res = Enumerable.Range(0,orig.Length).[Select](Function(i) orig.Substring(i,1))
So then you can do:
Dim x = res(0) 'x = "h"