ASG
IBM
Zystems
Cressida
Icon
Netflexity
 
  MQSeries.net
Search  Search       Tech Exchange      Education      Certifications      Library      Info Center      SupportPacs      LinkedIn  Search  Search                                                                   FAQ  FAQ   Usergroups  Usergroups
 
Register  ::  Log in Log in to check your private messages
 
RSS Feed - WebSphere MQ Support RSS Feed - Message Broker Support

MQSeries.net Forum Index » IBM MQ API Support » converting messageid from byte to string

Post new topic  Reply to topic
 converting messageid from byte to string « View previous topic :: View next topic » 
Author Message
btdo
PostPosted: Tue Jul 19, 2005 6:53 am    Post subject: converting messageid from byte to string Reply with quote

Novice

Joined: 03 Jun 2005
Posts: 19

Hello
I'm trying to convert message id from byte to string. Then from the string I would convert back to byte and put it in the correlation id to get the response message

Here are my first two functions to convert from byte to string and string to byte

Public Shared Function StrToByteArray(ByVal str As String) As Byte()
Dim encoding As New System.Text.ASCIIEncoding

Return encoding.GetBytes(str)
End Function 'StrToByteArray

Public Shared Function BytetoStr(ByVal byte_array As Byte()) As String
Dim str As String
Dim enc As New System.Text.ASCIIEncoding
str = enc.GetString(byte_array)
Return str
End Function

My second try with two new functions

Public Shared Function ConvertStringToByteArray(ByVal stringToConvert As String) As Byte()

Return (New UnicodeEncoding).GetBytes(stringToConvert)

End Function

Public Shared Function ConvertByteToString(ByVal byteToConvert() As Byte) As String

Return (New UnicodeEncoding).GetString(byteToConvert)

End Function

I also get a bunch of squares for the string that was converted from byte.

I am just wondering if you have any suggestions on how I can convert from byte to string and receive some normal standard characters for MQ.
Back to top
View user's profile Send private message
jefflowrey
PostPosted: Tue Jul 19, 2005 6:57 am    Post subject: Re: converting messageid from byte to string Reply with quote

Grand Poobah

Joined: 16 Oct 2002
Posts: 19981

btdo wrote:
I am just wondering if you have any suggestions on how I can convert from byte to string


DON'T!

Search here for any of ten million posts that explain why you should not do this.

Also, you should go back and review the MQAX and .NET API, as there's already a simple way to get a printable version - for example the property MessageIdHex.

But you should not convert, and then convert back when setting the output property. Just copy the bytes as is.
_________________
I am *not* the model of the modern major general.
Back to top
View user's profile Send private message
btdo
PostPosted: Tue Jul 19, 2005 7:08 am    Post subject: solution Reply with quote

Novice

Joined: 03 Jun 2005
Posts: 19

I've found the solution. Thanks anyway

I used these two methods

Imports Microsoft.VisualBasic

Public Shared Function byte2String(ByVal input() As Byte) As String
Dim charHelper(23) As Char
Dim returnStr As String

'--copy the byte array to the char array
input.CopyTo(charHelper, 0)

'--put the char array to the string
For Each singleton As Char In charHelper
returnStr = returnStr + singleton.ToString
Next

Return returnStr
End Function

Public Shared Function string2Byte(ByVal input As String) As Byte()
Dim charHelper(23) As Char
Dim byteArray(23) As Byte

'--copy the string into the char array
charHelper = input.ToCharArray

'--copy the char array to the byte array
For idx As Integer = 0 To 23
byteArray(idx) = AscW(charHelper(idx))
Next

Return byteArray
End Function
Back to top
View user's profile Send private message
fjb_saper
PostPosted: Tue Jul 19, 2005 1:00 pm    Post subject: Reply with quote

Grand High Poobah

Joined: 18 Nov 2003
Posts: 20756
Location: LI,NY

Again read jeffs post and understand the "between the lines"

You are setting yourself up for a world of grief.

There simply is not way to go from a true byte[] to a string.
You have to represent every byte in hex value.

A char[] or String can be represented by a byte[] but the reverse is NOT always true. There are circumstances where this will lead you into trouble.

Enjoy
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic  Reply to topic Page 1 of 1

MQSeries.net Forum Index » IBM MQ API Support » converting messageid from byte to string
Jump to:  



You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Protected by Anti-Spam ACP
 
 


Theme by Dustin Baccetti
Powered by phpBB © 2001, 2002 phpBB Group

Copyright © MQSeries.net. All rights reserved.