|
I have a base64 input("NjU0MDgA=") in a XML file which I am trying to convert to hexadecimal with the following code in C#:
ASCIIEncoding ascii = new ASCIIEncoding();
byte[] bytes = Encoding.ASCII.GetBytes("NjU0MDgA=");
string hex = BitConverter.ToString(bytes);
hex.Replace("-", String.Empty);
The 'hex' output is in the following format:
4D5459334D5445344D4467413D
However, required output should be in the format:
#44ff11 (example)
|
|
|
|
|
I'm confused. NjU0MDgA= decoded is "65408" which is FF80 in hex.
|
|
|
|
|
Apologies for any confusion.. I just gave sample values.
I am trying to get the values you mentioned programmatically.
|
|
|
|
|
I use Encoding.GetString ( System.Convert.FromBase64String ( Source ) ) , which yields TmpVME1EZ0E9 from your sample data.
|
|
|
|
|
And hex should be "4E6A55304D4467413D"
|
|
|
|
|
|
Thanks for your help. I was able to solve it. Following is what I ended up with:
byte[] decoded = Convert.FromBase64String("UUEncoded string to be decoded");
String value = ASCIIEncoding.ASCII.GetString(decoded);
int output = Convert.ToInt32(value);
// Convert int to hex format and reverse the byte
String.Format("{0:X02}{1:X02}{2:X02}", (output & 0x0000FF) >> 0, (output & 0x00FF00) >> 8, (output & 0xFF0000) >> 16);
|
|
|
|
|
Hi,
I would like to anyone tried to knows where to get details on how to post sales into Sage 50 Accounting using C# and sData[^]
Thanks,
Jassim
Technology News @ www.JassimRahma.com
|
|
|
|
|
Have some google foo[^] have you had a rat through their doco?
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
|
Hello...My self Swati. I'm from rajkot..
I want some extra report's name for my project..my project name is "BILLING SYSTEM"..so please suggest the report name for my project.which helps to improve my marks in project..thank you..
|
|
|
|
|
How about:
[Course Instructor's Name] Off-Shore Account Report [Month] [Year]
Where you'd fill in the text in brackets above with the appropriate text data ?
No, seriously, how about just:
Billing System Report [report number] [Time-interval Year/Quarter/Month/Week/Day]
सौभाग्य
«A man will be imprisoned in a room with a door that's unlocked and opens inwards ... as long as it does not occur to him to pull rather than push» Wittgenstein
|
|
|
|
|
I'm sorry, but what does this have to do with C#?
|
|
|
|
|
You knocked on the wrong door.
|
|
|
|
|
I'm developing a Smart Array (it's a request I cannot use a List of int that I know it's easier because I made both codes). I have done this class and below is the example of how I use it. The error is often in this line (153 from class):
// Ensure that the lock is released.
Monitor.Exit(array);
If I use a List nothing wrong happens just when I translate to an array. Thanks for your help.
SmartArray3.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace SmartArray
{
class SmartArray3
{
private int[] array;
private int size = 0;
private int count = 0;
public SmartArray3()
{
Resize(1);
}
public SmartArray3(int size)
{
this.size = size;
array = new int[this.size];
}
public bool Resize(int size)
{
try
{
if (array == null)
array = new int[size];
else
Array.Resize(ref array, size);
this.size++;
return true;
}
catch
{
return false;
}
}
private void add(int value)
{
try
{
if (array == null)
{
this.size = 1;
Resize(this.size);
array[0] = value;
this.count++;
}
else
{
if (this.count == (this.size - 1))
{
this.size *= 2;
this.Resize(this.size);
}
if ((this.count - 1) < 0)
array[0] = value;
else
array[this.count - 1] = value;
this.count++;
}
}
catch (Exception ex)
{
Console.Write(ex.ToString());
throw new System.IndexOutOfRangeException("Index out of Range.");
}
}
public void Add(int value)
{
Monitor.Enter(array);
try
{
if (array == null)
{
this.size = 1;
Resize(this.size);
array[0] = value;
this.count++;
}
else
{
if (this.count == (this.size - 1))
{
this.size *= 2;
this.Resize(this.size);
}
if ((this.count - 1) < 0)
array[0] = value;
else
array[this.count - 1] = value;
this.count++;
}
}
finally
{
Monitor.Exit(array);
}
}
public bool TryAdd(int value)
{
if (Monitor.TryEnter(array))
{
try
{
if (array == null)
{
this.size = 1;
Resize(this.size);
array[0] = value;
this.count++;
}
else
{
if (this.count == (this.size - 1))
{
this.size *= 2;
this.Resize(this.size);
}
if ((this.count - 1) < 0)
array[0] = value;
else
array[this.count - 1] = value;
this.count++;
}
}
finally
{
Monitor.Exit(array);
}
return true;
}
else
{
return false;
}
}
public int Get(int index)
{
try
{
return array[index];
}
catch (IndexOutOfRangeException ex)
{
throw new System.IndexOutOfRangeException("Index out of range");
}
}
}
}
Code for called the Class:
private static int threadsRunning = 0;
private SmartArray3 sa = new SmartArray3();
private List times;
private static string[] titles ={
"Add ", "Add failed ", "TryAdd succeeded ", "TryAdd failed "};
private static int[][] results = new int[3][];
private void newTest()
{
for (int i = 0; i < 3; i++)
{
Thread t = new Thread(ThreadProc);
t.Start(i);
Interlocked.Increment(ref threadsRunning);
}
}
private void ThreadProc(object state)
{
times = new List();
DateTime finish = DateTime.Now.AddSeconds(10);
Random rand = new Random();
int[] result = { 0, 0, 0, 0};
int threadNum = (int)state;
while (DateTime.Now < finish)
{
Stopwatch sw = Stopwatch.StartNew();
int what = rand.Next(250);
int how = rand.Next(25);
if (how < 16)
{
try
{
sa.Add(what);
result[(int)ThreadResultIndex.AddCt] += 1;
times.Add(sw.Elapsed.TotalMilliseconds);
}
catch
{
result[(int)ThreadResultIndex.AddFailCt] += 1;
}
}
else
{
if (sa.TryAdd(what))
{
result[(int)ThreadResultIndex.TryAddSucceedCt] += 1;
}
else
{
result[(int)ThreadResultIndex.TryAddFailCt] += 1;
}
}
sw.Stop();
}
results[threadNum] = result;
if (0 == Interlocked.Decrement(ref threadsRunning))
{
StringBuilder sb = new StringBuilder(
" Thread 1 Thread 2 Thread 3 Total\n");
for (int row = 0; row < 4; row++)
{
int total = 0;
sb.Append(titles[row]);
for (int col = 0; col < 3; col++)
{
sb.Append(String.Format("{0,4} ", results[col][row]));
total += results[col][row];
}
sb.AppendLine(String.Format("{0,4} ", total));
}
Console.WriteLine(sb.ToString());
}
}
private enum ThreadResultIndex
{
AddCt,
AddFailCt,
TryAddSucceedCt,
TryAddFailCt
}
Federico Navarrete
|
|
|
|
|
The object you pass to Monitor.Exit needs to be the same object that you passed to Monitor.Enter / Monitor.TryEnter .
When you call your Resize method, you're setting the array field to a new array instance. You then pass that new instance to Monitor.Exit , even though you've never passed it to Monitor.Enter .
The simplest solution would be to use a separate read-only object for all Monitor calls:
class SmartArray3
{
private readonly object _locker = new object();
...
public void Add(int value)
{
Monitor.Enter(_locker);
try
{
...
}
finally
{
Monitor.Exit(_locker);
}
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Good spot!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Hi,
In an attempt to graduate into a more professional programmer, I'm trying to comment my code more and use classes more. I have a demo project working and sadly enough I'm not very well versed in creating/using my own classes but would like to change that.
I have a datagridview demo project that I've written with the idea that I will have multiple datagridviews in my final project, on multiple tabs. Instead of cutting and pasting and changing the code multiple times, it seems like my own class would benefit me greatly in this instance, where I could instantiate the same object multiple times.
If this is correct, can I have help creating a class out of this demo project? It's ~1000 lines of code, but I'll be happy to post it if that would help.
Thanks
|
|
|
|
|
I'm not planning on even looking at 1000 lines of your code - I don't have the time - but I'd start by looking at creating a UserControl[^] rather than a generic class - they display directly. And then either embedding a DataGridView or deriving from one. If you are planning on using it in tabs, I'd also consider creating another UserControl, derived from TabPage which encapsulated the DataGridView UserControl to make that easier to use as well.
How easy this is for you to do will depend on how you wrote the original code: it may be worth your creating a new project and "playing with it" until you get the idea before you start launching into moving existing code.
I use UserControls a lot - they help to contain code and simplify interfaces between modules. Worth looking at!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Thanks for the reply. I read the MS article, and I also found this video.
This video seems a little more understandable for me being new to this, as I can see it done, would this also be a good representation of what you are talking about?
https://www.youtube.com/watch?v=l5L_q_jI494[^]
|
|
|
|
|
Difficult to tell - his voice and the microphone "wind noise" was so annoying I couldn't watch that much!
Seriously, give it a try on a "new project" and see what happens. It really does help with simplifying interfaces between modules and speeds up development too.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
That means you didn't make it to 4:20 as about that point a random hammer starts banging the wall behind him ... or he is residing in a cheap motel, I'm not sure?
I've started with a new project and a user control class. I can't get the class to instantiate, but I will keep try and report back if I fail.
Thanks.
|
|
|
|
|
Build the project, and it should add the new control to the top of the toolbox. Drag and drop one onto your form, then look at the designer.cs file - it'll show you the code VS uses to instantiate it.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Ok, I tried the code below but it did not work and my basic class is at the bottom. Under designer.cs, should I be using some of the code under InitializeComponent()?
dataGridViewUserControl1 dgv1 = new dataGridViewUserControl1();
dgv1.ColumnCount = 25;
dgv1.RowCount = 20;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace UserControlDemo
{
public partial class dataGridViewUserControl1 : UserControl
{
public dataGridViewUserControl1()
{
InitializeComponent();
}
private int columnCount;
public int ColumnCount
{
get { return columnCount; }
set { columnCount = value; }
}
private int rowCount;
public int RowCount
{
get { return rowCount; }
set { rowCount = value; }
}
private void UserControl1_Load(object sender, EventArgs e)
{
for (int i = 0; i < columnCount; i++)
{
dataGridView1.Columns[i].Name = "column " + i;
}
for (int i = 0; i < rowCount; i++)
{
string[] rowName = new string[] { "row" + i.ToString() };
dataGridView1.Rows.Add(rowName);
}
}
}
}
|
|
|
|
|
If you look at the designer file, you'll find some more code: of nothing else there will be a Controls.Add method call to add the new control instance to the forms Controls collection - that's the line which actually makes it display within the form. (For your app, it'll eventually be in the TabPage.Controls collection to make it a "child" of that control rather than the firm itself, but that's the advanced bit )
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|