How to convert ByteArray to String?

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default How to convert ByteArray to String?

    We are using SQL Server, one of the columns is ByteArray datatype, when I tried
    to output it with the <cfoutput> tag, I got ' ByteArray objects cannot be
    converted to strings' error. I tried the tostring function also with the
    tobase64 function, still it doesn't convert the byteArray correctly into a
    string. Any idea? Thanks in advance.

    gmu Guest

  2. Similar Questions and Discussions

    1. Put String into a ByteArray
      Hi: I have a String variable with the result of an SQL 2005 varbinary field. Can anybody tell me how to put this information into a ByteArray...
    2. ByteArray object to string
      Hi, I am trying to connect to an AS400 database. The query runs fine and returns values . But when I try and display some of the values ,I get an...
    3. convert string to safe string before adding to database
      Hi I am updating a site for a client who is running CF5. The update and insert queries break when the user uses inverted commas ". I currently...
    4. Convert String to Int then divide
      Here is my code: <cfdirectory directory = "f:\testbed\documents\" name = "myDirectory" sort = "name ASC, size DESC"> <!--- Output the contents...
    5. How to convert string to float?
      i want to covert a querystring to float number in asp. what function should i use? i know cint() can convert string to int but how about float?
  3. #2

    Default Re: How to convert ByteArray to String?

    Sorry, the data type is varbinary, but I do get the 'ByteArray objects cannot
    be converted to strings'' error, I have tried anything, tried toString() and
    tobase64(), also CONVERT function inside the SQL statement, still nothing is
    working. Any one can help? Many thanks.

    gmu Guest

  4. #3

    Default Re: How to convert ByteArray to String?

    How about CAST(varbinary_colum AS VARCHAR) within your SQL?

    SELECT CAST(column_name AS VARCHAR) AS new_col_name
    FROM table
    WHERE etc...

    Phil
    paross1 Guest

  5. #4

    Default Re: How to convert ByteArray to String?

    Thanks Phil, I did that also.... COVERT and CAST, nothing works :-( , it did
    covert/cast the varbinary to a string but the output value is just not correct.
    Actually we used Hash() to encrypt a string and inserted it in a varbinary
    column, then simply retrieved it with a SELECT statement even without any
    COMVERT or CAST or toString function, it worked just fine with the older
    version of CF 4.x and SQL 7, but once we upgraded to MX and SQL 2000, it
    doesn't work any more. Any idea? Thanks again in advance.

    gmu Guest

  6. #5

    Default Re: How to convert ByteArray to String?

    Please try using the function CharsetEncode() for converting byte[] to string.
    A sample code snippet is given below.

    <cfquery name="testQuery" datasource="mssql">
    select data from TestTable where SNo = 1
    </cfquery>

    <cfoutput>
    <cfset strData = CharsetEncode(testQuery.data,"ISO-8859-1")>
    The string value is #strData#
    </cfoutput>

    Hope this helps.

    Jacob

    JacobG Guest

  7. #6

    Default Re: How to convert ByteArray to String?

    Thanks Jacob. But it still doesn't work, I got ' Variable CHARSETENCODE is
    undefined' error, I even tried BinaryEnocde and also got 'Variable
    BINARYENCODE is undefined' error. Then I checked out the CharsetEncode function
    on the Web, found out that these two functions (CharsetEncode and BinaryEncode)
    is added to MX 7, but we are using MX 6. Any other idea? Thanks again in
    advance.

    gmu Guest

  8. #7

    Default Re: How to convert ByteArray to String?

    I'd appreciate some help on this as well.
    My .cfm worked fine in CF4.5. Just upgraded to CFMX6.
    Now, once we retreive the value from the mySQL database, I want to perform
    simple string functions on it. (replacing <p> with <p class="text2"> for
    example)

    However, I get the same error: ByteArray objects cannot be converted to
    strings.
    Thanks!



    Nikade Guest

  9. #8

    Default Re: How to convert ByteArray to String?

    Nikade, did you get the correct text when using #tostring()#? Yes, the
    tostring() function did get rid of the 'ByteArray objects cannot be converted
    to strings' error, but when I output it, the text is not really correct.

    gmu Guest

  10. #9

    Default Re: How to convert ByteArray to String?

    Hi,
    Sorry that I am not aware of any built-in functions in CF 6.0 that can
    solve your problem right away. But, one of the workarounds I can suggest is to
    create a user defined function. Given below is the CF code snippet that makes
    use of a custom function for the same.

    <cffunction name="getString" access="private" returntype="string"
    output="false">
    <cfargument name="bytes" type="array" required=true/>
    <cfargument name="encoding" type="string" required=true>
    <cfset var strData =
    CreateObject("java","java.lang.String").init(argum ents.bytes,arguments.encoding)
    >
    <cfreturn strData>
    </cffunction>

    <cfquery name="testQuery" datasource="mssql">
    select data from TestTable where myID = 1
    </cfquery>

    <cfoutput query="testQuery">
    <cfset strData = getString(data,'ISO-8859-1')>
    Retrieved from database is #strData#
    </cfoutput>

    Hope this helps.

    Best Regards

    JacobG Guest

  11. #10

    Default Re: How to convert ByteArray to String?

    JacobG:
    Your UDF works like a charm; Thanks for the help!
    supermario29 Guest

  12. #11

    Default Re: How to convert ByteArray to String?

    Jacob, thanks for your second reply. Unfortunately it still doesn't work, I
    tried your built-in function but the result is the same as when I was just
    using tostring() function. We can select that varbinary column from the DB
    table and use either tostring() or the built-in function you provided to
    convert the varbinary value to a string, however, the string we got is still
    some garbage that we can't use for string comparison. It looks like it's still
    in binary version. This doesn't happen in CF4.x/SQL 7.

    gmu Guest

  13. #12

    Default Re: How to convert ByteArray to String?

    Hi,
    Sorry to hear that your problem still persists. Would like to try one more
    thing. In the UDF provided, the encoding is given as ISO-8859-1. Are you sure
    that ISO-8859-1 was the encoding used to create the binary data present in the
    DB. If you hadn't provided any specific character set encoding, it should be
    the default encoding supported by your OS. Please use that encoding format (if
    any) instead of ISO-8859-1. Please try this and let me know.

    Best Regards

    JacobG Guest

  14. #13

    Default Re: How to convert ByteArray to String?

    Thanks again, Jacob. I did try tostring(var, 'iso-8859-1') where var is the
    value retrives from a varbinary database column, it doesn't make any difference
    if I use iso-8859-1 or not. I am starting to think there's something different
    between SQL7 and SQL2000 and CF may not be able to convert sql2000's varbinary
    to string.....

    gmu Guest

  15. #14

    Default Re: How to convert ByteArray to String?

    I know that I used to be able to get away with more implicit type conversions
    when using CF 4.5 than with MX, so that is why you probably didn't need to do
    any conversions wneh using the older version.

    For what it is worth, I did a little experiment by creating a table with a
    varbinary column in my SQL Serer 2k database. I then attempted to perform a
    direct insert into the table with a 'string' value, and also a
    '#hash("string")#', and got the error message Macromedia][SQLServer JDBC
    Driver][SQLServer]Disallowed implicit conversion from data type varchar to data
    type varbinary, table 'dbo.Table1', column 'testval';. Use the CONVERT function
    to run this query. When I CAST the value of the string to varbinary using
    cast('#hash("string")#' as varbinary), the insert then worked without error.

    However, in order to retrieve the value from the table within a select
    statement, I had to use cast(testval as varchar) to explicitly convert the
    varbinary column to varchar, otherwise I got the [i/ByteArray objects cannot be
    converted to strings error message. So, it looks like whatever you get wneh you
    CAST your column to a varchar is what is actually in the database. Have you
    attempted to perform the same select statement with CAST() directly in SQL
    Server using Enterprise manager? If you are returning the same value as you get
    when running the query in CF, then I'm afraid that is what is actually stored
    in the database.

    Phil

    paross1 Guest

  16. #15

    Default Re: How to convert ByteArray to String?

    Thanks Phil. Yes, I did try cast() within a select statement, what I got is
    just some garbage, for example, one of the strings I got is: <???#????e*
    As you can see, it contains some special symbols/characters, this string was
    encrypted by the hash() funcation and then inserted into the varbinary column,
    I don't think it's the 'right' string since hash() function won't encrypt a
    string with specials symbols/characters like those.

    gmu Guest

  17. #16

    Default Re: How to convert ByteArray to String?

    Were you using a different encoding when the string was hashed than what you are using now?

    Phil
    paross1 Guest

  18. #17

    Default Re: How to convert ByteArray to String?

    I didn't write the code, but the person who wrote the code simply used
    Hash(string), I don't see any encoding is used with the hash() function. Is the
    hash() fuction on CF 4.0 different from CF MX? Speaking of which, actually I
    am still wondering how come the application has been using the hash() function
    on 4.0 with no problem, I thought hash() was not available till 6.0??? And
    those special symbols/characters I mentioned in my previous message is what I
    got when cast(0 it to varchar.

    gmu Guest

  19. #18

    Default Re: How to convert ByteArray to String?

    Does it look any differen/better/worse if you CAST() it to NVARCHAR?

    ;)
    Phil
    paross1 Guest

  20. #19

    Default Re: How to convert ByteArray to String?

    It makes worse, if I cast() to nvarchar, some asian characters come up :)
    gmu Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139