Why Encoding.Default.GetBytes() Returns Different Results In C#

May 29, 2009

We recently came across some sample code from a vendor for hashing a secret key for a web service call, their sample was in VB.NET which we converted to C#. This caused the hashing to produce different input. It turns out the way they were generating the key for the encryption was by converting a char array to a string and back to a byte array. This led me to the discovery that VB.NET and C#'s default encoder work differently with some characters.

C#:
Console.Write(Encoding.Default.GetBytes(new char[] { (char)149 })[0]);

VB:
Dim b As Char() = {Chr(149)}
Console.WriteLine(Encoding.Default.GetBytes(b)(0))

The C# output is 63, while VB is the correct byte value of 149.if you use any other value, like 145, etc, the output matches.Walking through the debugging, both VB and C# default encoder is SBCSCodePageEncoding.I have corrected the sample code by directly initializing a byte array, which it should have been in the first place, but I still want to know why the encoder, which should not be language specific, appears to be just that.

View 5 Replies


ADVERTISEMENT

EnCoding Message To DDE Client Using System.Text.Encoding.ASCII.GetBytes (message)?

Oct 28, 2009

I am sending a DDE message to a client using System.Text.Encoding.ASCII.GetBytes(item) . However, before the message is actually sent, I would like to get the message coded where if the item="Ask" then item=Ask (string variable) and so on. The code is:

Protected Overrides Function OnRequest(ByVal conversation As DdeConversation, ByVal item As String, ByVal format As Integer) As RequestResult
' Return data to the client only if the format is CF_TEXT

[code]....

View 3 Replies

What Is The 'whitespace' ASCII Or Unicode Character In System.Text.Encoding.ASCII.GetBytes (whitespace)

Nov 4, 2009

I would like to pass a whitespace character by using the System.Text.Encoding.ASCII.GetBytes(" "), and System.Text.Encoding.Unicode.GetBytes(" ") , where " " is the whitespace Character required. What do I need to type (" ") to get a whitespace character passed.?

View 6 Replies

.net - What Is The Default Encoding For A String

Apr 27, 2012

I have a simple question about string. Consider following code:

Dim S1 as String = "abc"

What is the encoding for S1? Is that UTF-8 or depending on user windows local setting?

View 2 Replies

Change Default Encoding To ASCII?

Dec 9, 2010

I'm using this to write to a text file[code]..

but i need to change the encoding from the default. how do i change the default encoding to ASCII?

system.text.encoding?

everything in outputText is written as a string and then concatenated into outputText.

View 8 Replies

Change Default Encoding Of Assembly In Program?

Jun 1, 2009

How to change the default encoding of assembly in VS2008?

I have upgraded vb.net windows (VS2003) to VS-2008.

Whenever I re-builds the project,it is changing the assembly encoding to Windows-1252,which is not running my application.

I have to manual set this encoding to UTF-8,which runs the application.[code]...

View 1 Replies

Change The Default Encoding Of Assembly In VS2008?

Jun 1, 2009

How to change the default encoding of assembly in VS2008? I have upgraded vb.net windows (VS2003) to VS-2008. Whenever I re-builds the project,it is changing the assembly encoding to Windows-1252,which is not running my application. I have to manual set this encoding to UTF-8,which runs the application. Here is a assembly,

[Code]...

View 3 Replies

Default Encoding For Microsoft Winsock Control 6.0 In VB 6

Jun 13, 2009

I am developing a Door Access Control System which talks the hardware's firmware via TCP/IP. The first version was a success and done using VB6 Winsock Control 6.0. Currently, I am doing the second version in VB.NET and the pain is with the socket. Winsock Control 6.0 is such a blessing (finally admitted) Does anyone know the default encoding of Winsock Control 6.0 in VB6 so that I can set it exactly to the encoding for socket in VB.NET. ANSI (VB.NET) seems to work (but not fully tested with the hardware), but is there something more specific?

View 8 Replies

Windows - Default Encoding For System.IO.File.ReadAllText?

Jul 26, 2011

if we don't mention the decoding what decoding will they use?I do not think it's System.Text.Encoding.Default. Things work well if I EXPLICITLY put System.Text.Encoding.Default but things go wrong when I live that empty.So this doesn't work well

Dim b = System.IO.File.ReadAllText("test.txt")
System.IO.File.WriteAllText("test4.txt", b)

[Code]...

View 2 Replies

GetCurPos Returns Different Results?

Oct 28, 2010

Why this api in VB.net does not return right results. in my pc the X,Y results are ndefined.

Code:
Imports System.Runtime.InteropServices
Module Module1

[code].....

View 3 Replies

Simple Encryption In C# And VB Returns Different Results

Dec 9, 2011

I have an application that encrypts data which is written in VB. There is another application that uses the same data. Encryption code is the same but it returns different result in some cases. [Code]

View 2 Replies

Xpath Query On An XML Returns 0 Results

Nov 13, 2011

I've converted an html to XML and now i want to get all the child nodes nested
within a DIV element with a specific attribute (class="itemInfo").

Because the html that the XML is based on changes from time to time i want to use a more "safe" relative root,[code...]

Problem is that i always get 0 nodes ...I'm sure that something is wrong with my Xpath syntax, just don't know what it is. Here is my code'[code...]

View 7 Replies

Check If WQL __InstanceCreation Query Returns Results?

Apr 20, 2012

how can write code that can check if __InstanceCreation query has returned rows or not. I'm using this code to check for the creation of a file

Private Function CheckForFileCreation() As Boolean
Try
Dim query1 As New WqlEventQuery( _

[Code]....

so I need to know if the query returned > 0 rows, if it did then the function will return true, if the query returned 0 rows then the function should return false. Sorry I am a complete noob with WMI and WQL

View 2 Replies

VS 2010 : Xpath Query On A XML Returns 0 Results?

Nov 13, 2011

I've converted an html to XML and now i want to get all the child nodes nested within a DIV element with a specific attribute (class="itemInfo").Because the html that the XML is based on changes from time to time i want to use a more "safe" relative root,
(something like : "//div[@class='itemInfo']").Problem is that i always get 0 nodes ...

Dim doc As New System.Xml.XmlDocument()
doc.Load("products.xml")
Dim root As System.Xml.XmlElement = doc.DocumentElement

[code]....

View 6 Replies

Query Performed Returns Results In Reverse For Some Options?

Apr 6, 2010

I am trying to perform a query on an Access 2007 Database Using Visual Basic 2008. My database has a table tblStudents contains a field named Gender with Male/Female options. In VB 2008 I set up a form with a Combo box where the user can select either Male or Female to perform a query and navigate through the returned results. When i choose the 1st option in the Combo box the results are returned in the order. However when i choose the 2nd option in the combo box, i get the results in reverse order which i still could not find the reason why it does that. Here is my Code for the query and the Execute Query Button:

Private Sub DisplayGenderData(ByVal gender As String)
connUsers.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:StudentRecords.accdb;Jet OLEDB:Database Password=testing;"
' Open the connection.

[Code].....

View 4 Replies

VS 2010 Reading Remote Registry With WMI Returns No Results

Feb 9, 2012

I am trying to read the HKEY_USERS hive of a remote machine with this Class

Imports System.Management
Imports System.Management.Instrumentation
Public Enum RegHive As UInteger

[Code]....

The value of sSubKeyName is the user's SID + "" + Network (e.g. S-1-5-21-1606980848-2025429265-839522115-560021Network). Now under that key are various subkeys containing mapped drive letters. But it never returns anything.

Strangely, if I use the OpenRemoteBaseKey method it works BUT I don't want to use that because using WMI lets me specify alternate credentials and OpenRemoteBaseKey doesn't

View 1 Replies

Asp.net - LDAP User Attribute Request Returns Unusual Results?

May 31, 2012

I'm struggling to return user details from AD using LDAP, after i have authenticated that the user exists. I am using a simple auth method as follows:

Function AuthenticateUser(path As String, user As String, pass As String) As Boolean
Dim de As New DirectoryEntry(path, user, pass, AuthenticationTypes.Secure)
Try
Dim ds As DirectorySearcher = New DirectorySearcher(de)
Dim result As SearchResult = ds.FindOne()

[Code]...

the problem is that "distinguishedName" returns "DC=our-domain,DC=co,DC=uk" and "name" returns just "our-domain", not the name of the user that has just been auth'ed

Note: the displayName.text outputs are purely for debug purposes.I have tried various combos of requests but nothing seems to return USER details. ETA: to the security police: this is all within a https connection, I'm not sending passwords about in plain text!

View 1 Replies

App Returns To Default Settings When In New Area?

Jan 15, 2012

My Program changes it's My.settings to default , it looses the Location Where it is saved This only happens when i move to a new area But if i take a new builded exe and put it in same Dir of old exe then It Remembers old Settings?

View 7 Replies

.net - GetPrivateProfileString() Returns Empty String Despite Having A Default?

Jul 9, 2010

Private Declare Function GetPrivateProfileString Lib "kernel32" Alias _

"GetPrivateProfileStringA" (ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Integer, ByVal lpFileName As String) As Integer
Dim value As String = ""
Dim length As Integer
Dim IniFileName As String

GetPrivateProfileString("Config", "UserName", "None", value, length, IniFileName)

but value contains an empty string I confes to being a VB n00b, less than a week, in fact, but I can't see what's wrong there. The file exists, it contains a section called "Config" which has an entry called "UserName" with a value - but even if not, wouldn't value take the default?

Edit: It's not returning an empty string - it's returning whatever I initialize value to before calling GetPrivateProfileString().

Which is to say that if I Dim value As String = "xxx" then it stil contains "xxx" after the call and not the default value.

View 4 Replies

GetPrivateProfileString() Returns Empty String, Despite Having A Default?

Oct 25, 2009

Private Declare Function GetPrivateProfileString Lib "kernel32" Alias _
"GetPrivateProfileStringA" (ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _

[code].....

View 5 Replies

Catch Block In Text.vb And Display The Results In Default.aspx?

Feb 26, 2010

Catch block in text.vb and display the results in default.aspx

View 5 Replies

C# - NHibernate - Default Implementation Of QueryOver Use Some Sort Of Caching When The Results Are Returned?

Dec 15, 2010

I am developing a windows forms smart client that uses Fluent Nhibernate 1.1 and NHibernate 3.0 for persistence. Due to legacy code I have had no option but to create a single session that handles all client level CRUD operations. This tends to leave the Nhibernate session in a stale state. So I have made the decision to use QueryOver to eliminate this stale data issue, as I was under the impression that query results were not cached. This doesn't seem to be the happening as the results returned from NHibernate seem to be cached. My question is does the default implementation of QueryOver use some sort of caching when the results are returned?

View 1 Replies

C# - Use Flush Parameter With Encoder.GetBytes Method?

Oct 4, 2010

This link explains the Encoder.GetBytes Method and there is a bool parameter called flush explained too . The explanation of flush is : true if this encoder can flush its state at the end of the conversion; otherwise, false. To ensure correct termination of a sequence of blocks of encoded bytes, the last call to GetBytes can specify a value of true for flush.but I didn't understand what flush does?

View 3 Replies

C# - What 'length' Parameter Should I Pass To SqlDataReader.GetBytes()?

Feb 7, 2011

I have a SqlDataReader and need to read a varbinary(max) column from it using the SqlDataReader.GetBytes() method. This method populates a byte array and therefore needs to know what length of data to read.This is where I get confused.. Clearly I want to read all the data that has been returned from the database in this row/column so what 'length' parameter should I pass?As far as I can see, the SqlDataReader doesn't provide any methods to discover what length of data is available, therefore this method seems fairly awkward to me.I'm tempted to just pass int.MaxValue here and forget about the issue but something about this doesn't sit right with me.

I am aware that I can instead call

byte[] value = (byte[])dataReader["columnName"];

.. and this seems to completely take care of the length issue internally. However I am working with a set of complicated code generation templates that have been built around the SqlDataReader.GetXXXX() methods. So I am tied into using GetBytes and need to understand its proper usage.

View 7 Replies

C# - What 'length' Parameter Should Pass To SqlDataReader.GetBytes()

Jun 21, 2012

I have a SqlDataReader and need to read a varbinary(max) column from it using the SqlDataReader.GetBytes() method. This method populates a byte array and therefore needs to know what length of data to read.This is where I get confused.. Clearly I want to read all the data that has been returned from the database in this row/column so what 'length' parameter should I pass?As far as I can see, the SqlDataReader doesn't provide any methods to discover what length of data is available, therefore this method seems fairly awkward to me.I'm tempted to just pass int.MaxValue here and forget about the issue but something about this doesn't sit right with me.

View 2 Replies

Generic BitConverter.GetBytes For Primitive Data Types?

Jan 19, 2012

addressing the need for getting the bytes of an object. But I am wondering if there is an approach to calling BitConverter.GetBytes on a generic type where I know the type is a primitive (Int32, UInt16, etc).

Public Sub Foobar(Of T as Structure)()
Dim x as T 'Assume T is declared as Int32
Dim y() as Byte
y = System.BitConverter.GetBytes(x)
End Sub

The above will throw your usual error:

Overload resolution failed because no accessible 'GetBytes' can be called with these arguments:
'Public Shared Function GetBytes(value As Double) As Byte()': Value of type 'T' cannot be converted to 'Double'.
'Public Shared Function GetBytes(value As Single) As Byte()': Value of type 'T' cannot be converted to 'Single'.

[code]....

One solution I think would work is a large Select Case calling GetType(), but that is horrendously slow (because of boxing) and looks ugly. I would think that since I call my higher level class with a primitive data type for T, that the compiler would be smart enough to figure it out, but I assume I am not providing enough information for it to derive what T's underlying value is at compile time for the invoked instances.

View 6 Replies

Show The Final Results Instead Of The Results Real-time

Sep 28, 2010

I have a form that allows users to select file and then it reads the contents, parses the data and then executes a sql insert statement to add it to a database. What I am having issues with is showing real-time results. Currently, I have the import operation take place on the import form within the OnLoad event. The problem with this is it only shows the final results instead of the results real-time. Is there anyway to do this without creating a seperate thread and delegates?

View 1 Replies

Null Check Always Returns Null, If Removed Returns Object Reference Not Set To An Instance Of An Object

Jun 24, 2010

I have some code which gets child items for a menu via the GetChildren function which takes a list of menuData: Dim builtMenu As New List(Of MenuData)(_rawData.FindAll(Function(item) item.GroupingID = 0))

For Each menuData As MenuData In builtMenu
If menuData.Children IsNot Nothing Then
menuData.Children.AddRange(GetChildren(menuData))
End If
Next

If I check if menudata.children isnot nothing, it always is nothing because the GetChildren function is yet to run (providing the child items, which do exist). If I remove this check and just have this code:

Dim builtMenu As New List(Of MenuData)(_rawData.FindAll(Function(item) item.GroupingID = 0))

For Each menuData As MenuData In builtMenu
menuData.Children.AddRange(GetChildren(menuData))
Next

Then I am presented with a Object reference not set to an instance of an object error on menuData.Children.AddRange(GetChildren(menuData))

View 1 Replies

User To Select Just Straight To Default Printer With Its Default Settings

Dec 3, 2011

I need to print the contents of 3 textboxs to default printer. what examples I have found dont seem to work. I was trying the documentprint1 component as i thought it would be easy but cant figure out how to tell it what to print the second part is to print a variable (string) to the printer as well.no need for user to select just straight to default printer with its default settings

View 1 Replies

VS 2010 Combobox Default Value - Display The Name Of The Person Who Is Doing The Selection As The Default?

Nov 25, 2011

I have a combobox which contains a sample of names selected from a table.Currently the default name displayed in the box is the 1st name found in the table. What I would like to do is display the name of the person who is doing the selection as the default. The name is stored in a public variable 'PubName' and I do not want the user to be able to type into the combobox themselves.Is this possible?

View 2 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved