Showing posts with label number. Show all posts
Showing posts with label number. Show all posts

Saturday, March 31, 2012

string to byte array conversion

I've a string similiar to "A509DE5B" (Length == 8 ) where each 2 characters are 1 hex number. How to convert such string into array of bytes (Lenght == 4)? In C# please, it's important...Byte.Parse()?

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystembyteclassparsetopic.asp
Hi LesioS!

I think you should try out something like this (I haven't tested it out myself, not sure if it works!!)


string sBytes = "A509DE5B";

Byte[] bytes = new Byte[(int)(sBytes.Length/2)];

int i = 0, j = 0;

while(i < sBytes.Length)
{
bytes[j] = Byte.Parse(str.Substring(i,2), NumberStyles.HexNumber);
i += 2;
}

Maybe it would work..

Good luck!
Oh, sorry, there's lots of mistakes in the code earlier code.

I tested this, and it worked, so be my guest! :)


string sBytes = "A509DE5B";
int i=0, j=0;
Byte[] bytes = new Byte[(int)(sBytes.Length/2)];
while(i < str.Length)
{
bytes[j] = Byte.Parse(sBytes.Substring(i,2), NumberStyles.HexNumber);
i += 2;
}


byte[] bytes = new byte[str.Length / 2];
for(int i = 0; i < str.Length / 2; i++)
bytes [i] = Byte.Parse(str.Substring(i * 2, 2), NumberStyles.HexNumber);

works fine... THX

But I wonder why there's no such function like ToCharArray for string object which produces byte array. In many cases functions from .NET Framework uses byte arrays, not char arrays :(

Try this:

// C# to convert a string to a byte array.
public static byte[] StrToByteArray(string str)
{
System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding();
return encoding.GetBytes(str);
}

Good luck!

Newbie


I have tried this

System.Text.

ASCIIEncoding encoding =new System.Text.ASCIIEncoding();string queryStringKey =ConfigurationManager.AppSettings["queryStringKey"];byte[] key = encoding.GetBytes(queryStringKey);return key;

to achieve this

//byte[] Key = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6 };

but it is not working. What am I not getting? Newbie

Wednesday, March 28, 2012

String.Format comma for thousands and two decimal points only

I am trying to come up with theString.Format expression to format a number that will have a comma to separate the thousands and a period for the decimals ( I want to show only two decimals )

Will this work for you?

String.Format("{0:N2}", MyNumber)

Monday, March 26, 2012

String/Number manip.

In the following line:
txtWage.Text = "$" & Trim(dataReader.GetValue(9).ToString)

It returns $20.0000 I only want it to return back $20.00 -- every search I've done in string manip. has shown me zilch.

Any suggestions?

txtWage.Text = Format(CDbl(Trim(dataReader.GetValue(9).ToString)), "currency")
I think you can use String.Format also to do the same thing, with the format of {0:c}

Saturday, March 24, 2012

Strip characters from string

Hi, I want the user to be able to enter any form of number in my price textbox and I will automatically strip anything that is not an integer.

I obviously can't mand a string.replace for every character, so is there a way to use a regexp to say anything that isnt 0-9?

Thanks, Davehello, u can add a validation control to the textbox with the following:

[0-9]+

this way, they can enter any number they want.
But I don't want to have to restrict it like that. Many people will enter different formats for a price. I want them to be able to use '$' or ',' or whatever and have it stripped on the backend.

I am looking at regex.replace, but I can't get it to work yet.
well,
maybe u can allow them to enter any number or character and the loop through an array of all alphabets and eleminate all not needed characters !!!
I can offer you a commercial solution. My productProfessional Validation And More includes IntegerTextBox, CurrencyTextBox, and DecimalTextBox fields. They handle the filtering of illegal keystrokes, reformatting as the user exits the field, and supply you with the actual numeric value (an integer or Double property) on the server side.

strip non-numeric characters

Is there simple way to take a string that should be all numeric (like a
credit card number) and strip out anything that isn't a digit? In the past
I've done this in VFP using the IsDigit() function, looping through each
character. It was a little awkward, but worked. Is there something similar,
or better for vb.net?

Thanks!

MattThe IsNumeric function should fill the bill...

Dim MyVar As Object
Dim MyCheck As Boolean
' ...
MyVar = "53" ' Assign value.
MyCheck = IsNumeric(MyVar) ' Returns True.
' ...
MyVar = "459.95" ' Assign value.
MyCheck = IsNumeric(MyVar) ' Returns True.
' ...
MyVar = "45 Help" ' Assign value.
MyCheck = IsNumeric(MyVar) ' Returns False.

Regards,
January Smith

"MattB" <somedudeus@.yahoo.com> wrote in message
news:2hmemqFen4c9U1@.uni-berlin.de...
> Is there simple way to take a string that should be all numeric (like a
> credit card number) and strip out anything that isn't a digit? In the past
> I've done this in VFP using the IsDigit() function, looping through each
> character. It was a little awkward, but worked. Is there something
similar,
> or better for vb.net?
> Thanks!
> Matt
I would try Regex. You can look for \d and pull all instances, then concat
back the resulting array. There are a couple of other ways to work with
this.

Note that VB.NET still has IsNumber() or IsNumeric() [forget which one], so
you can still loop and test, if you want to go with VB.NET.

Another option is convert to a char array and test the numeric value of each
char. The ASCII value for numbers is extremely predictable.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
"MattB" <somedudeus@.yahoo.com> wrote in message
news:2hmemqFen4c9U1@.uni-berlin.de...
> Is there simple way to take a string that should be all numeric (like a
> credit card number) and strip out anything that isn't a digit? In the past
> I've done this in VFP using the IsDigit() function, looping through each
> character. It was a little awkward, but worked. Is there something
similar,
> or better for vb.net?
> Thanks!
> Matt
You can use the Regex.Replace method to check for any characters (\w) and replace them with an empty string. The result that would come out is a string that is all numeric.

strip non-numeric characters

Is there simple way to take a string that should be all numeric (like a
credit card number) and strip out anything that isn't a digit? In the past
I've done this in VFP using the IsDigit() function, looping through each
character. It was a little awkward, but worked. Is there something similar,
or better for vb.net?
Thanks!
MattThe IsNumeric function should fill the bill...
Dim MyVar As Object
Dim MyCheck As Boolean
' ...
MyVar = "53" ' Assign value.
MyCheck = IsNumeric(MyVar) ' Returns True.
' ...
MyVar = "459.95" ' Assign value.
MyCheck = IsNumeric(MyVar) ' Returns True.
' ...
MyVar = "45 Help" ' Assign value.
MyCheck = IsNumeric(MyVar) ' Returns False.
Regards,
January Smith
"MattB" <somedudeus@.yahoo.com> wrote in message
news:2hmemqFen4c9U1@.uni-berlin.de...
> Is there simple way to take a string that should be all numeric (like a
> credit card number) and strip out anything that isn't a digit? In the past
> I've done this in VFP using the IsDigit() function, looping through each
> character. It was a little awkward, but worked. Is there something
similar,
> or better for vb.net?
> Thanks!
> Matt
>
I would try Regex. You can look for \d and pull all instances, then concat
back the resulting array. There are a couple of other ways to work with
this.
Note that VB.NET still has IsNumber() or IsNumeric() [forget which one],
so
you can still loop and test, if you want to go with VB.NET.
Another option is convert to a char array and test the numeric value of each
char. The ASCII value for numbers is extremely predictable.
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
****************************************
********
Think Outside the Box!
****************************************
********
"MattB" <somedudeus@.yahoo.com> wrote in message
news:2hmemqFen4c9U1@.uni-berlin.de...
> Is there simple way to take a string that should be all numeric (like a
> credit card number) and strip out anything that isn't a digit? In the past
> I've done this in VFP using the IsDigit() function, looping through each
> character. It was a little awkward, but worked. Is there something
similar,
> or better for vb.net?
> Thanks!
> Matt
>
You can use the Regex.Replace method to check for any characters (\w) and re
place them with an empty string. The result that would come out is a string
that is all numeric.