1

I currently have a Hex value but it has an odd length. I used a file to byte[] code and it both produced an odd character length of a hex. After the value is in the Database, I copy the Hex value because I need that for a migration to apply to a bunch of other databases. I can't convert Hex to Byte[] because of the odd length. Any suggestions to jump over this hurdle? Is there a Hex to Byte[] converter that can handle odd number of lengths?

Encoder one

var fileStream = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read);
var bytes = new byte[fileStream.Length];

fileStream.Read(bytes, 0, Convert.ToInt32(fileStream.Length));
fileStream.Close();

UnitOfWork.FileRepository.Add(new ObjectModel.File.File
{
    FileArray = bytes,
    FileName = Path.GetFileName(openFileDialog.FileName),
    FileExtension = Path.GetExtension(openFileDialog.FileName),
    /InteractionId = _interaction.Id,
);

UnitOfWork.Save(User.Id);
Master
  • 2,038
  • 2
  • 27
  • 77
  • define "odd hex length" ? – Fredou Jun 19 '15 at 16:33
  • `0x123AS4A34A34...... ` A count of those characters total's to 43677 excluding 0x. – Master Jun 19 '15 at 16:34
  • what is the column type in the database? – Fredou Jun 19 '15 at 16:35
  • The code you posted is a buggy version of `File.ReadAllBytes(filename)`. Also: You actually want to use strings when dealing with hex values. Have a look at `ToUInt64(string value, int fromBase)` or `UInt64.TryParse(String, NumberStyles, IFormatProvider, UInt64)`. – Zotta Jun 19 '15 at 16:35
  • @Master please edit all relevant clarifications into your question. – ryanyuyu Jun 19 '15 at 16:35
  • As per CodesInChaos's comment [on your previous question](http://stackoverflow.com/questions/30942309/converting-a-very-long-hex-string-to-byte-array): _"...if the string has an odd length you should fix that. But we can't tell you how, since that depends on how the encoder messed it up"_ – James Thorpe Jun 19 '15 at 16:35
  • @Fredou It's varbinary(MAX) – Master Jun 19 '15 at 16:36
  • if it is a varbinary, can you show us the code used to read it? there is a bad conversion if you get a string – Fredou Jun 19 '15 at 16:38
  • @Fredou well the entry in sql database and I right clicked and copied it. There's no sql reader for this since I only need the array value. – Master Jun 19 '15 at 16:39
  • in that case it is a duplicate of http://stackoverflow.com/questions/311165/how-do-you-convert-byte-array-to-hexadecimal-string-and-vice-versa – Fredou Jun 19 '15 at 16:41
  • I have the conversion codes, the conversion codes won't work because the Hex character Length is odd. – Master Jun 19 '15 at 16:42
  • show us the code used to save it in the database – Fredou Jun 19 '15 at 16:44
  • Hi @Fredou I've Updated it. – Master Jun 19 '15 at 16:48
  • what orm are you using and can you show us the code under Save() ? – Fredou Jun 19 '15 at 16:49
  • My save points to my DbContext and triggers base.SaveChanges(); from Entity Framework – Master Jun 19 '15 at 16:53

1 Answers1

1

Since your doing a copy/paste from microsoft sql server management studio there is a maximum length that it can show in a result. You will have to use another tool to do a direct copy/paste of the sql database value.

or go in Tools -> Options -> Query Results -> Sql Server -> Result to Grid

change the Maximum Characters Retrieved for XML data to Unlimited and change your sql query to add for xml path('') at the end

it will generate a xml file in the result grid, click on it and copy/paste the hex value from there

Fredou
  • 19,848
  • 10
  • 58
  • 113