none
How to Input String to byte or byte array (not convert) RRS feed

  • Question

  • How to input String to byte or byte array (not convert string to byte)

    1)Case byte 

    string s = "0x30";

    byte result = s; 

    2)Case byte[]

    string s2 = "0x00, 0x35";

    byte[] result ={s2};

    .....

    Thank

    Narin Suksivakorn

    Friday, April 20, 2018 9:16 AM

All replies

  • Narin, 

    I think it will very difficult because there are no 8bit computers anymore which run C#.

    An int is the smallest addressable value type and has 32bits. 

    Therefore I think without code which does probably the same, you are not able to do this without converting. 


    Success
    Cor

    Friday, April 20, 2018 9:26 AM
  • Thank for your answer.

    if I wand to convert number 5000  to hex and bring hex to byte[]

    Can I use C# 

    Thank

    Narin

    Friday, April 20, 2018 9:49 AM
  • Thank for your answer.

    if I wand to convert number 5000  to hex and bring hex to byte[]

    Can I use C# 

    Thank

    Narin

    Ninux, 

    Before we go in the wrong direction.

    Hex is just a representation of a value. It is not a value. Using it in a string is in fact stupid.

    For instance this is correct 1985 style code.

    byte s = 0x30;
    byte[] s2 = { 0x00, 0x35 };
    It is only valid in a string if you could write NUL 0x00. However, that is the string terminator value. 


    Success
    Cor



    Friday, April 20, 2018 10:21 AM
  • It is very unclear what you are asking. The following might be relevant.

    byte[] result = { 0, 0x30, 0x35 };

    If you truly need to convert a string with a value such as "0x00, 0x35" then that would be complicated.



    Sam Hobbs
    SimpleSamples.Info

    Friday, April 20, 2018 6:43 PM
  • Cor, your answer is totally wrong.  When we call the current Intel processors "64-bit processors", that merely means the address bus and the registers are 64-bit wide.  The smallest addressable value is still 8 bits.  That's what the "byte" type is, after all.

    You can convert the 4-character string "0x30" to a byte by using Convert.ToInt32(s,16).

    To convert the string "0x00, 0x35" to a two-byte array containins [0x00,0x35], you will have to do that parsing yourself.  There's no built-in conversion.  Fortunately, it's not hard.  Just chop the string up into the individual parts by looking for the separator, and use Convert.ToInt32 on the parts.


    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Friday, April 20, 2018 10:42 PM