This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I am a beginner to C. I know C and I am little new to objects and classes.
I thought to do some exercises using C#. So I want to encode the each character of a string. So I have planned to convert string (login_name) into char[] array and then loop through the each element of char[] array and perfom little manipulation and give some encoded value to the each character. Finally assign these values to another char[] sery and convert back this char[] sery to string.
I have written the basic idea. Take a look at this
char[] array = login_name.ToCharArray(); // Converted string to char array
char[] sery = null; //created a new array.is it neccessary to specify the size?
// convert the characters to int and perfom some opeartion;
// here i have ex-ored with 123
int a =((int)array[0]) ^ 123;
// now convert the encoded value to char and assign it to each element of char[] sery
sery[0] = (char) a; but this statement gives run time error
Gives a run time error: create a new instance of char.
What does it mean?
You need to define the size of the array, otherwise in current form your sery is null
char[] sery = new char[array.Length]; //if the elements number would be same as array
Or you can use List<T>
List<char> seryList = new List<char>();
Related
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I've been while out of C# and MVC. And I am really struggling with the following error, I really don't see it. I have a list of restrictions and i want to add their keys to an string[].
int cntr = 0;
//loop through restrictions and add to array
foreach (var Restriction in this.admingroupRepository.Context.AdminRestrictions.ToList())
{
currentRestrictionKeys[cntr] = Restriction.Key;
cntr += 1;
}
This is the error i get on the cntr += 1 line:
Index was outside the bounds of the array.
I don't understand where this comes from, the foreach breaks before the cntr is out of the array's bounds right?
You have allocated too little space for currentRestrictionKeys. But you don't really need to preallocate it at all; you can just use a trivial projection with LINQ:
var currentRestrictionKeys = this.admingroupRepository.Context.AdminRestrictions
.Select(r => r.Key)
.ToArray();
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have 6 string arrays that contain a series of characters. What I would like to be able to do is random pick an array and once its picked afterwards, will produce a final string response. Do I need a list to do this or is there another way?
All of this is just an small array exercise using a random variable.
I should specify that this is a console application.
A simple option would be: Make an array of the arrays. Pick a random index to get one of the arrays. Create a reference to this and then pick each member as needed.
//let say u have an array of string
string[] myarr = new string[] { "str1", "str3", "str3", "str4", "str5", "str6"};
Random rnd = new Random();
// you dont need a list, simply pick one rnd element from array
string myRandomPickedString = myarr[rnd.Next(0, myarr.Length - 1)];
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am currently working on a console text analysis program for an assignment.
My problem is, I need to save all user entered words above 7 letters to a text file. The user can enter words by typing in their paragraph or by loading from a text file.
Any ideas on how I can do this ?
Thanks for any help in advance
Without giving you the code, think about what you really need to do.
Read in the entire text.
Split the text based on spaces / punctuation to identify each word. This will be stored in an array.
Test each split string's length.
Write the results to a file.
I'm not sure how you're getting the user-entered words, but you can do a simple LINQ statement to get the words greater than 7 letters:
//get all words into an array (wordArray)
var bigWords = wordArray.Where(w => w.Length > 7).ToArray();
Then you do something with the bigWords array.
I'm not going to do your assignment for you.
You should check functions such as String.Split, String.Length and String.Substring()
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I need to convert a char to an int and back.
Here's what I'm using to convert the char to an int:
(char)65 // Returns 'a'
Here's what I'm TRYING to use to convert it back:
(int)'a' // Returns 97
Any ideas what I can do?
try this,
char x = 'a';
int y = (int)x;
65 is the character code for a capital 'A'. 97 is a lower case 'a'.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I want to code this part of a VB6 application in c#.
How can I change a long into a Hex value?
Public Function longToHex(l As Long) As String
longToHex = Hex(l)
If Len(longToHex) < 4 Then longToHex = String(4 - Len(longToHex), "0") & longToHex
longToHex = Right(longToHex, 2) & Left(longToHex, 2)
End Function
Just format to a padded hex string:
string.Format("{0:X4}", myLong.ToString().Length / 2)
Then transpose the first two characters with the last two.
The VB6 code appears to take the length of sData divided by 2, then converts the length to a Hex string and pads it with 0s to 4 characters if needed. It then transposes the first two characters with the last two.
Seems convoluted -- what is the code supposed to do? Half the length of the string in hex?
This might work: sLen = (sData.length / 2).ToString("X")