VB.net - convert PDF bytes to string - vb.net

I have the below response in a json format, from an API request of printing something and it contains PDF bytes:
%PDF-1.7 %??5 0 obj <</Filter/FlateDecode/Alternate/DeviceRGB/Length 2592/N 3>>stream x???wTS???7??" %? ?H?. ! BB?+?#??4E?A??Q,??O?ADG??F?y??g}k????.
The idea is that I need to convert that into a string using VB and I wasn't able to find something that would help me on the web.
Can someone give me a hint on how to do this? Thanks

Use Base64 methods to convert byte[] to String.
Dim b As Byte() = Convert.FromBase64String(pdfByte)
Dim finalString As String = System.Text.Encoding.UTF8.GetString(b)
btw. I don't know how the json response will be created but there is something wrong. You can not get anything useful out of x???wTS???7??" %? ?H?. ! BB?+?#??4E?A??Q,
??O?ADG??F?y??g}k????.
There is a problem on the other site. It should convert the bytes to a Base64 String and then it will work.

Related

possible to get Pdf fonts encoding value?

Adobe Encoding: Build-In
I would like to get the encoding value : 'Build-in', currently i am using iTextSharp, seem like don't have any straight forward way to get the encoding value.
The only way i find to get font encoding value
Dim fonts As PdfDictionary = resources.GetAsDict(PdfName.FONT)
Dim fontEncoding As String = font.GetAsName(PdfName.ENCODING).ToString().Substring(1)
but this way unable to get some encoding value.
Such as encoding value (Build-in & Custom) font.GetAsName(PdfName.ENCODING) will return Nothing. For Ansi able to get value 'WinAnsiEncoding'

Sabre Web/ .NET - Special Characters In SabreCommandLLSRQ Response Not Handed Properly

I'm using VB.NET to consume Sabre Web Services, primarily using SabreCommandLLSRQ to send native Sabre commands. Sending special characters without any special encoding works fine, but when I try to manipulate any response that contain the Cross of Lorraine using the Response element of SabreCommandLLSRS all of the Cross of Lorraine chars are missing if I display my string in a MsgBox or try to manipulate it.
If I push that string into my clipboard and view it in Notepad++, the characters are there but they seem to be encoded improperly - they come through as something like "‡". I'm pretty new to unicode encoding so that's all a bit above my head.
I've tried using the Replace method of String Builder to change those characters to something visible no avail - anyone have a way around this issue?
Strangely, the other special characters (e.g. "¤") seem to come through just fine.
This section in Dev Studio includes references to special character hex codes:
https://developer.sabre.com/docs/read/soap_apis/management/utility/Send_Sabre_Command
Does this help?
This is a pain in the behind due to the invisible characters.
String replace does work you just need to make sure you capture the invisible character after the Â
Simply in the SabreCommandSend function before you send the string to Sabre put something like the below.
Hopefully this should copy and paste straight out including the invisible character.
if (tempCommand.Contains("‡"))
{
tempCommand = tempCommand.Replace("‡", "Â");
}
I figured out how to get this to work, but its not pretty so if anyone has a better way to do it, I'm all ears.
I couldn't figure out what char to use to do the simple string Replace method, so instead I'm casting the string to a byte array, iterating through the array and replacing any strange characters I find, recasting the byte array into a raw string and doing the string replace on that:
Imports System.Text
Dim byteArray() As Byte = System.Text.Encoding.ASCII.GetBytes(sabreResponse)
For i = 0 To byteArray.Length - 1
If byteArray(i) = 63 Then 'this is a question mark char
byteArray(i) = 94 'caret that doesn't exist in native Sabre
End If
Next
MyClass.respString = System.Text.ASCIIEncoding.ASCII.GetString(byteArray)
MyClass.respString = MyClass.respString.Replace("^", "¥")
For whatever reason, the string replace method works after I swap out the offending byte with a dummy character but not before.

VB.NET 2010 - Chr() Function of another code page

When I use function Chr(225), I get character "á", because code page of Windows is 1250 (System.Globalization.CultureInfo.CurrentCulture.TextInfo.ANSICodePage)
Is it possible to use Chr(225), but get character of another code page?
For example code 225 represents in code page DOS-852 character "ß".
I need to convert "á" to "ß".
Is it possible to get character of DOS code page 852?
For example Chr(225) should return "ß".
Thanks!
You can get an Encoding for a specific code page, 852 in your case:
Dim enc = Text.Encoding.GetEncoding(852)
Dim str = enc.GetString(New Byte() {225})
Have a look at the Encoding class in general for conversions between text encodings.

How to convert UnicodeEncoding output into plain string?

Hi I'm currently converting a delphi code into .net
they have this encryption in their database I have decrypted but the problem is
the output is in byte array.
heres what I've done so far
Private Function EnDeCrypt(ByVal Value As String) As String
Dim transformed = Encoding.Unicode.GetBytes(Value).Select( _
Function(item) Not item)
Return Encoding.Unicode.GetString(transformed.ToArray())
End Function
Result : º»¯³¶½ = e d p l i b(unprintable chars)
my problem is how to convert the output to string
Suggestions and help are greatly appreciated
The short answer is that if your encode algorithm really is bitwise negation of a UTF-16 string, then you cannot print the output. The algorithm produces un-printable output.
You could add an extra encoding to base64 at display time to make the negated bytes, transformed in your code, displayable.
If you want more precise help I think you will need to explain the context and what your motivations are.

How do I get the length (i.e. number of characters) of an ASCII string in VB.NET?

I'm using this code to return some string from a tcpclient but when the string comes back it has a leading " character in it. I'm trying to remove it but the Len() function is reading the number of bytes instead of the string itself. How can I alter this to give me the length of the string as I would normally use it and not of the array underlying the string itself?
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
' Output the data received from the host to the console.'
Dim returndata As String = Encoding.ASCII.GetString(bytes)
Dim LL As Int32 = Len(returndata)
Len() reports the number of bytes not the number of characters in the string.
Your code is currently somewhat broken. The answer is tcpClient.ReceiveBufferSize, regardless of how much data you actually received - because you're ignoring the return value from networkStream.Read. It could be returning just a few bytes, but you're creating a string using the rest of the bytes array anyway. Always check the return value of Stream.Read, because otherwise you don't know how much data has actually been read. You should do something like:
Dim bytesRead = networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
' Output the data received from the host to the console.'
Dim returndata As String = Encoding.ASCII.GetString(bytes, 0, bytesRead)
Now, ASCII always has a single character per byte (and vice versa) so the length of the string will be exactly the same as the length of the data you received.
Be aware that any non-ASCII data (i.e. any bytes over 127) will be converted to '?' by Encoding.ASCII.GetString. You may also get control characters. Is this definitely ASCII text data to start with? If it's not, I'd recommend hex-encoding it or using some other option to dump the exact data in a non-lossy way.
You could try trimming the string inside the call to Len():
Dim LL As Int32 = Len(returndata.Trim())
If Len reports the number of bytes and it doesn't match the number of characters, then I can think of two possibilities:
There are more chars being sent than you think (ie, that extra character is actually being sent)
The encoding is not ASCII, so there can be more than one byte per char (and one of them is that 'weird' character, that is the character is being sent and is not 'wrong data'). Try to find out if the data is really ASCII encoded, if not, change the call accordingly.
When I read you correctly, you get a single quotation mark at the beginning, right?
If you get that one consistently why not just subtract one from the string length? Or use a substring from the second character:
Len(returndata.Substring(1)
And I don't quite understand what you mean with »the length of the string as I would normally use it and not of the array underlying the string itself«. You have a string. Any array which might represent that string internally is entirely implementation-dependent and nothing you should see or rely on. Or am I getting you wrong here. The string is what you are using normally. I mean, if that's not what you do, then why not take the length of the string after processing it into something you would normally use?
Maybe I am missing something here, but what is wrong with String.Length?
Dim LL As Int32 = returndata.Length