Print an array as hex values in c# - c#

I want to print an array as hex. In the following code 'buffer' has been filled by 'XBaseRead'. I can print in hex using a foreach loop and the format "{0:X2}" but when I use string.join method with the same format it prints in decimal.
Here is the code:
byte[] buffer = new byte[160];
XBaseFunctions.XBaseRead(df2500VENDR, buffer, 160, false, XBaseFunctions.O_FLAG._O_BINARY);
foreach (byte i in buffer)
{
Console.Write("{0:X2} ", i);
}
Console.WriteLine("\n");
Console.WriteLine("{0:X2}", string.Join(", ", buffer));
Here is the output:
-----------------Dump of raw buffer-----------------
F0 F0 F0 F0 C1 F0 F1 F0 C1 40 C1 D5 E3 C8 D6 D5 E8 40 50 40 E2 D6 D5 E2 40 C9 D5
C3 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 F1 F4 F5 F0 40 E
6 40 F2 F1 E2 E3 40 E2 E3 5C C5 D9 C9 C5 6B 40 D7 C1 40 40 40 F1 F6 F5 F0 F2 40
40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40
40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 00 00 00 00 00 0
0 00 00 00 00 00 00 F9 60 03 A7 7E 3E 7E 3F 00 00 00 00 00 00 00 00 40 40 40 40
240, 240, 240, 240, 193, 240, 241, 240, 193, 64, 193, 213, 227, 200, 214, 213, 2
32, 64, 80, 64, 226, 214, 213, 226, 64, 201, 213, 195, 64, 64, 64, 64, 64, 64, 6
4, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 241, 244, 245, 240, 64, 2
30, 64, 242, 241, 226, 227, 64, 226, 227, 92, 197, 217, 201, 197, 107, 64, 215,
193, 64, 64, 64, 241, 246, 245, 240, 242, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 249, 96, 3, 167, 126, 62, 126, 63, 0, 0, 0, 0,
0, 0, 0, 0, 64, 64, 64, 64
So the question is Why does the {0:X2} work for the foreach loop but no for the string.join method?
Also, if someone could tell me how to limit each line to 16 bytes then I would appreciate that information too.

You should apply ToString("X2") on every byte first :
Console.WriteLine(string.Join(", ", buffer.Select(b => b.ToString("X2"));

Because when you're doing a foreach loop, you're passing a byte to the formatted Console.WriteLine(). However, you're passing a string of the entire joined buffer in the other instance, as string.Join(", ", buffer) returns a string.
foreach (byte i in buffer)
{
Console.Write("{0:X2} ", i); // <- A byte is being passed.
}
Console.WriteLine("{0:X2}", string.Join(", ", buffer)); // <- A string is being passed.
I would do use this:
var str = string.Join(", ", buffer.Select(x => string.Format("{0:X2}", x)));
Console.WriteLine(str);
...or this:
var str = string.Concat(buffer.Select(i => string.Format("{0:X2}, ", i)))
Console.WriteLine(str);
Although the first one doesn't give a trailing comma, so I would use that over the second method. Thought I'd share the different ways of accomplishing it, though.
Hopefully you're familiar with Linq, so I'm not going to go into great detail as far as what's going on, as it's pretty self-explanatory if you understand Linq.

Related

Encoding.ASCII.GetBytes(cArr) in Crystal

I'm converting some C# code into Crystal code for a project: my goal is to convert a string into an ASCII Byte array.
C# code
The C# code is the following:
string expression = "nYrXa#9Q\x00bf1&hCWM\\9(731Bp?t42=!k3.";
char[] cArr = expression.ToCharArray();
byte[] bytes = Encoding.ASCII.GetBytes(cArr);
Which gives:
{byte[32]} = 110, 89, 114, 88, 97, 64, 57, 81, 63, 49, 38, 104, 67, 87, 77, 92, 57, 40, 55, 51, 49, 66, 112, 63, 116, 52, 50, 61, 33, 107, 51, 46
Crystal implementation
temp = "nYrXa#9Q\x00bf1&hCWM\\9(731Bp?t42=!k3."
byteDecKey = temp.to_slice
array = [] of UInt8
temp.each_char do |char|
array << char.bytes[0] unless !char.ascii?
end
puts "EachChrKey: #{array}"
puts "ByteDecKey: #{byteDecKey}"
Result:
EachChrKey: [110, 89, 114, 88, 97, 64, 57, 81, 120, 48, 48, 98, 102, 49, 38, 104, 67, 87, 77, 92, 57, 40, 55, 51, 49, 66, 112, 63, 116, 52, 50, 61, 33, 107, 51, 46]
ByteDecKey: Bytes[110, 89, 114, 88, 97, 64, 57, 81, 120, 48, 48, 98, 102, 49, 38, 104, 67, 87, 77, 92, 57, 40, 55, 51, 49, 66, 112, 63, 116, 52, 50, 61, 33, 107, 51, 46]
C Sharp : Bytes[110, 89, 114, 88, 97, 64, 57, 81, 63, 49, 38, 104, 67, 87, 77, 92, 57, 40, 55, 51, 49, 66, 112, 63, 116, 52, 50, 61, 33, 107, 51, 46]
Am I doing something wrong?
In your C# code, \x00bf is being interpreted as an escape code representing the byte 0xbf, whereas in crystal, strings do not support \x escape codes as strings can contain only valid unicode. Therefore the contents of the string contain the characters x00bf instead of the signal byte 0xbf. Encoding.ASCII.GetBytes seems to replace the unknown byte 0xbf with ?.
I'd suggest using Slice(UInt8) for holding binary data, and using some encoding method such as base64 to transport the data if it's required as a string. Strings are meant to contain only valid unicode, not arbitrary data.

How to know a StreamGeometry is a Line [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a collection of StreamGeometrys. Each StreamGeometry could be any shape. I just want to pick up the StreamGeometrys that are lines. How can I know a StreamGeometry is a line.
Background:
I get collection of StreamGeometrys from an interface. The interface is given by other team and doesn't return the information that what the Geometry is. (I may discuss with them to make an update, but it involve the architecture change.) I have a function to group the Geometrys and get the outline of the group. Originally, I add them to a GeometryGroup, then I use Geometry.GetOutlinedPathGeometry to get the outline of the GeometryGroup. The behavior is good.
Problem:But when the Geometrys number increases to more than 200, GetOutlinedPathGeometry becomes extremely slow.
Current solution:Then I have to make a compromise to get an outline not that accurate. Instead of GetOutlinedPathGeometry, I use Geometry.Combine to combine the Geometrys. Geometry.Combine is mainly used for closed Geometry. When a Geometry is not closed, it will find the nearest points to make it closed. This behavior somehow reduce the complexity of the overall outline that GetOutlinedPathGeometry can get a result quicker.
How I get to this question:But one problem is that Geometry.Combine will omit Line Shapes. After combine all Geometrys, Lines are not in the group. So I want to find those lines and use GetOutlinedPathGeometry to get their outlines first. Then use their outlines to combine with others.
Also, even I have the Geometry type information, it could not help too much. Because Geometry.Combine will omit any Line Shapes that can be made of any Geometry type. Line, Path even Rectangle can all make a Line shape.
Other attempts:I tried to use GetOutlinedPathGeometry to get outline for each geometry first, then to combine. The performance improves a little bit but still very slow.
Just drop this code in and you can use StreamGeometry.IsLine() to check for lines.
public static class StreamGeometryExtensions
{
public static bool IsLine(this StreamGeometry sg, double sampleRate = 0.2, decimal tolerance = 0
{
PathGeometry g = sg.GetFlattenedPathGeometry();
if (g.MayHaveCurves())
{
return false;
}
Point origin, originTangent;
Point end, endTangent;
g.GetPointAtFractionLength(0, out origin, out originTangent);
g.GetPointAtFractionLength(1, out end, out endTangent);
Vector originToEnd = end - origin;
for (double i = 0; i < 1; i += sampleRate)
{
Point current, currentTangent;
g.GetPointAtFractionLength(i, out current, out currentTangent);
Vector currentToEnd = end - current;
Vector originTocurrent = current - origin;
decimal l1 = (decimal)(originTocurrent.Length + currentToEnd.Length);
decimal l2 = (decimal)originToEnd.Length;
if (Math.Abs(l2 - l1) > (l2 * tolerance))
{
return false;
}
}
return true;
}
}
The code "samples" the path every X% (example 0.2 = 20%) and checks if the origin, the end and the current sample point are on a line.
The code is a very general solution to the problem and can definetly be optimized for specific scenarios where performance is important.
I measured the performance for following Paths: (sampleRate = 0.2, tolerance = 0)
<Path Data="M 217,172 L 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300, 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300, 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300, 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300"></Path>
<Path Data="M 217,172 L 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20"></Path>
<Path Data="M 217,172 L 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300, 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300, 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300, 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300"></Path>
<Path Data="M 217,172 L 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300, 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300, 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300, 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300"></Path>
<Path Data="M 217,172 L 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300, 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300, 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300, 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300"></Path>
<Path Data="M 217,172 L 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20"></Path>
On an i7 4790 i got these times:
80µs - False
390µs - True
86µs - False
82µs - False
69µs - False
355µs - True

Read string of numbers into a grid/matrix [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Can anyone tell me how can I read this grid into arrays like a[i][j] ? I searched on google but I can't seem to find anything useful.Thank you very much for helping!
static void Main(string[] args)
{
String grid = "08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08" +
"49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00" +
"81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65" +
"52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91" +
"22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80" +
"24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50" +
"32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70" +
"67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21" +
"24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72" +
"21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95" +
"78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92" +
"16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57" +
"86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58" +
"19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40" +
"04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66" +
"88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69" +
"04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36" +
"20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16" +
"20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54" +
"01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48";
int[] a = new int[20];
for(int i=0;i<20;i++)
for (int j = 1; j < 20; j++)
{
}
}
As suggested in the comments you can simply separate your numbers and use split string. for example:
private static void Main(string[] args)
{
String grid = "08,02,22,97,38,15,00,40,00,75,04,05,07,78,52,12,50,77,91,08," +
"49,49,99,40,17,81,18,57,60,87,17,40,98,43,69,48,04,56,62,00," +
"81,49,31,73,55,79,14,29,93,71,40,67,53,88,30,03,49,13,36,65," +
"52,70,95,23,04,60,11,42,69,24,68,56,01,32,56,71,37,02,36,91," +
"22,31,16,71,51,67,63,89,41,92,36,54,22,40,40,28,66,33,13,80," +
"24,47,32,60,99,03,45,02,44,75,33,53,78,36,84,20,35,17,12,50," +
"32,98,81,28,64,23,67,10,26,38,40,67,59,54,70,66,18,38,64,70," +
"67,26,20,68,02,62,12,20,95,63,94,39,63,08,40,91,66,49,94,21," +
"24,55,58,05,66,73,99,26,97,17,78,78,96,83,14,88,34,89,63,72," +
"21,36,23,09,75,00,76,44,20,45,35,14,00,61,33,97,34,31,33,95," +
"78,17,53,28,22,75,31,67,15,94,03,80,04,62,16,14,09,53,56,92," +
"16,39,05,42,96,35,31,47,55,58,88,24,00,17,54,24,36,29,85,57," +
"86,56,00,48,35,71,89,07,05,44,44,37,44,60,21,58,51,54,17,58," +
"19,80,81,68,05,94,47,69,28,73,92,13,86,52,17,77,04,89,55,40," +
"04,52,08,83,97,35,99,16,07,97,57,32,16,26,26,79,33,27,98,66," +
"88,36,68,87,57,62,20,72,03,46,33,67,46,55,12,32,63,93,53,69," +
"04,42,16,73,38,25,39,11,24,94,72,18,08,46,29,32,40,62,76,36," +
"20,69,36,41,72,30,23,88,34,62,99,69,82,67,59,85,74,04,36,16," +
"20,73,35,29,78,31,90,01,74,31,49,71,48,86,81,16,23,57,05,54," +
"01,70,54,71,83,51,54,69,16,92,33,48,61,43,52,01,89,19,67,48";
var splitstring = grid.Split(',');
var a = new int[20,20];
const int rowCount = 19; //counts 0 as 1
var rowIndex = 0;
var colIndex = 0;
foreach (var s in splitstring)
{
if (rowIndex > rowCount)
{
rowIndex = 0;
colIndex++;
}
a[colIndex, rowIndex] = Int32.Parse(s);
rowIndex++;
}
}
Note the Int32.Parse(s) will throw an exception if the parse fails. You can instead use an Int32.TryParse and use the out value for your result. Depends on what you want to do.
Consider add a space to the end of each "line" such as the following:
String grid = "08 02 .. 91 08 " +
"01 70 .. 67 48 ";
// ^-- add space here
This will allow the string to be converted trivially into a 1D array of strings with string.Split.
string grid = "08 02 .. 91 08"; // every number is space-separated now
string[] gridArray = grid.Split(" "); // -> ["08", "02", .. "91", "08"]
(Even without ensuring the extra spaces, a 1D array can be achieved using a Regular Expression split: var gridArray = Regex.Split(grid, "(?:\s|(?<=\d{2})(?=\d{2}))"), but I recommend "normalizing" the input string literals if possible.)
And each index in the resulting 1D array can be accessed as so, where columns represents the number of columns of the super-imposed matrix, or the "width" of each row.
int columns = 20;
int gridIndex = j * columns + i; // where j is a *row* and i is a *column*
// for a column-major matrix
string numStr = gridArray[gridIndex]; // and value at the [column,row]
Then it's just a matter of converting numStr to an integer and assigning it to the appropriate array index.
If every number is separated by a space such that ever number NN is in form "NN " it also takes up 3 characters. In this case the intermediate Split can be skipped, using the same idea of indexing into the source as a 1D sequence.
int gridNumOffset = (j * columns + i) * 3;
string numStr = grid.Substring(gridNumOffset, 2);
(Finding the substring offset even when there is no space at the end-of-line can be done using a little bit more math, which is a good exercise and the formula just becomes (j * columns + i) * 3 + f(i), where f(i) applies the appropriate offset.)
Another more mundane approach, assuming that the original string cannot be modified to include an end-of-line space/character, is to read in each line as N characters, deal with it, and move on. The concepts from above can be applied:
int rowWidth = (columns * 3) - 1; // -1 assuming no line-end space
for(int j = 0; j < rows; j++) { // j is *rows*, for column-major matrix
string rowStr = str.Substring(j * rowWidth, rowWidth);
string[] row = rowStr.Split(" "); // 1D array just for this row
for (int i = 0; i < columns; i++) {
string numStr = row[i];
// Convert and put value into the correct matrix position
}
}

Int Array to Base64 in C# like sjcl.codec.base64.fromBits does

In JavaScript, doing this:
var numbers = new Array(1042147201, -1682263442, -1463053899, 1834416100)
sjcl.codec.base64.fromBits(numbers)
Return "Ph3ngZu6sm6oy5G1bVb35A==", but doing this in C#:
var numbers = new[] { 1042147201, -1682263442, -1463053899, 1834416100 };
var byteNumbers = new byte[numbers.Length * sizeof(int)];
Buffer.BlockCopy(numbers, 0, byteNumbers, 0, byteNumbers.Length);
Convert.ToBase64String(byteNumbers);
Return "gecdPm6yupu1kcuo5PdWbQ=="
Why is the result different and what do I have to do to get the same result like in JavaScript?
Looking at output of the 2 two pieces of code you have issue with endianness of ints
1834416100 - > 6D 56 F7 E4
Ph3ngZu6sm6oy5G1bVb35A== -> 3E 1D E7 81 9B BA B2 6E A8 CB 91 B5 6D 56 F7 E4
gecdPm6yupu1kcuo5PdWbQ== -> 81 E7 1D 3E 6E B2 BA 9B B5 91 CB A8 E4 F7 56 6D
Possible fix: reverse each integer when adding to array as shown in BitConverter class
int value = 12345678;
byte[] bytes = BitConverter.GetBytes(value);
if (BitConverter.IsLittleEndian)
Array.Reverse(bytes);

Printing 100 to 1 ?How?

print 10 number on each line , Below Is The Code Of What I Did But Still Not Successful , Help
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int i;
for (i =100; i>=1; i--)
{
Console.Write(i);
}
Console.ReadLine();
}
}
}
Try this:
for (i = 100; i >=1; i--)
{
if(i%10==0) //if 10 numbers are printed
Console.WriteLine(); //then line break
Console.Write(i+" "); //print the number with a space character
}
Console.ReadLine();
It will print 10 number in each line.
Result:
100 99 98 97 96 95 94 93 92 91
90 89 88 87 86 85 84 83 82 81
80 79 78 77 76 75 74 73 72 71
70 69 68 67 66 65 64 63 62 61
60 59 58 57 56 55 54 53 52 51
50 49 48 47 46 45 44 43 42 41
40 39 38 37 36 35 34 33 32 31
30 29 28 27 26 25 24 23 22 21
20 19 18 17 16 15 14 13 12 11
10 9 8 7 6 5 4 3 2 1
See result in ideone.
Try this
static void Main(string[] args)
{
int i=100;
for (i; i <=100; i--)
{
Console.Write(i);
if(i==0)
{
break;
}
}
Console.ReadLine();
}
Edit
Must use
if(i==0){break;}
otherwise this for loop does not end.
static void Main(string[] args)
{
for (int i = 100; i >0; i--)
{
if (i % 10 == 0)
Console.WriteLine();
Console.Write(i);
}
Console.ReadLine();
}
This might do what you need:
Enumerable.Range(1, 100).Reverse()
.ToList()
.ForEach(i => Console.Write(i % 10 == 1 ? i + "\r\n" : i + ", "));
Which will output:
100, 99, 98, 97, 96, 95, 94, 93, 92, 91
90, 89, 88, 87, 86, 85, 84, 83, 82, 81
80, 79, 78, 77, 76, 75, 74, 73, 72, 71
70, 69, 68, 67, 66, 65, 64, 63, 62, 61
60, 59, 58, 57, 56, 55, 54, 53, 52, 51
50, 49, 48, 47, 46, 45, 44, 43, 42, 41
40, 39, 38, 37, 36, 35, 34, 33, 32, 31
30, 29, 28, 27, 26, 25, 24, 23, 22, 21
20, 19, 18, 17, 16, 15, 14, 13, 12, 11
10, 9, 8, 7, 6, 5, 4, 3, 2, 1
using System;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int i;
for (i = 100; i > 0; i--)
{
if(i%10==0)Console.WriteLine();
Console.Write(i);
}
Console.ReadLine();
}
}
}
Updated Demo:http://ideone.com/st4i8n
Enumerable.Range(0, 10).Reverse().ToList().ForEach(n =>
{
Enumerable.Range(n * 10, 10).Select(i => i + 1).Reverse().ToList().ForEach(i => Console.Write(i + " "));
Console.WriteLine();
});
// output: 100 99 98 97 96 95 94 93 92 91
// 90 89 88 87 86 85 84 83 82 81
// 80 79 78 77 76 75 74 73 72 71
// 70 69 68 67 66 65 64 63 62 61
// 60 59 58 57 56 55 54 53 52 51
// 50 49 48 47 46 45 44 43 42 41
// 40 39 38 37 36 35 34 33 32 31
// 30 29 28 27 26 25 24 23 22 21
// 20 19 18 17 16 15 14 13 12 11
// 10 9 8 7 6 5 4 3 2 1
HundredToOne()
{
for(int i=100;i>0;i--)
{
if(i%10 == 0)
Console.WriteLine("\n");
Console.Write(i);
}
}
You can also use a StringBuilder to build the 10 numbers per line, but concatenating like so should yield sufficient results (since it's only 100 numbers)
foreach (var number in Enumerable.Range(1, 100).ToArray().Reverse())
{
Console.Write((number % 10 == 0) ? "\n" : (number.ToString() + " "));
}

Categories