I have a problem when converting a char to hex value the code below works correctly when the char is a number but it's throw exception when the char is a latter
System.FormatException: 'Input string was not in a correct format
Code:
public byte[,] get_state(string plainText)
{
char[] cplainText = plainText.ToCharArray();
byte[,] state = new byte[4, 4];
plainText = plainText.Remove(0, 2);
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j+=2)
{
string sub = plainText.Substring((i * 4 + j), 2);
state[i, j] = Convert.ToByte(sub);
}
}
return state;
}
The input string was "0x3243F6A8885A308D313198A2e0370734" and the exception across when the iteration of "F6"
Convert.ToByte();
It says in the overload that it only accepts numbers in string format.
You have to think about if you use the right method, or convert it beforehand.
Related
Error message: System.ArgumentException: 'Destination array is not long enough to copy all the items in the collection. Check array index and length.'
public static string PassConv(string Password)
{
int iLoop;
int iL;
int iTotal;
int iValue;
string sPassConv;
iL = 3;
iTotal = 0;
for (iLoop = 1; iLoop <= Password.Length; iLoop++) {
string sNo = Password.Substring(iLoop-1,1);
byte[] asciiBytes = Encoding.ASCII.GetBytes(sNo);
iValue = BitConverter.ToInt32(asciiBytes, 0); ---->**Error**
iTotal = iTotal + iValue * (iL + iLoop - 1);
sPassConv = iTotal.ToString();
}
return sPassConv;
}
Please Help
Thank
I try to reach your idea because of so many redundancy codes. By using:
SubString exports each character even Encoding.ASCII.GetBytes can return all byte array from these characters
BitConverter expected of 4-byte binary input as well
https://learn.microsoft.com/en-us/dotnet/api/system.bitconverter.toint32 and seem this function is no help at all
Declare iLoop = 1 but inside loop does iLoop - 1
sPassConv gets value from iTotal so many times, should put outside the loop
Quick fix from your code:
public static string PassConv(string Password)
{
int iLoop;
int iL;
int iTotal;
int iValue;
string sPassConv = "";
iL = 3;
iTotal = 0;
for (iLoop = 1; iLoop <= Password.Length; iLoop++)
{
string sNo = Password.Substring(iLoop - 1, 1);
iValue = Encoding.ASCII.GetBytes(sNo)[0];
iTotal = iTotal + iValue * (iL + iLoop - 1);
sPassConv = iTotal.ToString();
}
return sPassConv;
}
A little bit refactor
public static string PassConv2(string Password, int iL = 3)
{
int iTotal = 0;
var bytes = Encoding.ASCII.GetBytes(Password);
for (var iLoop = 0; iLoop < bytes.Length; iLoop++)
{
// bytes[iLoop] = iValue
iTotal += bytes[iLoop] * (iL + iLoop);
}
return iTotal.ToString();
}
This function is need to be improved because it does loop 2 times, first to bytes and second to calculate. Since every character can convert to ASCII even Extended ASCII, so we could cast it directly. PassConv3 gives more exact when input is Unicode such as "Áaa", the Encoding.ASCII.GetByteswill cast as "?aa" with '?' represents 63 ASCII
public static string PassConv3(string Password, int iL = 3)
{
int iTotal = 0;
for (var iLoop = 0; iLoop < Password.Length; iLoop++)
{
iTotal += Password[iLoop] * (iL + iLoop);
}
return iTotal.ToString();
}
And the result
public static void MainFunc()
{
Console.WriteLine(PassConv("ABCD")); // 1202
Console.WriteLine(PassConv2("ABCD")); // 1202
Console.WriteLine(PassConv3("ABCD")); // 1202
}
Hope it helps !
I'm writing a tool that gets GPS coordinates from a XML file and sends them to the Google Static Maps API. I'm trying to get a polyline from Google.
I've found the documentation and so far I've written this to convert a double value to a encoded value for a polyline:
private String convertDouble(Double _input)
{
String result = String.Empty;
//value * 1e5
Int32 multiplication = Convert.ToInt32(Math.Floor(_input * 1e5));
//value to binary string
String binaryString = Convert.ToString(multiplication, 2);
//binary to hex
binaryString = BinaryStringToHexString(binaryString);
//value to hex + 1
Int32 hexConvert = Convert.ToInt32(binaryString, 16) + Convert.ToInt32("01", 16);
//value to binary string
binaryString = Convert.ToString(hexConvert, 2);
//binary string zu int[] for further calculations
Int32[] bitValues = new Int32[binaryString.Length];
for (Int32 i = 0; i < bitValues.Length; i++)
{
if (binaryString[i] == '0')
{
bitValues[i] = 0;
}
else
{
bitValues[i] = 1;
}
}
//shift binary to left
Int32[] bitValues_2 = new Int32[bitValues.Length];
for (Int32 i = 0; i < bitValues.Length - 1; i++)
{
bitValues_2[i] = bitValues[i + 1];
}
bitValues_2[bitValues_2.Length - 1] = 0;
//if input value is negative invert binary
if (_input < 0)
{
for (Int32 i = 0; i < bitValues.Length; i++)
{
if (bitValues_2[i] == 0)
{
bitValues_2[i] = 1;
}
else
{
bitValues_2[i] = 0;
}
}
}
//make blocks of 5
Int32 lengthDifference = bitValues_2.Length % 5;
Int32[] bitValues_3 = new Int32[bitValues_2.Length - lengthDifference];
for (Int32 i = bitValues_2.Length - 1; i > (bitValues_2.Length - bitValues_3.Length); i--)
{
bitValues_3[i - (bitValues_2.Length - bitValues_3.Length)] = bitValues_2[i];
}
//twist blocks
Int32[] bitValues_4 = new Int32[bitValues_3.Length];
Int32 numberOfBlocks = bitValues_3.Length / 5;
Int32 counter = 0;
String[] stringValues = new String[numberOfBlocks];
for (Int32 i = numberOfBlocks - 1; i >= 0; i--)
{
for (Int32 j = i * 5; j < (i * 5) + 5; j++)
{
bitValues_4[counter] = bitValues_3[j];
counter++;
}
}
counter = 0;
//write int[] into strings for final conversion
for (Int32 i = 0; i < bitValues_4.Length; i++)
{
if (i > 0 && i % 5 == 0)
{
counter++;
}
stringValues[counter] += bitValues_4[i].ToString();
}
// blocks ^ 0x20 (32) and convert to char
Int32 value = 0;
Int32[] intValues = new Int32[stringValues.Length];
Char[] charValues = new Char[stringValues.Length];
for (Int32 i = 0; i < stringValues.Length; i++)
{
value = Convert.ToInt32(stringValues[i], 2);
if (i < stringValues.Length - 1)
{
intValues[i] = value ^ 32;
}
else
{
intValues[i] = value;
}
intValues[i] = intValues[i] + 63;
charValues[i] = (Char)intValues[i];
result += charValues[i];
}
return result;
}
If I use the value from the documentation
-179.9832104
I get the result
`~oia#
Which is fine, but if I use one of my values e.g.:
LAT: 8.7587061 LONG: 48.6331662
LAT: 8.8905152 LONG: 48.6226701
I get the wrong values. The final polyline should be in southern Germany. But with my calculation the polyline is somewhere near a cost. Maybe there is a finished class that gives me the encoded coordinates and I haven't found it yet.
And yes, I have to encode the polylines, because there will be many different coordinates in the XML (the GPS tracker runs for several hours and captures the location every 10 seconds).
Well, after another night of searching and testing I have found a solution.
Brian Pedersen has a solution in his blog. And it works just like it should.
I don't want to copy and paste the code here, but the link has it.
My goal is to be able to convert a string into binary code that is still a string. I am able to turn the string into byte[] but not back to a string without decoding it.
You can use the Convert method for that:
byte [] bytesToEncode = Encoding.UTF8.GetBytes (inputText);
string encodedText = Convert.ToBase64String (bytesToEncode);
If you can encode/decode a byte, e.g.
private static String ToBinary(Byte value) {
StringBuilder Sb = new StringBuilder(8);
Sb.Length = 8;
for (int i = 0; i < 8; ++i) {
Sb[7 - i] = (Char) ('0' + value % 2);
value /= 2;
}
return Sb.ToString();
}
private static Byte FromBinary(String value) {
int result = 0;
for (int i = 0; i < value.Length; ++i)
result = result * 2 + value[i] - '0';
return (Byte) result;
}
You can easily encode/decode a whole string:
// Encoding...
String source = "abc";
// 011000010110001001100011
String result = String.Join("", UTF8Encoding.UTF8.GetBytes(source).Select(x => ToBinary(x)));
...
// Decoding...
List<Byte> codes = new List<Byte>();
for (int i = 0; i < result.Length; i += 8)
codes.Add(FromBinary(result.Substring(i, 8)));
// abc
String sourceBack = UTF8Encoding.UTF8.GetString(codes.ToArray());
use
string str = "Welcome";
byte []arr = System.Text.Encoding.ASCII.GetBytes(str);
I am trying to write some C# code for Unity that will read from a text file, store each line in a string array, and then convert it to a 2D char array.
The error occurs at:
void ReadFile()
{
StreamReader read = new StreamReader(Application.dataPath + "/Maze1.txt");
int length = read.ReadLine().Length;
maze = new string[length, length];
line = new string[length];
while(!read.EndOfStream)
{
for (int i = 0; i <= length; i++)
{
line[i] = read.ReadLine();
}
for( int i = 0; i <= length; i++)
{
for( int j = 0; j <= length; j++)
{
maze[i,j] = line[i].Split(','); // <---This line is the issue.
}
}
}
}
The Exact error I am getting is:
Cannot Implicitly convert type 'string[]' to 'string'
What does this error mean and how do I fix the code?
I have a feeling you meant to do this:
for( int i = 0; i <= length; i++)
{
var parts = line[i].Split(',');
for( int j = 0; j <= length; j++)
{
maze[i,j] = parts[j];
}
}
As error says maze[i,j] will take a string but line[i].Split(','); would return a string[].
The better data structure for the maze is in your case an array of arrays, instead of the 2d array. So you can assign the result of the split operation directly without the extra copy:
StreamReader read = new StreamReader(Application.dataPath + "/Maze1.txt");
string firstLine = read.ReadLine();
int length = firstLine.Length;
string[][] maze = new string[length][];
maze[0] = firstLine.Split(',');
while(!read.EndOfStream)
{
for (int i = 1; i < length; i++)
{
maze[i] = read.ReadLine().Split(',');
}
}
You can then access the maze similar to the 2d array:
var aMazeChar = maze[i][j];
This question already has answers here:
How to convert byte array to string [duplicate]
(4 answers)
Closed 9 years ago.
Is there an easy way to convert a byte array to a string so the following unit test passes? I can't find an encoding that works for all values.
[TestMethod]
public void TestBytToString()
{
byte[] bytArray = new byte[256];
for (int i = 0; i < bytArray.Length; i++)
{
bytArray[i] = (byte)i;
}
string x = System.Text.Encoding.Default.GetString(bytArray);
for (int i = 0; i < x.Length; i++)
{
int y = (int)x[i];
Assert.AreEqual(i, y);
}
}
The System.Text.Encoding.UTF8 should do a trick for you.
string x = Encoding.UTF8.GetString(bytArray, 0, bytArray.Length);
var str = System.Text.Encoding.Default.GetString(bytArray);
As far as i know everthing above value 127 in byte is considered a negative number and as char can only take positive values it results with an unknown char in every encoding you take.
You might want to convert the byte array to unsigned short (ushort) and then to string...
This worked:
[TestMethod]
public void TestBytToString()
{
byte[] bytArray = new byte[256];
ushort[] usArray = new ushort[256];
for (int i = 0; i < bytArray.Length; i++)
{
bytArray[i] = (byte)i;
}
string x = System.Text.Encoding.Default.GetString(bytArray);
for (int i = 0; i < x.Length; i++)
{
int y = System.Text.Encoding.Default.GetBytes(x.Substring(i, 1))[0];
Assert.AreEqual(i, y);
}
}